`
TimerBin
  • 浏览: 355564 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

MultipartFile upload

阅读更多

今天在使用Spring MVC 进行文件上传时提示出如下错误:

 

java.lang.IllegalStateException: File has been moved - cannot be read again
at org.springframework.web.multipart.commons.CommonsMultipartFile.getBytes(CommonsMultipartFile.java:108) ~[spring-web-3.2.4.RELEASE.jar:3.2.4.RELEASE]
......

 在网上查了一下File has been moved - cannot be read again 错误都说将以下的maxInMemorySize改大就可以了。改了以后发现确实可以了,但是为什么呢?

 

 

<bean id="multipartResolver"
		class="org.springframework.web.multipart.commons.CommonsMultipartResolver">    <property name="defaultEncoding" value="utf-8"></property>
    //允许最大可上传的文件大小
    <property name="maxUploadSize" value="10485760000"></property>
    //上传文件超出这个大小就会用于创建临时文件存储,而不使用内存存储
    <property name="maxInMemorySize" value="4194304"></property>
</bean>

 其中三个参数如下所示:

 

/**
  * Set the maximum allowed size (in bytes) before uploads are refused. 
  * -1 indicates no limit (the default).
  * @param maxUploadSize the maximum upload size allowed 
  * @see org.apache.commons.fileupload.FileUploadBase#setSizeMax
  */
  public void setMaxUploadSize(long maxUploadSize) {
    this.fileUpload.setSizeMax(maxUploadSize);
  }
/**
  * Set the maximum allowed size (in bytes) before uploads are written to disk.
  * Uploaded files will still be received past this amount, but they will not be
  * stored in memory. Default is 10240, according to Commons FileUpload.
  * @param maxInMemorySize the maximum in memory size allowed
  * @see org.apache.commons.fileupload.disk.DiskFileItemFactory#setSizeThreshold
  */ 
  public void setMaxInMemorySize(int maxInMemorySize) {
        this.fileItemFactory.setSizeThreshold(maxInMemorySize);
  }

从以上内容可以分析出,实际进行修改maxInMemorySize并没有解决掉 File has been moved - cannot be read again问题,而是避开了这个问题。将文件流不再写入到临时文件,而是用内存存储。

 

以下留下我的解决办法不依赖maxInMemorySize,不过这个方法是根据业务而定的,首先要找到生成的临时文件被使用的地方,由于我的业务上先将MultipartFile写成了文件,然后又尝试用MultipartFile.getBytes()进行读取该文件类型,所以出现此问题,代码如下所示:

错误代码:

public String uploadFile(MultipartFile file,String filePath) {
   ....
   //创建原图
   file.transferTo(new File(filePath));
   // 图片上传 类型
   String fileType = readPictureType(file);
   ......
}

改正后代码:

public String uploadFile(MultipartFile file,String filePath) {
   ....
   // 图片上传 类型
   String fileType = readPictureType(file);
   //创建原图
   file.transferTo(new File(filePath));
  
   ......
}

 

 

 

 

 

 

 

 

 

分享到:
评论
1 楼 tonyyan 2016-02-15  
感谢分享。

相关推荐

    上传文件加密的小实例

    1:对上传的文件进行压缩 然后加密 2:下载此文件要先解密才能解压

    Upload.java

    //上传 @RequestMapping("add") public String add... String tp = Upload.upload(file); if(""!=tp){ user.setimg(tp); } ud.add(gs); return "redirect:list";//return ""; 后面的值对应的是页面的名称 }

    实现阿里OSS对象存储带进度条

    阿里oss图片上传实现进度条显示页面jsp用ajax轮询获取session中的数据,通过页面标签显示进度;后台通过继承ProgressListener类获取进度数据时时写入session中;资源中有完整代码;

    upload_image.rar

    常见的上传文件几种方法,PartServlet/FileUploadServlet/MultipartFile/拖拽上传文件/剪切上传文件

    vue使用el-upload上传文件及Feign服务间传递文件的方法

    el-upload class="step_content" drag action="string" ref="upload" :multiple="false" :http-request="createAppVersion" :data="appVersion" :auto-upload="false" :limit="1" :on-change=...

    SmartUpload 上传组件(中文终极解决版) JDK1.6

    许多朋友反应使用后,出现Bad Version...1.上传默认方法upload(),指定编码使用upload("编码名称"); 2.下载默认方法downloadFile("要下载的文件路径"),指定编码使用downloadFile("要下载的文件路径", "编码名称")。

    SpringMVC文件上传下载

    public String upload(@RequestParam("myfiles") MultipartFile myfiles) throws IOException{ System.out.println(user.getBeanName()); System.out.println(user.getBeanFactory()); if(!myfiles....

    解决layui上传文件提示上传异常,实际文件已经上传成功的问题

    layui上传文件提示上传...public JSONObject uploadSourceData(@RequestParam(value="file") MultipartFile file ){ String filePath = “”; String fileName = filePath+System.currentTimeMillis()+"_"+file.getOrig

    uniapp前端单文件上传JAVA后台接收实现(亲测)

    uniapp前端单文件上传JAVA后台接收实现(亲测),看了些文档,亲测了一天,后面还有多页面上传文档代码上传哦

    《技术分享》图片上传

    记录一下图片上传的代码 哈 供以后参考,可以大致修改一下代码来搬砖。。。。。。。 先来说一下场景,这里是在spring环境下,是他...public ResultMoudel upload(MultipartFile imgFile){ if (imgFile.isEmpty()) {

    简单实现Android文件上传

    文件上传在B/S应用中是一种十分常见的...package com.xzq.upload; import java.io.DataOutputStream; import java.io.FileInputStream; import java.io.InputStream; import java.net.HttpURLConnection; import java

    带有进度指示通知的后台轻松上传文件(开箱即用的Multipart / Binary / FTP)-Android开发

    4.x入门Wiki:red_heart:Contributing:person_raising_hand:功能:person_raising_hand:谁在使用Upload Service:megaphone:Credits:scroll:许可功能Android 5.0(API 21)到Android 10(API) 29)支持微小的库上传...

    Spring-Boot-File-Handling-Tutorial:Spring Boot文件上传与下载教程

    对于文件上传,控制器中对应的上传方法的参数必须是MultipartFile对象, MultipartFile对象可以是一个表格对象,也可以是对应对象,如果是一个分区对象,则可以进行多文件上传;这里我们仅演示分开文件上传,下面的...

    SpringMVC批量上传

    List&lt;MultipartFile&gt; files = murequest.getFiles("image"); if( files !=null && files.size()&gt;0) { for(int i=0; i(); i++) { String uuid = UUID.randomUUID().toString().replace("-", "").toUpperCase...

    spring-boot-rest-ful-demo:Spring Boot应用程序的简单演示

    春天引导休息的演示原料药如何使用您可以通过将以下数据发送到地址http://192.168.59.103:8080/upload将文件(图片)发送到服务器: "name" - в формате String"file" - в формате MultipartFile...

    axios实现简单文件上传功能

    本文实例为大家分享了axios实现文件上传功能的具体代码,供大家参考,...&lt;span u03bfnclick=upload()&gt;上传图片 JS: function upload() { alert&#40;上传&#41; let file = document.getElementsByName('img')[0].fil

    解决IE下AjaxSubmit上传文件提示下载文件问题

    public String upload(HttpSession session,@RequestParam("file1") MultipartFile file,@RequestParam("paperId") String paperId,HttpServletRequest request) { //获取登录人员的id Integer userId = (In

    微信小程序实现上传图片功能

    微信小程序图片上传,供大家参考,具体内容如下 先来看一下微信小程序的api 来看一下页面效果 查看大图 wxml文件代码: ... &lt;view class=weui-uploader__info&gt;{{imageList.length}}/{{cou

Global site tag (gtag.js) - Google Analytics