原因很简单就是上传的文件太大。解决办法如下:
本人项目是Springboot项目,内置Tomcat,可以直接添加配置文件即可:
1.添加依赖
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3</version>
</dependency>
2.添加配置类
import org.springframework.boot.web.servlet.FilterRegistrationBean;
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.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
import javax.servlet.MultipartConfigElement;
import java.io.File;
/**
* @ProjectName: home_education
* @Package: com.lingnan.home_education.config
* @ClassName: CommonConfig
* @Author: chenziyu1@kungeek.com
* @Description: ${description}
* @Date: 2020/3/12 1:31
* @Version: 1.0
*/
@Configuration
public class CommonConfig {
@Bean
public MultipartConfigElement multipartConfigElement() {
MultipartConfigFactory factory = new MultipartConfigFactory();
//单个文件最大-30MB
factory.setMaxFileSize(DataSize.ofMegabytes(30));
//该方法已降级
//factory.setMaxRequestSize("30MB");
/// 设置总上传数据总大小-120MB
factory.setMaxRequestSize(DataSize.ofMegabytes(120));
return factory.createMultipartConfig();
}
}
个人实践是可以正常上传了的。
如果不是Springboot项目,查看外部tomcat配置文件server.xml并修改上传文件总大小的限制
<Connector port="8080" protocol="HTTP/1.1"
maxHttpHeaderSize="8089"
useBodyEncodingForURI="true"
maxPostSize="20971520"
maxThreads="2048"
redirectPort="8443"
enableLookups="false"
compression="on"
compressionMinSize="2048"
compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain"
connectionTimeout="20000"
disableUploadTimeout="true"
URIEncoding="UTF-8"/>
该方案没有尝试过,借鉴其他博客的,做下记录,如果有问题的话可以尝试一下。
还有一种情况就是因为Nginx的配置限制了上传的大小,从而引发这个问题。配置 nginx下的conf/nginx.conf
文件,修改client_max_body_size的值即可。
server {
listen 8054;
server_name localhost;
location /upload {
proxy_pass http://127.0.0.1:8001/ct-upload-service;
#set size of upload files
client_max_body_size 100m;
}
}