I\'m developing application based on Spring Boot and AngularJS using JHipster. My question is how to set max size of uploading files?
If I\'m trying
In spring 2.x
. Options have changed slightly. So the above answers are almost correct but not entirely . In your application.properties
file , add the following-
spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=10MB
I know this is extremely late to the game, but I wanted to post the additional issue I faced when using mysql, if anyone faces the same issue in future.
As some of the answers mentioned above, setting these in the application.properties will mostly fix the problem
spring.http.multipart.max-file-size=16MB
spring.http.multipart.max-request-size=16MB
I am setting it to 16 MB, because I am using mysql MEDIUMBLOB datatype to store the file.
But after fixing the application.properties, When uploading a file > 4MB gave the error: org.springframework.dao.TransientDataAccessResourceException: PreparedStatementCallback; SQL [insert into test_doc(doc_id, duration_of_doc, doc_version_id, file_name, doc) values (?, ?, ?, ?, ?)]; Packet for query is too large (6656781 > 4194304). You can change this value on the server by setting the max_allowed_packet' variable.; nested exception is com.mysql.jdbc.PacketTooBigException: Packet for query is too large (6656781 > 4194304). You can change this value on the server by setting the max_allowed_packet' variable.
So I ran this command from mysql:
SET GLOBAL max_allowed_packet = 1024*1024*16;
If you get a "connection resets" error, the problem could be in the Tomcat default connector maxSwallowSize attribute added from Tomcat 7.0.55 (ChangeLog)
From Apache Tomcat 8 Configuration Reference
maxSwallowSize: The maximum number of request body bytes (excluding transfer encoding overhead) that will be swallowed by Tomcat for an aborted upload. An aborted upload is when Tomcat knows that the request body is going to be ignored but the client still sends it. If Tomcat does not swallow the body the client is unlikely to see the response. If not specified the default of 2097152 (2 megabytes) will be used. A value of less than zero indicates that no limit should be enforced.
For Springboot embedded Tomcat declare a TomcatEmbeddedServletContainerFactory
Java 8:
@Bean
public TomcatEmbeddedServletContainerFactory tomcatEmbedded() {
TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();
tomcat.addConnectorCustomizers((TomcatConnectorCustomizer) connector -> {
if ((connector.getProtocolHandler() instanceof AbstractHttp11Protocol<?>)) {
//-1 for unlimited
((AbstractHttp11Protocol<?>) connector.getProtocolHandler()).setMaxSwallowSize(-1);
}
});
return tomcat;
}
Java 7:
@Bean
public TomcatEmbeddedServletContainerFactory tomcatEmbedded() {
TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();
tomcat.addConnectorCustomizers(new TomcatConnectorCustomizer() {
@Override
public void customize(Connector connector) {
if ((connector.getProtocolHandler() instanceof AbstractHttp11Protocol<?>)) {
//-1 for unlimited
((AbstractHttp11Protocol<?>) connector.getProtocolHandler()).setMaxSwallowSize(-1);
}
}
});
return tomcat;
}
Or in the Tomcat/conf/server.xml for 5MB
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"
maxSwallowSize="5242880" />
There is some difference when we define the properties in the application.properties and application yaml.
In application.yml :
spring:
http:
multipart:
max-file-size: 256KB
max-request-size: 256KB
And in application.propeties :
spring.http.multipart.max-file-size=128KB
spring.http.multipart.max-request-size=128KB
Note : Spring version 4.3 and Spring boot 1.4