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链接

spring-boot上传文件MultiPartFile获取不到文件问题解决的更多相关文章

  1. Spring Boot上传文件(带进度条)

    Spring Boot 上传文件(带进度条)# 配置文件 spring: freemarker: template-loader-path: classpath:/static/ ##Spring B ...

  2. springboot(十七):使用Spring Boot上传文件

    上传文件是互联网中常常应用的场景之一,最典型的情况就是上传头像等,今天就带着带着大家做一个Spring Boot上传文件的小案例. 1.pom包配置 我们使用Spring Boot最新版本1.5.9. ...

  3. (转)Spring Boot(十七):使用 Spring Boot 上传文件

    http://www.ityouknow.com/springboot/2018/01/12/spring-boot-upload-file.html 上传文件是互联网中常常应用的场景之一,最典型的情 ...

  4. Spring Boot(十七):使用Spring Boot上传文件

    Spring Boot(十七):使用Spring Boot上传文件 环境:Spring Boot最新版本1.5.9.jdk使用1.8.tomcat8.0 一.pom包配置 <parent> ...

  5. 使用Spring Boot上传文件

    原文:http://www.cnblogs.com/ityouknow/p/8298344.html 上传文件是互联网中常常应用的场景之一,最典型的情况就是上传头像等,今天就带着带着大家做一个Spri ...

  6. Spring Boot(十七):使用 Spring Boot 上传文件

      上传文件是互联网中常常应用的场景之一,最典型的情况就是上传头像等,今天就带着带着大家做一个 Spring Boot 上传文件的小案例. 1.pom 包配置 我们使用 Spring Boot 版本 ...

  7. Spring Boot 上传文件 获取项目根路径 物理地址 resttemplate上传文件

    springboot部署之后无法获取项目目录的问题: 之前看到网上有提问在开发一个springboot的项目时,在项目部署的时候遇到一个问题:就是我将项目导出为jar包,然后用java -jar 运行 ...

  8. Spring Boot上传文件

    我们使用Spring Boot最新版本1.5.9.jdk使用1.8.tomcat8.0. <parent> <groupId>org.springframework.boot& ...

  9. Java Spring Boot 上传文件和预览文件地址解析

    @RequestMapping(value ="/upload",method = RequestMethod.POST) @Permission(isAjax=false) pu ...

随机推荐

  1. linux下svn安装(ALI ECS)

    yum安装svn 搭建和使用SVN 可参考阿里云文档:https://help.aliyun.com/document_detail/52864.html?spm=5176.8208715.110.1 ...

  2. F. Asya And Kittens并查集

    F. Asya And Kittens time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  3. python嵌套列表

    从excel读取一行信息添加到一个临时列表,最后将所有行的列表添加到一个大列表. 源码: import xlrd,reclass Info(): def read_info(self): data = ...

  4. 笔试算法题(30):从已排序数组中确定数字出现的次数 & 最大公共子串和最大公共序列(LCS)

    出题:在已经排序的数组中,找出给定数字出现的次数: 分析: 解法1:由于数组已经排序,所以可以考虑使用二分查找确定给定数字A的第一个出现的位置m和最后一个出现的位置n,最后m-n+1就是A出现的次数: ...

  5. jmeter 接口测试

    web接口测试工具: 手工测试的话可以用postman ,自动化测试多是用到 Jmeter(开源).soupUI(开源&商业版). 下面将对前一篇Postman做接口测试中的接口用Jmeter ...

  6. 01--安装Activiti流程设计器eclipse插件

    Activiti1 安装流程设计器eclipse插件 Name:Activiti BPMN 2.0 designer(随便起个名字) Location: http://activiti.org/des ...

  7. LeetCode(6) ZigZag Conversion

    题目 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows li ...

  8. Far Relative’s Problem (贪心 计算来的最多客人)

    Description Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n  ...

  9. 杭电1722 Cake (分蛋糕)

    #include<cstdio> int f(int m,int n) { ) return n; else return f(n,m%n); } int main() { int m,n ...

  10. 集训第五周动态规划 J题 括号匹配

    Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...