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
The request was rejected because no multipart boundary was found in springboot
提问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-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.
问题是你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.
我认为人们可能会遇到类似的问题,因此,我正在分享我的解决方案。
回答by Bala
回答by gorjanz
回答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
希望这可以解决您的问题