Could not parse multipart servlet request; nested exception is : The temporary up

时间:2025-02-13 22:37:54

spring boot 上传文件出现以下错误:

{"timestamp":1539235200525,"status":500,"error":"Internal Server Error","exception":"","message":"Could not parse multipart servlet request; nested exception is : The temporary upload location [C:\\Users\\admin\\AppData\\Local\\Temp\\tomcat.6593687039777917764.8088\\work\\Tomcat\\localhost\\ROOT] is not valid","path":"/expertService/picture/upload"}

看错误是因为 C:\\Users\\admin\\AppData\\Local\\Temp\\tomcat.6593687039777917764.8088\\work\\Tomcat\\localhost\\ROOT 目录不存在导致文件上传失败,该目录是项目启动时,项目自动生成,但是,要是长时间(这个时间可能是1天,也可能是更长或更短)未使用该目录,操作系统会自动删除该临时目录。目录被删除,上传文件时找不见目录,就出错了。

解决方法:

1、重新启动该项目,重新生成新的临时目录;

2、在项目中添加以下代码,指定临时目录

@Bean
public MultipartConfigElement multipartConfigElement() {
    MultipartConfigFactory factory = new MultipartConfigFactory();
    ("./tmp");
    return ();
}

3、在项目配置文件中指定该目录

(这个目录的名字无论我起什么,都是在该项目下生成一个work的文件)

 = .\temp