1.现象是在spring-boot里加入commons-fileupload jar并且配置了mutilPart的bean,在upload的POST请求后,发现

multipartRequest.getFiles("file")=null,有点奇怪,查了文档资料才解决。
  1. <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
  2. <property name="maxUploadSize" value="104857600"/>
  3. <property name="maxInMemorySize" value="4096"/>
  4. </bean>

2.原因是:spring-boot自带的org.springframework.web.multipart.MultipartFile
和Multipart产生冲突,如果同时使用了MultipartResolver 和ServletFileUpload,就会在iter.hasNext()返回false.然后整个循环就跳出去了。整个问题产生的原因是Spring框架先调用了MultipartResolver 来处理http multi-part的请求。这里http multipart的请求已经消耗掉。后面又交给ServletFileUpload ,那么ServletFileUpload 就获取不到相应的multi-part请求。因此将multipartResolve配置去除,问题就解决了。

3. 单文件的话只需要一个变量即,多文件上传的话就将MultipartFile改为数组,然后分别上传保存即可。

  1. @RequestMapping(value="/multipleSave", method=RequestMethod.POST )
  2. public @ResponseBody String multipleSave(@RequestParam("file") MultipartFile[] files){
  3. String fileName = null;
  4. String msg = "";
  5. if (files != null && files.length >0) {
  6. for(int i =0 ;i< files.length; i++){
  7. try {
  8. fileName = files[i].getOriginalFilename();
  9. byte[] bytes = files[i].getBytes();
  10. BufferedOutputStream buffStream =
  11. new BufferedOutputStream(new FileOutputStream(new File("/tmp/" + fileName)));
  12. buffStream.write(bytes);
  13. buffStream.close();
  14. msg += "You have successfully uploaded " + fileName";
  15. } catch (Exception e) {
  16. return "You failed to upload " + fileName + ": " + e.getMessage();
  17. }
  18. }
  19. return msg;
  20. } else {
  21. return "Unable to upload. File is empty.";
  22. }
  23. }
  24. }

4.spring-boot 配置上传文件和请求文件的最大值限制:
直接在application.properties中
multipart.maxFileSize=128KB
multipart.maxRequestSize=128KB

5. spring-boot-starter-web are already added as dependencies. To upload files with Servlet containers, you need to register aMultipartConfigElement class (which would be <multipart-config> in web.xml). Thanks to Spring Boot, everything is auto-configured for you! spring-boot-upload链接

最新文章

  1. Android开发--Adapter的应用
  2. git 远程仓库 轻松创建
  3. htons
  4. JQuery EasyUI validatebox(验证框)
  5. iOS JSPatch 热修复使用
  6. asp.net datatable 导出为 txt
  7. js学习--DOM操作详解大全 前奏(认识DOM)
  8. ACdream群赛1112(Alice and Bob)
  9. uva 1401 dp+Trie
  10. WinForm LED循环显示信息,使用定时器Threading.Timer
  11. 线程:Semaphore实现信号灯
  12. ajax 动态添加商品列表
  13. SAP资产折旧,消息编号AA687:在上一年结算之后您只能记帐到新的一年
  14. 【java学习笔记】文件操作
  15. python 堆排序
  16. C# 当中 foreach 的原理
  17. Web 性能优化: 使用 Webpack 分离数据的正确方法
  18. springcloud-hystrix断路器对微服务的容错处理
  19. 关于VC工程编译不过去这件事
  20. ECharts JS应用:图表页面实现

热门文章

  1. R Programming week1-Reading Data
  2. 触发器deleted 表和 inserted 表详解
  3. 关于SQL Server数据表的五种约束
  4. MySql(一)mysql服务的基本操作及环境配置
  5. vue工程化:返回顶部和底部的动画效果
  6. HDOJ 1846 Brave Game - 博弈入门
  7. LOJ 2321 清华集训2017 无限之环 拆点+最小费用最大流
  8. [Algorithm] 1. A+B Problem
  9. db2事务日志已满解决办法
  10. C语言学习3