MultipartException: the current request is not a multiple request

I am trying to create a backup controller to upload files. I saw 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 the postman to send pdf:

enter image description here

But the server crashes with an error:

 .MultipartException: Current request is not a multipart request 

Again I found this one and added the 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, he still complains about the same error.

+5
source share
2 answers

When you use Postman for multidisciplinary requests, do not include a custom Content-Type in the header. Therefore, your title tab in Postman should be blank. The postman will determine the border of the data form. On the Body Postman tab, you must select form data and select a file type. You can find the relevant discussion at https://github.com/postmanlabs/postman-app-support/issues/576

+10
source

It seems the problem is that the server request is not a multi-part request. Basically you need to change your client form. For instance:

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

Hope this helps.

+1
source

Source: https://habr.com/ru/post/1263755/


All Articles