Java 请求被拒绝,因为在 springboot 中没有找到多部分边界

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36005436/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 17:22:10  来源:igfitidea点击:

The request was rejected because no multipart boundary was found in springboot

javaweb-servicesfile-uploadspring-bootmultifile-uploader

提问by Mohammed Javed

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

因为我正在尝试使用 spring boot 和带有 postman chrome 附加组件的 web 服务。

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

在邮递员中content-type="multipart/form-data",我收到以下异常。

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

回答by de.la.ru

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

问题是你Content-Type自己设置的,让它为空。谷歌浏览器将为您完成。multipartContent-Type需要知道文件边界,当您删除 时Content-Type,Postman 会自动为您完成。

回答by tomeokin

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.

“Postman - REST Client”不适合设置 content-type 的 post action。您可以尝试使用“Advanced REST client”或其他。

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.

此外,从 Spring 3.1 M2 开始,headers 被消耗和生产替换,请参阅https://spring.io/blog/2011/06/13/spring-3-1-m2-spring-mvc-enhancements。并且可以直接使用produces = MediaType.MULTIPART_FORM_DATA_VALUE

回答by Reaz Murshed

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.

我在从 Postman 发出 POST 请求时遇到了同样的问题,后来我可以通过设置一个自定义的 Content-Type 并像这样设置边界值来解决这个问题。

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

我认为人们可能会遇到类似的问题,因此,我正在分享我的解决方案。

postman

邮差

回答by Bala

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

取消选中 Postman 中的内容类型,postman 会根据您在运行时的输入自动检测内容类型。

Sample

样本

回答by gorjanz

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

这对我有用:通过 Postman 上传文件到 SpringMVC 后端 webapp:

Backend: Endpoint controller definition

后端: 端点控制器定义

Postman: Headers setupPOST Body setup

邮差: 标题设置POST Body setup

回答by hao huang

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.

当我使用邮递员将 5.6M 的文件发送到外部网络时,我遇到了同样的问题。同样的动作在我自己的电脑和本地测试环境上都成功了。

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 :)

在检查了所有服务器配置和 HTTP 标头后,我发现原因是 Postman 可能在模拟对外部 HTTP 请求的请求时遇到了一些麻烦。最后,我在chrome HTML页面上成功做了sendfile请求。仅供参考:)

回答by Hanz

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

我遇到了这个问题,因为我使用了基于 axios 编写的 request.js
而且我已经在 request.js 中设置了 defaults.headers

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直接发送请求,没有加配置

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

hope this can solve your problem

希望这可以解决您的问题

回答by Sandeep Kumar

Newer versions of ARC(Advaced Rest client) also provides file upload option:

较新版本的 ARC(Advanced Rest 客户端)还提供文件上传选项:

enter image description here

enter image description here