带有gridfs大小限制异常的Spring多部分文件上载

时间:2017-10-18 08:35:05

标签: java spring mongodb jhipster gridfs

使用Jhipster和Spring + Mongo以及Gridfs来处理db中保存的文件。当我尝试上传大于1Mb的文件时,它会给我一个500错误:

java.io.IOException: UT000054: The maximum size 1048576 for an individual file in a multipart request was exceeded

尝试在application-dev.yml中设置此项但未成功:

spring:
    http:
        multipart:
            max-file-size: 10MB
            max-request-size: 10MB

如何更改此限制?

5 个答案:

答案 0 :(得分:3)

将此内容添加到WebConfigurer(如果存在)

@Bean

public MultipartConfigElement multipartConfigElement() {

     MultipartConfigFactory factory = new MultipartConfigFactory();

     factory.setMaxFileSize("100MB");

     factory.setMaxRequestSize("100MB");

     return factory.createMultipartConfig();

}

答案 1 :(得分:2)

当JHipster使用下载时,一种解决方法是在多部分解析器bean中设置上传大小,如下所示:

@Configuration
public class UndertowConfiguration {

    @Value("${spring.http.multipart.max-file-size:10}")
    private long maxFileSize;

    @Bean(name = "multipartResolver")
    public MultipartResolver multipartResolver() {
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
        multipartResolver.setDefaultEncoding("utf-8");
        multipartResolver.setResolveLazily(true);
        multipartResolver.setMaxUploadSize(1024*1024*maxFileSize);
        multipartResolver.setMaxUploadSizePerFile(1024*1024*maxFileSize);
        return multipartResolver;
    }
}

答案 2 :(得分:1)

您可以在application-dev.yml文件中配置这两个属性,而不是在application.properties中对其进行配置:

spring.http.multipart.max-file-size=10MB
spring.http.multipart.max-request-size=10MB

有关其他信息,您可以查看Spring Uploading file指南。

作为旁注,如果您稍后决定迁移到 spring-boot 2.0 ,则这些属性已从spring.http.multipart更改为spring.servlet.multipart

答案 3 :(得分:1)

试试这个,

spring:
    servlet:
        multipart:
            max-file-size: 10MB
            max-request-size: 10MB

答案 4 :(得分:0)

对于spring boot2.x:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.spring.mvc</groupId>
  <artifactId>springboot-api</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>springboot-api</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
  
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>2.3.1.RELEASE</version>
    </dependency>
    
    <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-jpa -->
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
        <version>2.2.5.RELEASE</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.apache.derby/derby -->
    <dependency>
        <groupId>org.apache.derby</groupId>
        <artifactId>derby</artifactId>
        <version>10.14.2.0</version>
        <scope>test</scope>
    </dependency>
  
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

以及旧版本

spring:
    servlet:
        multipart:
            max-file-size: 10MB
            max-request-size: 10MB

要获得无限制的上传文件大小,请将设置设置为max-file-size:-1将使其具有无限的文件大小。