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. 6 Specialzed layers 特殊层 第一部分 读书笔记

    6 Specialzed layers 特殊层  第一部分  读书笔记   Specialization is a feature of every complex organization. 专注是 ...

  2. 11 Hash tables

    11 Hash tables    Many applications require a dynamic set that supports only the dictionary operatio ...

  3. Python学习 Day 2-数据类型和变量

    数据类型和变量 在Python中,能够直接处理的数据类型有以下几种: 整数 Python可以处理任意大小的整数,当然包括负整数,在程序中的表示方法和数学上的写法一模一样,例如:1,100,-8080, ...

  4. 自定义对话框(jDialog)

    [配置项]jDialog options点击收起 一.接口功能 jDialog的默认配置项,本组件提供的所有对话框,都可以通过修改这些配置项来实现不同的效果. 二.详细配置项 /** * 对话框的默认 ...

  5. 火狐删除配置文件 会删除目录下所有文件 切记不要把配置文件建立在桌面 恢复软件:易我数据恢复向导 9.0 DiskGenius500

    火狐删除配置文件 会删除目录下所有文件 切记不要把配置文件建立在桌面 恢复软件:易我数据恢复向导 9.0  DiskGenius500 结果:由于时间比较常 恢复文件均失败了~

  6. python selenium等待特定网页元素加载完毕

    selenium等待特定元素加载完毕 is_disappeared = WebDriverWait(driver, 8, 0.5, ignored_exceptions=TimeoutExceptio ...

  7. WC2007 石头剪刀布 数学+最小费用最大流

    题面: 有N个人参加一场比赛,赛程规定任意两个人之间都要进行一场比赛:这样总共有N*(N-1)/2场比赛.比赛已经进行了一部分,我们想知道在极端情况下,比赛结束后最多会发生多少剪刀石头布情况.即给出已 ...

  8. Buffer.compare()

    Buffer.compare(buf1, buf2) buf1 {Buffer} buf2 {Buffer} 返回:{Number} 比较 buf1 和 buf2 通常用于 Buffer 数组的排序目 ...

  9. ruby on rails 常见配置错误解决

    error:in `require': cannot load such file -- sqlite3/sqlite3_native (LoadError) 先删除 Ruby下的D:\Ruby22- ...

  10. pyton学习之路

    文件操作 打开文件的模式有: r,只读模式(默认). w,只写模式.[不可读:不存在则创建:存在则删除内容:] a,追加模式.[可读:   不存在则创建:存在则只追加内容:] "+" ...