java.io.IOException: java.io.FileNotFoundException: /tmp/tomcat.273391201583741210.8080/work/Tomcat/localhost/ROOT/tmp/source/IMG_20160129_132623.jpg (No such file or directory)
问题源码:transferTo方法报错
// 前端传入mulFileSource
// 创建压缩前源文件
File fileSourcePath = new File("tmp/source/");
File fileSource = new File(fileSourcePath, mulFileSource.getOriginalFilename());
if (!fileSourcePath.exists()) {
fileSourcePath.mkdirs();
// 将接收得图片暂存到临时文件中
mulFileSource.transferTo(fileSource);
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的使用。
[Request processing failed; nested exception is org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request; nested exception is java.io .IOException: The temporary upload location [/tmp/tomcat.3814974221022613431.8080/work/Tomcat/localhost/ROOT] is not valid] with root causejava.io .IOException: The temporary upload location [/tmp/tomcat.3814974221022613431.8080/work/Tomcat/localhost/ROOT] is not valid