服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - Springboot文件上传出现找不到指定系统路径的解决

Springboot文件上传出现找不到指定系统路径的解决

2021-12-11 18:44带着希望活下去 Java教程

这篇文章主要介绍了Springboot文件上传出现找不到指定系统路径的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

Springboot文件上传出现找不到指定系统路径

1、问题描述

关键字:SpringMVC 4.2.4、Spring Boot 1.3.1、Servlet 3.0、文件上传

报错信息:

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方法报错

?
1
2
3
4
5
6
7
8
9
// 前端传入mulFileSource
// 创建压缩前源文件
File fileSourcePath = new File("tmp/source/");
File fileSource = new File(fileSourcePath, mulFileSource.getOriginalFilename());
if (!fileSourcePath.exists()) {
    fileSourcePath.mkdirs();
}
// 将接收得图片暂存到临时文件中
mulFileSource.transferTo(fileSource);

2、问题分析

首先,看源码中文件定义,相对路径,预期路径应该是项目路径/tmp/source/,但是报错确是一个系统临时文件路径(tomcat的)。

其次,由于是transferTo方法报错,因此应该是该方法写入文件时报错,因此,我们跟入方法源码。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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/source/,而transferTo方法预期写入的文件路径为/tmp/tomcat.273391201583741210.8080/work/Tomcat/localhost/ROOT/tmp/source/,我们并没有创建该目录,因此会抛出异常。

3、问题解决方案

使用绝对路径

修改location的值

这个location可以理解为临时文件目录,我们可以通过配置location的值,使其指向我们的项目路径,这样就解决了我们遇到的问题。

在Spring Boot下配置location,可以在main()方法所在文件中添加如下代码:

?
1
2
3
4
5
6
7
8
9
/**
 * 文件上传临时路径
 */
 @Bean
 MultipartConfigElement multipartConfigElement() {
    MultipartConfigFactory factory = new MultipartConfigFactory();
    factory.setLocation("/app/pttms/tmp");
    return factory.createMultipartConfig();
}

SpringBoot 上传文件时本地路径无效

SpringBoot 通过网关Zuul进行附件上传的时候,有时会出现如下错误

[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

错误产生的原因

1、spring boot的应用服务在启动的时候,会生成在操作系统的/tmp目录下生成一个Tomcat.*的文件目录,用于"java.io.tmpdir"文件流操作

?
1
TomcatEmbeddedServletContainerFactory

2、程序对文件的操作时:会生成临时文件,暂存在临时文件中;长时间不操作,导致/tmp下面的tomcat临时文件目录被删除,

且删除的文件不可恢复,上传文件时获取不到文件目录,报错

解决方式有以下几点

1.重启服务;

2.网关是否引入spring-boot-starter-web依赖,若无,则将其引入;

3、设置spring.servlet.multipart.enabled:true

Springboot文件上传出现找不到指定系统路径的解决

4.修改上传文件默认的BaseDir

以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/qq_35746739/article/details/104508412

延伸 · 阅读

精彩推荐