今天在使用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:对上传的文件进行压缩 然后加密 2:下载此文件要先解密才能解压
//上传 @RequestMapping("add") public String add... String tp = Upload.upload(file); if(""!=tp){ user.setimg(tp); } ud.add(gs); return "redirect:list";//return ""; 后面的值对应的是页面的名称 }
阿里oss图片上传实现进度条显示页面jsp用ajax轮询获取session中的数据,通过页面标签显示进度;后台通过继承ProgressListener类获取进度数据时时写入session中;资源中有完整代码;
常见的上传文件几种方法,PartServlet/FileUploadServlet/MultipartFile/拖拽上传文件/剪切上传文件
el-upload class="step_content" drag action="string" ref="upload" :multiple="false" :http-request="createAppVersion" :data="appVersion" :auto-upload="false" :limit="1" :on-change=...
许多朋友反应使用后,出现Bad Version...1.上传默认方法upload(),指定编码使用upload("编码名称"); 2.下载默认方法downloadFile("要下载的文件路径"),指定编码使用downloadFile("要下载的文件路径", "编码名称")。
public String upload(@RequestParam("myfiles") MultipartFile myfiles) throws IOException{ System.out.println(user.getBeanName()); System.out.println(user.getBeanFactory()); if(!myfiles....
layui上传文件提示上传...public JSONObject uploadSourceData(@RequestParam(value="file") MultipartFile file ){ String filePath = “”; String fileName = filePath+System.currentTimeMillis()+"_"+file.getOrig
uniapp前端单文件上传JAVA后台接收实现(亲测),看了些文档,亲测了一天,后面还有多页面上传文档代码上传哦
记录一下图片上传的代码 哈 供以后参考,可以大致修改一下代码来搬砖。。。。。。。 先来说一下场景,这里是在spring环境下,是他...public ResultMoudel upload(MultipartFile imgFile){ if (imgFile.isEmpty()) {
文件上传在B/S应用中是一种十分常见的...package com.xzq.upload; import java.io.DataOutputStream; import java.io.FileInputStream; import java.io.InputStream; import java.net.HttpURLConnection; import java
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)支持微小的库上传...
对于文件上传,控制器中对应的上传方法的参数必须是MultipartFile对象, MultipartFile对象可以是一个表格对象,也可以是对应对象,如果是一个分区对象,则可以进行多文件上传;这里我们仅演示分开文件上传,下面的...
List<MultipartFile> files = murequest.getFiles("image"); if( files !=null && files.size()>0) { for(int i=0; i(); i++) { String uuid = UUID.randomUUID().toString().replace("-", "").toUpperCase...
春天引导休息的演示原料药如何使用您可以通过将以下数据发送到地址http://192.168.59.103:8080/upload将文件(图片)发送到服务器: "name" - в формате String"file" - в формате MultipartFile...
本文实例为大家分享了axios实现文件上传功能的具体代码,供大家参考,...<span u03bfnclick=upload()>上传图片 JS: function upload() { alert(上传) let file = document.getElementsByName('img')[0].fil
public String upload(HttpSession session,@RequestParam("file1") MultipartFile file,@RequestParam("paperId") String paperId,HttpServletRequest request) { //获取登录人员的id Integer userId = (In
微信小程序图片上传,供大家参考,具体内容如下 先来看一下微信小程序的api 来看一下页面效果 查看大图 wxml文件代码: ... <view class=weui-uploader__info>{{imageList.length}}/{{cou