简体   繁体   English

如何在Spring Boot中使用MultipartConfigElement一次上传多个文件?

[英]How to upload multiple files at once using MultipartConfigElement in Spring Boot?

I am using Spring Boot 1.1.3 with the CommonsMultipartResolver to allow uploading of multiple files at once. 我使用Spring Boot 1.1.3与CommonsMultipartResolver一起上传多个文件。

I get this stacktrace when I try to upload a file bigger than 1 MB: 当我尝试上传大于1 MB的文件时,我得到了这个堆栈跟踪:

Caused by: org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: 
The field files[] exceeds its maximum permitted size of 1048576 bytes.
    at org.apache.tomcat.util.http.fileupload.FileUploadBase$FileItemIteratorImpl$FileItemStreamImpl$1.raiseError(FileUploadBase.java:637)
    at org.apache.tomcat.util.http.fileupload.util.LimitedInputStream.checkLimit(LimitedInputStream.java:76)
    at org.apache.tomcat.util.http.fileupload.util.LimitedInputStream.read(LimitedInputStream.java:135)
    at java.io.FilterInputStream.read(FilterInputStream.java:107)

I tried setting the max upload size like this: 我尝试设置这样的最大上传大小:

public MultipartResolver multipartResolver()
{
    CommonsMultipartResolver resolver = new CommonsMultipartResolver();
    resolver.setMaxUploadSize( 100 * MEGABYTE_IN_BYTES );
    return resolver;
}

However, this does not work. 但是,这不起作用。 I have found this Spring guide on upoading files and there they use MultipartConfigFactory instead. 我已经找到了关于upoading文件的Spring指南,他们使用MultipartConfigFactory代替。 However, I now need to use the MultipartFile class instead of MultipartHttpServletRequest in my controller. 但是,我现在需要在我的控制器中使用MultipartFile类而不是MultipartHttpServletRequest

With the MultipartHttpServletRequest I could do getFileMap() to get all the files, but there is no such method on MultipartFile . 使用MultipartHttpServletRequest我可以使用getFileMap()来获取所有文件,但是在MultipartFile上没有这样的方法。

Any ideas on how to work with MultipartConfigFactory and multiple files? 有关如何使用MultipartConfigFactory和多个文件的任何想法? I am using jquery-file-upload on the client if that would matter. 我在客户端上使用jquery-file-upload,如果这很重要的话。

The right way to increase upload limit is to set property 增加上传限制的正确方法是设置属性
multipart.maxFileSize=10Mb
In your application.properties file. 在您的application.properties文件中。 You can read more on this topic here https://stackoverflow.com/a/27062232/668417 and here MultipartProperties.java 你可以在这里阅读更多关于这个主题的信息https://stackoverflow.com/a/27062232/668417和这里的MultipartProperties.java

The question was asked with regard to Spring Boot 1.1.3. 问题是关于Spring Boot 1.1.3。 As time marches forward the accepted solutions no longer work with newer versions of Spring Boot. 随着时间的推移,已接受的解决方案不再适用于较新版本的Spring Boot。 However the problem of increasing the maximum allowable upload size persists. 但是,增加最大允许上载大小的问题仍然存在。

I am writing this as of Spring Boot 1.4.2, so this solution may not be correct in perpetuity. 我在Spring Boot 1.4.2中写这个,所以这个解决方案可能永远不正确。

From the spring guide to uploading files the new properties are thus: 从弹簧指南到上传文件 ,新属性因此:

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

Seems there is no need to use the MultipartFile in the Controller, you can also use the MultipartHttpServletRequest . 似乎不需要在Controller中使用MultipartFile ,您也可以使用MultipartHttpServletRequest This is the signature of my @RestController annotated class: 这是我的@RestController注释类的签名:

@RequestMapping(value = "/{datasheetId}/addDoc", method = RequestMethod.POST)
@Secured(ROLE_USER)
public ResponseEntity<?> addDocumentToDatasheet( Principal principal,
                                                 @PathVariable("datasheetId") long datasheetId,
                                                 MultipartHttpServletRequest request ) throws IOException
{
     Map<String, MultipartFile> fileMap = request.getFileMap();
     // handle fileMap further...
}

for the size problem in a my project I use this properties in application.properties: 对于我的项目中的大小问题,我在application.properties中使用此属性:

multipart.max-file-size=100Mb
multipart.max-request-size=100Mb

for the case of have multiple file I suggest of use a bean like this 对于有多个文件的情况我建议使用这样的bean

@Data
class MultipartDTO{

    private MultipartFile file1; 
    private MultipartFile file2;
    .....
    private MultipartFile filen; 

    //eventually other proerties
}

then of course in your controller you colud have a thing like this 然后当然在你的控制器中,你的colud有这样的事情

@RequestMapping(value = "/multipartUrl", method = RequestMethod.POST, consumes = "multipart/form-data")
public ResponseEntity multipartCall(MultipartDTO multipartDTO){
    ....
}

I hope this can help you 我希望这可以帮到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM