Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring REST MultipartFile file is always null when do upload file

@RequestMapping(value = "{fileName:.+}", method = RequestMethod.POST, consumes = { MediaType.MULTIPART_FORM_DATA_VALUE})
public ResponseEntity<ResponseEnvelope<String>> uploadFile(
         @RequestParam("ownerId") Long ownerId, 
         @PathVariable("fileName") String fileName,
         @RequestBody MultipartFile file)
         throws Exception {
    ResponseEnvelope<String> env;
    if(null == certFileContent) {
        env = new ResponseEnvelope<String>("fail");
        return new ResponseEntity<ResponseEnvelope<String>>(env, HttpStatus.OK);
    }
    service.uploadCertificate(ownerId, fileName, certFileContent.getBytes());
    env = new ResponseEnvelope<String>("success");
    return new ResponseEntity<ResponseEnvelope<String>>(env, HttpStatus.OK);
}

Why I always get the file value is null, I've configure the multipart support,see below,

like image 342
Berry2081898 Avatar asked Dec 19 '13 10:12

Berry2081898


People also ask

How does MultipartFile work in spring?

The file contents are either stored in memory or temporarily on disk. In either case, the user is responsible for copying file contents to a session-level or persistent store as and if desired. The temporary storages will be cleared at the end of request processing.

How do I change a file to MultipartFile?

MultipartFile multipartFile = new MockMultipartFile("sourceFile. tmp", "Hello World". getBytes()); File file = new File("src/main/resources/targetFile. tmp"); try (OutputStream os = new FileOutputStream(file)) { os.

How do I use MultipartFile in Junit?

FileInputStream inputFile = new FileInputStream( "path of the file"); MockMultipartFile file = new MockMultipartFile("file", "NameOfTheFile", "multipart/form-data", inputFile); now use the file input as multipart file.

Does spring handle multipart request?

By default, Spring does no multipart handling, because some developers want to handle multiparts themselves. You enable Spring multipart handling by adding a multipart resolver to the web application's context. Each request is inspected to see if it contains a multipart.


2 Answers

The file should be binded to a RequestParam instead of the RequestBody as follows:

public ResponseEntity<ResponseEnvelope<String>> uploadFile(
         @RequestParam("ownerId") Long ownerId, 
         @PathVariable("fileName") String fileName,
         @RequestParam(value = "file") MultipartFile file)

This would correspond with the following HTML form:

<form method="post" action="some action" enctype="multipart/form-data">
    <input type="file" name="file" size="35"/>
</form>

Then in your dispatcher configuration specify the CommonsMultiPartResolver

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="5000000"/>
</bean>
like image 110
Kevin Bowersox Avatar answered Sep 28 '22 16:09

Kevin Bowersox


This is what worked for me,

Previously my input field was defined as,

<input type="file" />

I was getting null file with the above line but when I added the name="file" everything worked fine!

<input type="file" name="file" />

Hope this helps!

like image 21
Saumil Avatar answered Sep 28 '22 16:09

Saumil