解决使用Spring Boot、Multipartfile实现上传提示无法找到文件的问题
前言
SpringBoot使用MultiPartFile接收来自表单的file文件,然后进行服务器的上传是一个项目最基本的需求,我以前的项目都是基于SpringMVC框架搭建的,所以在使用SpringBoot的时候进行MultiPartFile上传遇到了坑
遇到的问题
由于我需要对文件进行MIME-TYPE安全校验,所以代码中先后两次调用了MultipartFile的transferTo()方法,在第二次调用的时候报错,提示无法找到.tmp文件(事实上我在本地windows10环境下,两次都没报错,可以正常上传,是在服务器上运行时发现第二次调用报错了,我查了一下,具体原因没找到,可能和linux的临时文件回收策略有关),以下是报错信息:
java.io.FileNotFoundException: /home/app/sxpservice/temp/upload_e46ffe57_8ee6_4353_b533_d57c040bbc60_00000000.tmp (No such file or directory)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at org.apache.tomcat.util.http.fileupload.disk.DiskFileItem.getInputStream(DiskFileItem.java:188)
at org.apache.catalina.core.ApplicationPart.getInputStream(ApplicationPart.java:100)
at org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile.getBytes(StandardMultipartHttpServletRequest.java:292)
at com.suixingpay.fin.pcc.controller.UploadController.in(UploadController.java:54)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:644)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.boot.web.filter.ApplicationContextHeaderFilter.doFilterInternal(ApplicationContextHeaderFilter.java:55)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
注意:这里的报错是提示一个后缀名为.tmp的临时文件找不到,而不是.png之类的(和你上传的文件相同格式)找不到,如果是后者,那么你遇到的是另一个问题,是location设置问题,具体解决方法可以百度,这里我们就不讲了
问题分析
我们看一下transferTo()的实现方式可以了解到,其实就是对文件流进行了读取,我们应该知道,流只可以被读取一次,所以第二次读取的时候肯定就报错了,所以我考虑不直接读取流,而是将流复制一份,这时候我想到将第一次调用改为使用FileUtils.copyInputStreamToFile()方法
问题解决
具体使用方式如下:
//获取上传的文件名
String fileName = uploadFile.getOriginalFilename();
// 获取文件后缀名
String suffix=fileName.substring(fileName.lastIndexOf("."));
// 用uuid作为文件名,防止生成的临时文件重复
File dest = File.createTempFile(UuidUtils.getUuid(), suffix);
// 将上传文件复制到临时文件
FileUtils.copyInputStreamToFile(uploadFile.getInputStream(),dest);
这样就实现了文件流的复制,进而实现文件上传,并且一个流可以使用多次
第一次写博客,有不足和错误的地方,欢迎斧正,勿喷~~感谢感谢~
解决使用Spring Boot、Multipartfile实现上传提示无法找到文件的问题的更多相关文章
- Spring Boot 在接收上传文件时,文件过大异常处理问题
Spring Boot 在接收上传文件时,文件过大时,或者请求过大,spring内部处理都会抛出异常,并且捕获不到. 虽然可以通过调节配置,增大 请求的限制值. 但是还是不太方便. 之所以捕获不到异常 ...
- 【Spring Boot】关于上传文件例子的剖析
目录 Spring Boot 上传文件 功能实现 增加ControllerFileUploadController 增加ServiceStorageService 增加一个Thymeleaf页面 修改 ...
- 基于Spring Boot的图片上传
package com.clou.inteface.domain.web.user; import java.io.File; import java.io.IOException; import j ...
- [转]Spring Boot修改最大上传文件限制:The field file exceeds its maximum permitted size of 1048576 bytes.
来源:http://blog.csdn.net/awmw74520/article/details/70230591 SpringBoot做文件上传时出现了The field file exceeds ...
- Spring Boot 使用 ServletFileUpload上传文件失败,upload.parseRequest(request)为空
使用Apache Commons FileUpload组件上传文件时总是返回null,调试发现ServletFileUpload对象为空,在Spring Boot中有默认的文件上传组件,在使用Serv ...
- Spring Boot +Bootstrap 图片上传与下载,以及在bootstrap-table中的显示
1.前台上传: <input type="file" name="file" id="file"> 2.后台的接收与处理: St ...
- spring boot 2.X上传文件限制大小
Spring Boot 1.3.x multipart.maxFileSize multipart.maxRequestSize Spring Boot 1.4.x and 1.5.x spring. ...
- spring boot 使用java9上传到github其他人clone后报错
错误原因: Java.lang.NoClassDefFoundError:javax/xml/bind/JAXBException jdk9存在版本兼容问题. 经过查找资料发现问题所在 大致意思是ja ...
- Spring Boot + Vue 前后端分离,两种文件上传方式总结
在Vue.js 中,如果网络请求使用 axios ,并且使用了 ElementUI 库,那么一般来说,文件上传有两种不同的实现方案: 通过 Ajax 实现文件上传 通过 ElementUI 里边的 U ...
随机推荐
- LitepalNewDemo【开源数据库ORM框架-LitePal2.0.0版本的使用】
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 本Demo使用的是LitePal2.0.0版本,对于旧项目如何升级到2.0.0版本,请阅读<赶快使用LitePal 2.0版本 ...
- 麒麟子Cocos Creator实用技巧一:如何正确地显示微信头像
不管是游戏App,还是H5,又或者是微信小游戏.但凡接入了微信登录的应用,都可能需要显示微信头像. 在Cocos Creator中,我们常见的显示方法像下面这样 var headimg = 'http ...
- 从PRISM开始学WPF(番外)共享上下文 RegionContext-更新至Prism7.1
RegionContext共享上下文 There are a lot of scenarios where you might want to share contextual information ...
- Webpack系列-第三篇流程杂记
系列文章 Webpack系列-第一篇基础杂记 Webpack系列-第二篇插件机制杂记 Webpack系列-第三篇流程杂记 前言 本文章个人理解, 只是为了理清webpack流程, 没有关注内部过多细节 ...
- Docker在Linux上运行NetCore系列(三)在Linux上使用Docker运行Asp.NetCore
转发请注明此文章作者与路径,请尊重原著,违者必究. 系列文章:https://www.cnblogs.com/alunchen/p/10121379.html 开始说明 上几篇文章都是通过Linux运 ...
- 常见js报错
1Uncaught TypeError: Cannot read property 'length' of null Uncaught TypeError: Cannot read property ...
- WebForm+一般处理程序+Ajax聊天
#### 很容易理解 插入数据 到数据库 在使用 setInterval() 读取数据 显示在 页面中 好了 不废话 直接上代码 不会的 可以加我 微信 Jth11163## 效果图片 ![在这里插入 ...
- 杂牌机搞机之旅(二)————移植TWRP第三方Recovery并刷入
原本想把杂牌机作为android破解和开发的测试机,破解的话肯定是安装框架的嘛,毕竟有些是要涉及到脱壳 . 但是,我尝试安装xposed的时候,手机卡在了开机界面,也就是magisk出现了错误,如果想 ...
- Poj1543
Perfect Cubes Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16522 Accepted: 8444 De ...
- 折腾Java设计模式之迭代器模式
迭代器模式 Provide a way to access the elements of an aggregate object sequentially without exposing its ...