服务报错
/tmp/tomcat.582075295829581120.7301/work/Tomcat/localhost/*** (No such file or directory)
很多解决方案都是配置修改临时存储目录或者修改服务器tmp的配置文件,修改清理时间,我觉得都复杂了。
我的解决方法是查看源代码发现在setServletContext时,会读取系统配置的临时缓存文件目录
if (!this.isUploadTempDirSpecified()) {
this.getFileItemFactory().setRepository(WebUtils.getTempDir(servletContext));
}
getTempDir方法实现
public static File getTempDir(ServletContext servletContext) {
Assert.notNull(servletContext, "ServletContext must not be null");
return (File)servletContext.getAttribute("javax.servlet.context.tempdir");
}
所以只需要判断读取到的目录是否存在就可以了
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
ServletContext servletContext = request.getSession().getServletContext();
String temporaryDirectory = servletContext.getAttribute("javax.servlet.context.tempdir").toString();
File tmpFile = new File(temporaryDirectory);
if (!tmpFile.exists()) {
log.info("目录被删除重新创建"+temporaryDirectory);
tmpFile.mkdirs();
}
multipartResolver.setServletContext(request.getSession().getServletContext());