Spring--《Spring实战》The temporary upload location [/tmp/uploads] is not valid
2017-11-14 08:52
701 查看
在看《Spring实战》第七章的时候,需要上传文件,书上说的是将上传的图片保存在/tmp/uploads这个目录下,因此我给项目的根路径下创建了/tmp/uploads这个目录,但是却出现了标题中的错误,经过一番斗争之后,明白了问题的所在。
源码一目了然,使用Servlet3.0的支持的上传文件功能时,如果我们没有使用绝对路径的话,transferTo方法会在相对路径前添加一个location路径,即:file = new File(location, fileName);。当然,这也影响了SpringMVC的Multipartfile的使用。
由于我们创建的File在项目路径/tmp/uploads/,而transferTo方法预期写入的文件路径为/etc/tpmcat/work/Catalina/localhost/ROOT/tmp/uploads/,注意此时写入的路径是相对于你本地tomcat的路径,因此书上的代码:
也只是在本地tomcat服务器的根路径下创建/tmp/uploads,此时本地服务器并没有这个目录,因此报错。
2.修改location的值
这个location可以理解为临时文件目录,我们可以通过配置location的值,使其指向我们的项目路径,这样就解决了我们遇到的问题。代码如下:
这样就解决错误啦~~
问题分析
要解决这个问题,我们需要看一下Spring的源码:public class StandardMultipartHttpServletRequest extends AbstractMultipartHttpServletRequest { //中间代码省略 /** * Spring MultipartFile adapter, wrapping a Servlet 3.0 Part object. */ @SuppressWarnings("serial") private static class StandardMultipartFile implements MultipartFile, Serializable { //中间代码省略 @Override public void transferTo(File dest) throws IOException,IllegalStateException { this.part.write(dest.getPath()); } } }
package org.apache.catalina.core; /** * Adaptor to allow {@link FileItem} objects generated by the package renamed * commons-upload to be used by the Servlet 3.0 upload API that expects * {@link Part}s. */ public class ApplicationPart implements Part { //中间代码省略 @Override public void write(String fileName) throws IOException { File file = new File(fileName); if (!file.isAbsolute()) { file = new File(location, fileName); } try { fileItem.write(file); } catch (Exception e) { throw new IOException(e); } } }
源码一目了然,使用Servlet3.0的支持的上传文件功能时,如果我们没有使用绝对路径的话,transferTo方法会在相对路径前添加一个location路径,即:file = new File(location, fileName);。当然,这也影响了SpringMVC的Multipartfile的使用。
由于我们创建的File在项目路径/tmp/uploads/,而transferTo方法预期写入的文件路径为/etc/tpmcat/work/Catalina/localhost/ROOT/tmp/uploads/,注意此时写入的路径是相对于你本地tomcat的路径,因此书上的代码:
@Override protected void customizeRegistration(Dynamic registration) { registration.setMultipartConfig( new MultipartConfigElement("/tmp/uploads") ); }
也只是在本地tomcat服务器的根路径下创建/tmp/uploads,此时本地服务器并没有这个目录,因此报错。
解决方法
1.使用绝对路径2.修改location的值
这个location可以理解为临时文件目录,我们可以通过配置location的值,使其指向我们的项目路径,这样就解决了我们遇到的问题。代码如下:
@Override protected void customizeRegistration(Dynamic registration) { registration.setMultipartConfig( new MultipartConfigElement("/home/hg_yi/Web项目/spittr/web") ); }
profilePicture.write("/tmp/uploads" + profilePicture.getSubmittedFileName());
这样就解决错误啦~~
相关文章推荐
- spring boot上传文件 The temporary upload location is not valid
- SpringBoot项目的The temporary upload location ***is not valid 问题
- Could not parse multipart servlet request; nested exception is java.io.IOException: The temporary upload location
- spring boot 上传文件 Caused by: java.io.IOException: The temporary upload location [/tmp/tomcat.**/work/
- spring boot 上传文件出错:java.io.IOException: The temporary upload location
- nested exception is java.io.IOException: The temporary upload location
- the app xxx.app could not be added to your iTunes library because it is not a valid app
- URL-encoded form data is not valid ,Operation is not valid due to the current state of the object
- spring整合hibernate的时候使用session的时候报:is not valid without active transaction
- VMware安装VMware tool是 遇到The path "" is not a valid path to the 3.10.0-693.el7.x86_64 kernel headers.
- VM tools安装错误The path "" is not a valid path to the xx generic kernel headers.
- kali安装VMware Tools出现The path "" is not a valid path……
- 群晖NAS使用Docker安装迅雷离线下载出现the active key is not valid.
- 【转载】关于错误:ASP.NET The URL-encoded form data is not valid. .
- spring声明式管理事务+ hibernate + save is not valid without active transaction
- Operation is not valid due to the current state of the object.
- 关于The temporary upload location [/tmp/tomcat.5422911029087836936.28080/work/Tomcat/localhost/ROOT]异常
- 关于 error: Operation is not valid due to the current state of the object。
- VM tools安装错误The path "" is not a valid path to the xx generic kernel headers
- The addressing-1.5.mar module, which is not valid, caused null java.lang.ExceptionInInitializerError