当前位置: 代码迷 >> 综合 >> 文件上传报错:Could not parse multipart servlet request; nested exception is java.io.IOException
  详细解决方案

文件上传报错:Could not parse multipart servlet request; nested exception is java.io.IOException

热度:89   发布时间:2023-11-17 13:00:44.0

最近在做文件上传的功能,文件上传功能一直是好的,今天突然莫名的报错了:

       Could not parse multipart servlet request; nested exception is java.io.IOException: The temporary upload location.......

百度翻译:无法分析多部分servlet请求;嵌套异常为java.io.io exception:临时上载位置

解决办法:

         创建临时文件存储路径。

   我习惯把文件信息写在配置文件中,而不是用常量写在类中,因此我的写法如下:(建议写在配置文件当中)

springboot的配置文件:

xxxx:upload:filePath: D:\uploadFile\ #上传存储本地文件的位置 localTempPath: D:\temp\ #本地文件临时文件夹allowTypes:   #允许上传的文件类型- application/pdffileSize: 10485760 #字节,允许上传文件大小10M
package com.dhcc.znbt.business.util;import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;import java.util.List;/*** @Author: Lucifer* @Date: 2019/1/21 15:04* @Description: 文件上传的配置类*/
@Data
@ConfigurationProperties(prefix = "xxxx.upload")
public class UploadProperties {//文件存储路径private String filePath;//文件临时存储路径(必有,否则上传可能会出错)private String localTempPath;//允许上传文件的格式private List<String> allowTypes;//允许上传文件大小private long fileSize;
}
package com.dhcc.znbt.business.util;import org.springframework.beans.factory.annotation.Autowired;
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;/*** @Author: Lucifer* @Date: 2019/1/31 16:08* @Description: 文件临时路径 配置类*/
@Configuration
public class MultipartConfig {@Autowiredprivate UploadProperties uploadProperties;/*** 文件上传临时路径* 注:文件存储临时文件夹必须有,否则上传文件,可能会报错:*      Could not parse multipart servlet request; nested exception is java.io.IOException: The temporary upload location*/@BeanMultipartConfigElement multipartConfigElement() {MultipartConfigFactory factory = new MultipartConfigFactory();//判断是否有文件夹,没有就创建File file = new File(uploadProperties.getLocalTempPath());if (!file.exists()) {file.getParentFile().mkdirs();}//文件路径,如:d:\temp\factory.setLocation(uploadProperties.getLocalTempPath());return factory.createMultipartConfig();}}