解决使用Spring Boot、Multipartfile上传文件路径错误问题

错误信息:The temporary upload location[C:\Users\……]is not valid

原因:之前运行良好是因为,springboot启动时会创建一个/tmp/tomcat.7*/work/Tomcat/localhost/ROOT的临时目录作为文件上传的临时目录,但是该目录会在10天之后被系统自动清理掉。

解决办法:
1 重启项目,系统会自动重新生成该目录
2 手动创建该目录

3 在代码中增加系统默认目录配置 ,如下:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.MultipartConfigElement;
import java.io.File;

/**
 * 文件上传临时文件配置
 * Created by win 10 on 2018/4/17.
 */
@Configuration
public class MultipartConfig {

    @Value("${report.temporary.file.path}")
    private String filePath;

    /**
     * 文件上传临时路径
     */
    @Bean
    MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        File tmpFile = new File(filePath);
        if (!tmpFile.exists()) {
            tmpFile.mkdirs();
        }
        factory.setLocation(filePath);
        return factory.createMultipartConfig();
    }
}

猜你喜欢

转载自blog.csdn.net/qiunian144084/article/details/79975625