15

I'm using Spring Boot for my application, and I want to upload some files into my database. I used a tutorial to achive this, and it works fine. My problem is that I don't know how to set max file size to upload. The default is 1MB but that's just not enough for me.

I added these lines to my application.properties:

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

but it didn't help.

My code:

FileService.java

@Service
public class FileService {
@Autowired
FileRepository fileRepository;
public Response uploadFile(MultipartHttpServletRequest request) throws  IOException {
    
    Response response = new Response();
    List fileList = new ArrayList();
    
    Iterator<String> itr = request.getFileNames();
    
    while (itr.hasNext()) {
        String uploadedFile = itr.next();
        MultipartFile file = request.getFile(uploadedFile);
        String mimeType = file.getContentType();
        String filename = file.getOriginalFilename();
        byte[] bytes = file.getBytes();

        File newFile = new File(filename, bytes, mimeType);
        File savedFile = fileRepository.saveAndFlush(newFile);
        savedFile.setFile(null);
        fileList.add(savedFile);
    }
    
    response.setReport(fileList);
    return response;
}
}

FileController.java

@RestController
@RequestMapping("/file")
public class FileController {
            
    @Autowired
    FileService fileService;
@RequestMapping(value = "/upload", method = RequestMethod.POST)
    public Response uploadFile(MultipartHttpServletRequest request) throws IOException{
        return fileService.uploadFile(request);
     }
}

This code is just fine, it works perfectly, I just can't set max file size.

Paolo
  • 20,112
  • 21
  • 72
  • 113
Siriann
  • 405
  • 1
  • 6
  • 16
  • properties looks fine, Are you getting any error when you try with bigger file? – Sundararaj Govindasamy Oct 26 '16 at 20:55
  • MultipartException Could not parse multipart servlet request; nested exception is java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: The field uploadfile exceeds its maximum permitted size of 1048576 bytes. – Siriann Oct 27 '16 at 04:59
  • Which spring boot version are you using. – M. Deinum Oct 27 '16 at 05:55
  • I used 1.3. The names of the properties are changed, these were multipart.maxFileSize and multipart.maxRequestSize. After I switched to Spring Boot 1.4 the properties above worked fine. – Siriann Oct 29 '16 at 10:32
  • List of spring properties: https://docs.spring.io/spring-boot/docs/current/reference/html/application-properties.html#application-properties.web.spring.servlet.multipart.enabled – Jordan Stewart Sep 21 '21 at 03:33

3 Answers3

19

With Spring earlier than 4.0 the right properties are

multipart.maxFileSize

multipart.maxRequestSize

From Spring 4 these were changed to

spring.http.multipart.max-file-size

spring.http.multipart.max-request-size

cst1992
  • 3,823
  • 1
  • 29
  • 40
Siriann
  • 405
  • 1
  • 6
  • 16
14

This configuration worked for me:

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

Reference to docs: Tuning File Upload Limits

Paolo
  • 20,112
  • 21
  • 72
  • 113
Elouafi
  • 281
  • 4
  • 12
13

For me worked (in Spring Boot 2.0.0):

spring.servlet.multipart.max-file-size=-1
Stéphane Bruckert
  • 21,706
  • 14
  • 92
  • 130
MaciekS
  • 151
  • 1
  • 3
  • 2
    Negative value mean that this restriction is disabled at all. You can do it so but in this case you have to control incoming traffic on front. Negative value is an open door for DDoS. – Mike Menko Nov 25 '20 at 08:36