Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring upload file size limit

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.

like image 241
Siriann Avatar asked Oct 26 '16 20:10

Siriann


People also ask

What is default multipart file upload size in spring boot?

The default is 10MB. file-size-threshold specifies the size threshold after which files will be written to disk.

How we can upload file in spring boot?

Spring Boot file uploader Create a Spring @Controller class; Add a method to the controller class which takes Spring's MultipartFile as an argument; Save the uploaded file to a directory on the server; and. Send a response code to the client indicating the Spring file upload was successful.

What is the maximum file size?

Maximum size The maximum file size in the FAT32 file system, for example, is 4,294,967,295 bytes, which is one byte less than four gigabytes. The table below details the maximum file size for a number of common or historical file systems. File system.


1 Answers

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

like image 128
Elouafi Avatar answered Oct 06 '22 19:10

Elouafi