93

As I am trying this with spring boot and webservices with postman chrome add-ons.

In postman content-type="multipart/form-data" and I am getting the below exception.

HTTP Status 500 - Request processing failed; 
nested exception is org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request; 
nested exception is java.io.IOException: 
org.apache.tomcat.util.http.fileupload.FileUploadException: the request was rejected because no multipart boundary was found

In Controller I specified the below code

@ResponseBody
@RequestMapping(value = "/file", headers = "Content-Type= multipart/form-data", method = RequestMethod.POST)

public String upload(@RequestParam("name") String name,
        @RequestParam(value = "file", required = true) MultipartFile file)
//@RequestParam ()CommonsMultipartFile[] fileUpload
{
    // @RequestMapping(value="/newDocument", , method = RequestMethod.POST)
    if (!file.isEmpty()) {
        try {
            byte[] fileContent = file.getBytes();
            fileSystemHandler.create(123, fileContent, name);
            return "You successfully uploaded " + name + "!";
        } catch (Exception e) {
            return "You failed to upload " + name + " => " + e.getMessage();
        }
    } else {
        return "You failed to upload " + name + " because the file was empty.";
    }
}

Here I specify the file handler code

public String create(int jonId, byte[] fileContent, String name) {
    String status = "Created file...";
    try {
        String path = env.getProperty("file.uploadPath") + name;
        File newFile = new File(path);
        newFile.createNewFile();
        BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(newFile));
        stream.write(fileContent);
        stream.close();
    } catch (IOException ex) {
        status = "Failed to create file...";
        Logger.getLogger(FileSystemHandler.class.getName()).log(Level.SEVERE, null, ex);
    }
    return status;
}
Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
Mohammed Javed
  • 943
  • 2
  • 7
  • 10
  • I also facing the same problem, and its only work in postman not working with other tools like "Advance rest client". may I know why?? – Narandhran Thangavel Mar 28 '17 at 06:31
  • 1
    @Narendhran, we can upload files from ARC now, which will eliminate this problem. Please check this: https://stackoverflow.com/a/59342761/3519504 – Sandeep Kumar Dec 15 '19 at 09:52

10 Answers10

154

The problem is that you are setting the Content-Type by yourself, let it be blank. Google Chrome will do it for you. The multipart Content-Type needs to know the file boundary, and when you remove the Content-Type, Postman will do it automagically for you.

cristid9
  • 1,070
  • 1
  • 17
  • 37
de.la.ru
  • 2,994
  • 1
  • 27
  • 32
26

Unchecked the content type in Postman and postman automatically detect the content type based on your input in the run time.

Sample

mate00
  • 2,727
  • 5
  • 26
  • 34
Bala
  • 391
  • 4
  • 6
21

This worked for me: Uploading a file via Postman, to a SpringMVC backend webapp:

Backend: Endpoint controller definition

Postman: Headers setup POST Body setup

gorjanz
  • 1,954
  • 2
  • 18
  • 13
3

I was having the same problem while making a POST request from Postman and later I could solve the problem by setting a custom Content-Type with a boundary value set along with it like this.

I thought people can run into similar problem and hence, I'm sharing my solution.

postman

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
1

Heard you can do this in postman:

placeholders

Bitzu
  • 125
  • 2
  • 11
0

The "Postman - REST Client" is not suitable for doing post action with setting content-type.You can try to use "Advanced REST client" or others.

Additionally, headers was replace by consumes and produces since Spring 3.1 M2, see https://spring.io/blog/2011/06/13/spring-3-1-m2-spring-mvc-enhancements. And you can directly use produces = MediaType.MULTIPART_FORM_DATA_VALUE.

tomeokin
  • 17
  • 3
  • This is really helpful answer. It solved my problem. With Advanced REST client, I am able to send the same request which I was trying with Postman. Postman requests were resulting in an errors `org.apache.commons.fileupload.FileUploadException: the request was rejected because no multipart boundary was found` and `HTTP 405` – Aniket Kulkarni Nov 23 '18 at 04:26
0

When I use postman to send a file which is 5.6M to an external network, I faced the same issue. The same action is succeeded on my own computer and local testing environment.

After checking all the server configs and HTTP headers, I found that the reason is Postman may have some trouble simulating requests to external HTTP requests. Finally, I did the sendfile request on the chrome HTML page successfully. Just as a reference :)

Sandeep Kumar
  • 2,397
  • 5
  • 30
  • 37
0

I met this problem because I use request.js which writen base on axios
And I already set a defaults.headers in request.js

import axios from 'axios'
const request = axios.create({
  baseURL: '', 
  timeout: 15000 
})
service.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'

here is how I solve this
instead of

request.post('/manage/product/upload.do',
      param,config
    )

I use axios directly send request,and didn't add config

axios.post('/manage/product/upload.do',
      param
    )

hope this can solve your problem

Hanz
  • 244
  • 3
  • 8
0

You can try with below simple code, it should work. I tested on Advanced REST Client and below attached screenshot will help for configuration.

package com.example.demo;

import java.io.FileOutputStream;
import java.io.IOException;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
public class ImageUploadController {

    @PostMapping("image-upload")
    public String uploadImage(@RequestParam MultipartFile myFile) throws IOException {
        
        byte[] bytes = myFile.getBytes();

        FileOutputStream fileOutputStream = new FileOutputStream("image.jpg");

        fileOutputStream.write(bytes);
        fileOutputStream.close();
        
        return "Image uploaded successfully..!";

    }
}

enter image description here

you can find uploaded image on below location in project.

enter image description here

Also please note that if your controller should be within the package of @SpringBootApplication package. You can refer below image.

enter image description here

Mr.A
  • 41
  • 4
0

You do not need to send Content-Type in Postman, leave the default setting as in Headers section. It should automatically calculate. enter image description here

In Body, you can select file as: enter image description here

At Spring controller side, you can add following code:

 @PostMapping("/upload")  
 public ResponseEntity<MyResponseObject> uploadFile(@RequestParam("file") MultipartFile myFile)
  throws IOException {...}
Sanjay Amin
  • 341
  • 1
  • 4
  • 13