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--