Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring file upload content type validation, always octet-stream?

I'm trying to implement pdf file uploading in my Restful Spring Boot application.

I have the following method;

@RequestMapping(value = FILE_URL, method = RequestMethod.POST)
public ResponseDTO submitPDF(
        @ModelAttribute("file") FileDTO file) {

    MediaType mediaType = MediaType.parseMediaType(file.getFile().getContentType());

    System.out.println(file.getFile().getContentType());
    System.out.println(mediaType);
    System.out.println(mediaType.getType());

    if(!"application/pdf".equals(mediaType.getType())) {
        throw new IllegalArgumentException("Incorrect file type, PDF required.");
    }

    ... more code here ...
}

FileDTO is just a wrapper around MultipartFile.

I'm then using Postman to POST the request with form-data body 'file'=<filename.pdf>

The content type in the printlns above is ALWAYS octet-stream. No matter what type of file I send in (png, pdf, etc) its always octet stream. If I specifically set application/pdf as Content-Type header in Postman the MultipartFile within FileDTO ends up as null.

Question is, is there something wrong with my Spring Controller method, or is the request just not being built correctly by Postman?

If Postman can't get the Content-Type right, can I expect actual client apps to properly set content type to pdf?

like image 648
Nick Foote Avatar asked Dec 02 '25 15:12

Nick Foote


2 Answers

Have you tried Apache Tika library for detecting mime types of uploaded file?

Code Example in Kotlin

private fun getMimeType(file: File) = Tika().detect(file)
like image 119
Dias Abdraimov Avatar answered Dec 04 '25 06:12

Dias Abdraimov


The FileDTO will wrap the whole content of the multipart/form-data so if your uploaded file input is named file, your DTO/Form/POJO should be alike to:

class FileDTO{

  @NotNull
  private String anotherAttribute;

  @NotNull
  private MultipartFile file;

  //Getters and Setters
}

Therefore you should also change your controller function to

@RequestMapping(value = FILE_URL, method = RequestMethod.POST)
public ResponseDTO submitPDF(@ModelAttribute FileDTO fileWrapper) {

    MediaType mediaType = MediaType.parseMediaType(fileWrapper.getFile().getContentType());

    System.out.println(fileWrapper.getFile().getContentType());
    System.out.println(mediaType);
    System.out.println(mediaType.getType());

    if(!"application/pdf".equals(mediaType.getType())) {
        throw new IllegalArgumentException("Incorrect file type, PDF required.");
    }

    ... more code here ...
}

To use this sort of functions you should use the StandardServletMultipartResolver in your MVC configurer file. Something like:

@EnableWebMvc
@Configuration
@ComponentScan("mypackage.web.etc")
public class WebMvcConfig extends WebMvcConfigurerAdapter{

    @Bean
    public StandardServletMultipartResolver multipartResolver() {
        return new StandardServletMultipartResolver();
    }
}

I hope it suites you.

like image 31
EliuX Avatar answered Dec 04 '25 07:12

EliuX



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!