环境概述
前端上传Excel文件,后端通过Spring的MultipartFile对象接收。
问题现象
本地环境始终可以,线上环境偶尔可以。偶尔的报错提示如下
java.io.FileNotFoundException: /tmp/tomcat.6000477750703684958.8120/work/Tomcat/localhost/ROOT/upload_03540bf9_3ae6_4233_af15_cd87c5bacebf_00000000.tmp (No such file or directory)
原因解析
官方源码注释如下:
/**
* A representation of an uploaded file received in a multipart request.
*
* <p>The file contents are either stored in memory or temporarily on disk.
* In either case, the user is responsible for copying file contents to a
* session-level or persistent store as and if desired. The temporary storage
* will be cleared at the end of request processing.
*
* @author Juergen Hoeller
* @author Trevor D. Cook
* @since 29.09.2003
* @see org.springframework.web.multipart.MultipartHttpServletRequest
* @see org.springframework.web.multipart.MultipartResolver
*/
本人理解(自行翻译):
请求中收到的文件的,文件内容存储在内存中或临时存储在磁盘上。 *在任何一种情况下,用户都有责任根据需要将文件内容复制到会话级或持久性存储中。临时存储将在请求处理结束时清除。
所以呢?所以要保存处理或转文件对象。是不是解决了?是不是so easy?
解决方案
知道原因了,解决方案就多了。
我的解决方案是:将MultipartFile对象转化为File对象。
怎么转?请自行百度,方案太多了。