Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MultipartException: Current request is not a multipart request

Tags:

java

rest

spring

I am trying to make a restful controller to upload files. I have seen this and made this controller:

@RestController public class MaterialController {      @RequestMapping(value="/upload", method= RequestMethod.POST)     public String handleFileUpload(             @RequestParam("file") MultipartFile file){         String name = "test11";         if (!file.isEmpty()) {             try {                 byte[] bytes = file.getBytes();                 BufferedOutputStream stream =                         new BufferedOutputStream(new FileOutputStream(new File(name + "-uploaded")));                 stream.write(bytes);                 stream.close();                 return "You successfully uploaded " + name + " into " + name + "-uploaded !";             } catch (Exception e) {                 return "You failed to upload " + name + " => " + e.getMessage();             }         } else {             return "You failed to upload " + name + " because the file was empty.";         }     } } 

and then i used postman to send a pdf:

enter image description here

But the server crashes with the error:

.MultipartException: Current request is not a multipart request 

Again i have found this, and added a bean.xml file

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">      <bean id="multipartResolver"           class="org.springframework.web.multipart.commons.CommonsMultipartResolver">     </bean> </beans> 

Unfortunately, it still complains with the same error.

like image 299
Sal-laS Avatar asked Feb 02 '17 21:02

Sal-laS


People also ask

What is @RequestPart?

Using @RequestPartThis annotation associates a part of a multipart request with the method argument, which is useful for sending complex multi-attribute data as payload, e.g., JSON or XML.

What is multipart API?

Multipart requests combine one or more sets of data into a single body, separated by boundaries. You typically use these requests for file uploads and for transferring data of several types in a single request (for example, a file along with a JSON object).


1 Answers

When you are using Postman for multipart request then don't specify a custom Content-Type in Header. So your Header tab in Postman should be empty. Postman will determine form-data boundary. In Body tab of Postman you should select form-data and select file type. You can find related discussion at https://github.com/postmanlabs/postman-app-support/issues/576

like image 166
abaghel Avatar answered Sep 22 '22 17:09

abaghel