* tomcat中配置虚拟目录
修改server.xml,在里面添加
<Context docBase="D:\develop\upload\temp" path="/pic" reloadable="false"/>
这样,就可以通过访问http://localhost:8080/pic访问到d盘下的文件
eclipse中设置
依赖jar
SpringMVC.xml中配置文件上传解析器
<!-- 文件上传,id必须设置为multipartResolver -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 设置文件上传大小 单位B 50M-->
<property name="maxUploadSize" value="52428800" />
</bean>
jsp页面修改
<form action="${pageContext.request.contextPath}/item/fileUpload.action"
method="post" enctype="multipart/form-data">
<input type="file" name="file"> <input type="submit"
value="提交">
</form>
enctype 必须改为 multipart/form-data
controller方法
@RequestMapping("/fileUpload.action")
public String doFileUpload(@RequestParam(required = false) MultipartFile file,
HttpServletRequest request
) throws IllegalStateException, IOException{
if(file==null){
return "itemList";
String orginFileName = file.getOriginalFilename();
String fileName = UUID.randomUUID()+""+orginFileName.substring(orginFileName.lastIndexOf("."));
String path = request.getServletContext().getRealPath(File.separatorChar+"WEB-INF"+File.separatorChar+"upload");
File file2 = new File(path, fileName);
if(!file2.getParentFile().exists()){
file2.getParentFile().mkdirs();
file.transferTo(file2);
return "forward:/itemList.action";
文件路径访问不到的异常
因为父目录不存在,加上file.getParentFile().mkdirs()创建父目录解决
存放文件的路径说明
1. 直接在webRoot下创建upload文件夹,上传文件放在这里,在浏览器上能够直接访问到,因此这里存放是安全的
http://localhost:8081/goldSpringDemo/upload/0ea8aa64-76c6-49bf-9ecd-3a56c9fa87ed.xls
2. 在webRoot下的WEB-INF下创建upload文件夹,将上传文件放在这里,浏览器无法直接访问到,这里比较安全
,本例中用的就是这种
多文件上传
修改jsp页面
<form action="${pageContext.request.contextPath}/item/fileUploads.action"
method="post" enctype="multipart/form-data">
<input type="file" name="files">
<input type="file" name="files">
<input type="file" name="files">
<input type="submit" value="提交">
</form>
修改controller方法形参,遍历后按照单个文件的处理流程处理就是
@RequestMapping("/fileUploads.action")
public String doFileUploads(@RequestParam(required = false) List<MultipartFile> files,
HttpServletRequest request
) throws IllegalStateException, IOException{
if(files==null){
return "itemList";
for(int i = 0;i<files.size();i++){
MultipartFile file = files.get(i);
String orginFileName = file.getOriginalFilename();
String fileName = UUID.randomUUID()+""+orginFileName.substring(orginFileName.lastIndexOf("."));
String path = request.getServletContext().getRealPath(File.separatorChar+"WEB-INF"+File.separatorChar+"upload");
File file2 = new File(path, fileName);
if(!file2.getParentFile().exists()){
file2.getParentFile().mkdirs();
file.transferTo(file2);
return "forward:/item/itemList.action";
Json数据交互
@RequestBody注解将请求的json数据转为java对象进行绑定
@ResponseBody将controller方法返回的java对象转为json响应客户端。
jar支持
JSON转换器
如果xml中使用注解驱动<mvc:annotation-driven/>,不需要配置json转换器,不适用注解驱动就需要配置
<!--处理器适配器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
</list>
</property>
</bean>
开始测试下:
jsp页面:
<script type="text/javascript">
//live 能够对还没有添加进DOM的元素有效,jquery 1.7后使用on替换
$("#bt").live("click",function(){
$.ajax({
type:'GET',
url:contentpath+'/item/json.action',
data:$("#form1").serialize(),
contentType:"application/x-www-form-urlencoded;charset=utf-8",
dataType:'json',
success:function(data){
console.log(data);
error:function(){
alert('服务器连接失败,请重试!');
</script>
<form action="" id="form1">
<table>
<td>编号</td>
<input type="text" name="item.id">
<td>名称</td>
<input type="text" name="item.name">
<td>时间</td>
<input type="text" name="item.time">
<td>备注</td>
<input type="text" name="item.remark">
<input id="bt" type="button" value="ajax提交">
</table>
</form>
controller方法
@RequestMapping("/json.action")
public @ResponseBody Item dojsonSupport(QueryVo qv){
return qv.getItem();
因为要上传文件所以得用form-data,上传文件同时还想再放入一个List集合对象,实现方式就是前段用JSON.stringify()方法将JavaScript对象或值转换为JSON字符串,后台使用字符串接收然后解析一下。
图片是AirPost接口调试工具,类似postMan
这里传入的字符串已经是json格式的了,图片是多个文件;
后台可以接收到files,feedbackBills集合和其他数据了
import com.fasterxml.jackson.core.type.TypeR
springmvc的
文件上传和
json数据转化
springmvc的
文件上传json数据的转换
springmvc的
文件上传
struts的
文件上传回顾
1,上传界面有所要求,需要多功能表单
enctype="mutipart/form-data" method="post"
2,需要一个处理
文件上传的子控制器
UpladfileAction
File file
SpringMVC常见应用 1、@RequestMapping 此注解放在类上用于窄化请求、防止重名@Controller
@RequestMapping("item")
public class ItemsController {
@Autowired
private ItemsService itemsService;
@Request...
在Spring 4.0.2版本提供的restful服务中,可以通过注解@RequestPart来同时上传格式化的XML/JSON数据和文件。
后端Controller包含Multipart和JSON的请求写法如下:
@RequestMapping(value = "/executesampleservice", method = RequestMethod.POST,
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</de...
当使用Spring MVC进行异步文件上传时,需要进行以下步骤:
1. 在Spring MVC的配置文件中启用异步支持,可以使用<mvc:annotation-driven>元素或@EnableWebMvc注解来完成。
2. 创建一个控制器方法来处理文件上传请求。该方法应该使用@RequestParam注解来接收文件,并且需要使用@RequestBody注解来指定处理请求体的方式为异步。
3. 创建一个异步任务来处理文件上传操作。可以使用Spring的异步任务支持,即@Async注解来完成。在异步任务中,可以使用Spring的ResourceUtils类将文件保存到指定的位置。
4. 返回一个异步结果对象来表示文件上传的状态。可以使用DeferredResult或ListenableFuture等Spring提供的异步结果对象来完成。
下面是一个示例控制器方法的代码:
@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
@ResponseBody
public DeferredResult<String> handleFileUpload(@RequestParam("file") MultipartFile file) {
DeferredResult<String> deferredResult = new DeferredResult<>();
AsyncTask asyncTask = new AsyncTask(file, deferredResult);
asyncTask.execute();
return deferredResult;
在该示例代码中,handleFileUpload方法使用DeferredResult来表示异步处理的结果,然后创建一个AsyncTask对象来处理文件上传操作。AsyncTask类是一个自定义的异步任务类,其中包含一个execute方法,用于执行文件上传操作。在该方法中,使用ResourceUtils类将文件保存到指定的位置,并最终将文件上传的结果通知给DeferredResult对象。
请注意,在使用Spring MVC进行文件上传时,需要确保上传的文件大小不超过服务器所允许的最大限制。可以通过在Spring MVC的配置文件中配置multipartResolver来设置上传文件的最大大小。
MySQL5.7 安装报错 --initialize specified but the data directory exists and is not writable
人间花木: