【问题标题】:Changing the size-limit for file upload on spring boot 2.1.2在 Spring Boot 2.1.2 上更改文件上传的大小限制
【发布时间】:2019-10-05 13:32:22
【问题描述】:

我正在开发一个带有 angular 前端和 springboot 2.1.2 后端的项目,并且已经实现了一种上传图像的方法。但是它仅在图像小于 1MB 时才有效,更大的图像会导致异常:

org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: 字段图像超过其最大允许大小 1048576 字节

我已经看过这些问题: I am trying to set maxFileSize but it is not honored Spring upload file size limit

我尝试添加

spring.servlet.multipart.max-File-Size: 30MB 
spring.servlet.multipart.max-Request-Size: 30MB

当它也不起作用时

spring.servlet.multipart.maxFileSize: 30MB
spring.servlet.multipart.maxRequestSize: 30MB

到我的 application.yml,它们似乎都没有做任何事情。

我还发现了这个: https://www.baeldung.com/spring-maxuploadsizeexceeded

并尝试按照该站点的第 2 点所述设置限制。

这些都没有奏效。任何想法我做错了什么? 这是我的 application.yml:

logging:
  file: ./log/backend.log
  level.: WARN

banner:
  location: banner/banner.txt

server:
  context-path: /

spring:
  application:
    name: Backend
  jpa:
    hibernate:
      ddl-auto: validate
      use-new-id-generator-mappings: true
  profiles:
    active: development
  http:
    multipart:
      maxFileSize: 30MB
      maxRequestSize: 30MB

security:
  basic:
    enabled: false

management:
  info:
    git:
      mode: full

这是我的控制器:

@RestController
@RequestMapping(value = "/api/v1/image")
@Api(value = "Image")
public class ImageEndpoint {

    @Autowired
    private ImageMapper imageMapper;
    @Autowired
    private IImageService imageService;

    @RequestMapping(method = RequestMethod.POST)
    @PreAuthorize("hasRole('ADMIN')")
    @ApiOperation(value = "Upload a new image", authorizations = {@Authorization(value = "apiKey")})
    @ResponseStatus(HttpStatus.OK)
    public @ResponseHeader Long post(@RequestParam ("image") MultipartFile input){
        try {
            return imageService.save(imageMapper.dtoToEntitiy(new ImageDto(input.getBytes())));
        }catch (IOException io){
            throw new ResponseStatusException(HttpStatus.BAD_REQUEST);
        }
    }

更新:

  servlet:
    multipart:
      max-request-size: 30MB
      max-file-size: 30MB

这使它工作,我刚开始添加它后重新编译,所以更改没有通过,我实际上只需要重新构建才能使其工作。

【问题讨论】:

  • 显示一些您为上传文件而编写的代码,我的意思是分段上传

标签: java spring spring-boot file-upload configuration


【解决方案1】:

您可以尝试在属性文件中使用以下配置吗?

multipart.max-file-size=30MB
multipart.max-request-size=30MB

如果是 yml 配置,请添加以下设置。

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

要上传不限长度的文件,您可以设置如下。

spring.http.multipart.max-file-size=-1

希望它对你有用。

【讨论】:

    猜你喜欢
    • 2019-04-10
    • 2017-03-09
    • 1970-01-01
    • 2017-05-19
    • 2018-12-26
    • 2022-12-21
    • 1970-01-01
    • 2015-11-29
    • 2011-10-23
    相关资源
    最近更新 更多