强烈推荐一个大神的人工智能的教程:http://www.captainai.net/zhanghan
【前言】
有个在线上跑了一段时间的上传文件的程序报错,查了些资料,最终通过设置上传文件的临时路径解决此问题,在此与大家共享。
【问题解决之路】
一、报错信息
org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request; nested exception is java.io.IOException: The temporary upload location[/tmp/tomcat.53876517222872.8078/work/Tomcat/localhost/ROOT] is not valid
二、问题解决思路增加上传的配置类(其中最主要是 factory.setLocation(uploadTmp))
/* * Copyright (c) 2019. zhanghan_java@163.com All Rights Reserved. * 项目名称:实战SpringBoot * 类名称:MultipartConfig.java * 创建人:张晗 * 联系方式:zhanghan_java@163.com * 开源地址: https://github.com/dangnianchuntian/springboot * 博客地址: https://blog.csdn.net/zhanghan18333611647 */ package com.zhanghan.zhboot.config; 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 org.springframework.util.unit.DataSize; import org.springframework.web.multipart.commons.CommonsMultipartResolver; import javax.servlet.MultipartConfigElement; @Configuration public class MultipartConfig { @Value("${upload.maxFileSize}") private String uploadMaxFileSize; @Value("${upload.MaxRequestSize}") private String uploadMaxRequestSize; @Value("${upload.tmp}") private String uploadTmp; @Bean public MultipartConfigElement multipartConfigElement() { MultipartConfigFactory factory = new MultipartConfigFactory(); factory.setMaxFileSize(DataSize.ofMegabytes(Long.parseLong(uploadMaxFileSize))); factory.setMaxRequestSize(DataSize.ofMegabytes(Long.parseLong(uploadMaxRequestSize))); factory.setLocation(uploadTmp); return factory.createMultipartConfig(); } @Bean public CommonsMultipartResolver multipartResolver() { return new CommonsMultipartResolver(); } }
三、项目地址及代码版本:
1、地址:https://github.com/dangnianchuntian/springboot
2、代码版本:1.4.0-Release
【总结】
1、实战出真知;
2、不断积累。