1

I have a Spring REST file upload controller that I need to restrict the allowed file types. I have a predefined list to check against, and I can do it with the following code snippet.

public ResponseEntity<Object> uploadFile(MultipartFile file) {
        
        if(!file.isEmpty()) {
            log.debug("File name: " + file.getOriginalFilename());
            if(ipConfigService.isFileTypeSupported(file.getOriginalFilename())) {

However, as the files being upload could be quite large, I want to reject unsupported files without having to wait for the whole file to be transferred first (which is what's happening in the above example).

Is there a way to annotate the uploadFile method to only accept certain types (dynamically pulled from cache/database), or is there a way I can intercept the request to get the name or mime type without waiting for the full binary to upload?

I can see in the request from the UX that mime type is also sent:

------WebKitFormBoundarygcPzBq2V6QNzl25V Content-Disposition: form-data; name="file"; filename="TestFile.mp4" Content-Type: video/mp4


------WebKitFormBoundarygcPzBq2V6QNzl25V--
Drew
  • 464
  • 1
  • 5
  • 18
  • Have you tried the `consumes` parameter of the `@PostMapping` or the `@RequestMapping` annotations? – Cebrail Yilmaz Oct 12 '20 at 11:31
  • Maybe this [stackoverflow question](https://stackoverflow.com/questions/4328947/limit-file-format-when-using-input-type-file) be also relevant if you're coding the frontend too. – Cebrail Yilmaz Oct 12 '20 at 11:34
  • @CebrailYilmaz the consumes is actually multipart/form-data, so that doesn't help in this context – Drew Oct 12 '20 at 16:18

0 Answers0