The request was rejected because no multipart boun

2019-01-17 03:37发布

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;
}

7条回答
我只想做你的唯一
2楼-- · 2019-01-17 03:39

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

Sample

查看更多
唯我独甜
3楼-- · 2019-01-17 03:40

problem isn't in your code .problem with in your request. you are not getting any kinda boundary in multi-part request.

查看更多
叼着烟拽天下
4楼-- · 2019-01-17 03:41

when I use postman to send a file which is 5.6M to external network , I met the same trouble. (The same action is succeeded on my own computer and local testing environment.) After check all the server configs and http headers, I found that the reason is Postman may have some trouble simulate request to external http request. Finally, I did the sendfile request on chrome html page successfully. Just as a reference :)

查看更多
一夜七次
5楼-- · 2019-01-17 03:42

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.

查看更多
▲ chillily
6楼-- · 2019-01-17 03:50

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

查看更多
放荡不羁爱自由
7楼-- · 2019-01-17 03:54

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

Backend: Endpoint controller definition

Postman: Headers setup POST Body setup

查看更多
登录 后发表回答