Java MultipartException:当前请求不是多部分请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42013087/
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
MultipartException: Current request is not a multipart request
提问by Salman Lashkarara
I am trying to make a restful controller to upload files. I have seen thisand 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 postman to send a pdf:
然后我用邮递员发送了一个pdf:
But the server crashes with the error:
但是服务器因错误而崩溃:
.MultipartException: Current request is not a multipart request
Again i have found this, and added a bean.xml
file
我再次找到了这个,并添加了一个bean.xml
文件
<?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, it still complains with the same error.
不幸的是,它仍然抱怨同样的错误。
采纳答案by abaghel
When you are using Postman for multipart request then don't specify a custom Content-Type in Header. So your Header tab in Postman should be empty. Postman will determine form-data boundary. In Body tab of Postman you should select form-data and select file type. You can find related discussion at https://github.com/postmanlabs/postman-app-support/issues/576
当您使用 Postman 进行多部分请求时,请不要在 Header 中指定自定义 Content-Type。所以你在 Postman 中的 Header 标签应该是空的。邮递员将确定表单数据边界。在 Postman 的 Body 选项卡中,您应该选择 form-data 并选择文件类型。您可以在https://github.com/postmanlabs/postman-app-support/issues/576找到相关讨论
回答by Javier Z.
It looks like the problem is request to server is not a multi-part request. Basically you need to modify your client-side form. For example:
看起来问题是对服务器的请求不是多部分请求。基本上你需要修改你的客户端表单。例如:
<form action="..." method="post" enctype="multipart/form-data">
<input type="file" name="file" />
</form>
Hope this helps.
希望这可以帮助。
回答by N.Luan Pham
In application.properties, please add this:
在 application.properties 中,请添加以下内容:
spring.servlet.multipart.max-file-size=128KB
spring.servlet.multipart.max-request-size=128KB
spring.http.multipart.enabled=false
and in your html form, you need an : enctype="multipart/form-data"
.
For example:
在您的 html 表单中,您需要一个 : enctype="multipart/form-data"
。例如:
<form method="POST" enctype="multipart/form-data" action="/">
Hope this help!
希望这有帮助!
回答by Nilesh Kumar
I was also facing the same issue with Postman
for multipart
. I fixed it by doing the following steps:
我也面临同样的问题Postman
for multipart
。我通过执行以下步骤修复了它:
- Do not select
Content-Type
in theHeaders
section. - In
Body
tab ofPostman
you should selectform-data
and selectfile type
.
- 不要
Content-Type
在该Headers
部分中选择。 - 在您应该选择的
Body
选项卡中选择。Postman
form-data
file type
It worked for me.
它对我有用。
回答by Pravin
in ARC (advanced rest client) - specify as below to make it work
Content-Type multipart/form-data
(this is header name and header value)
this allows you to add form data as key and values
you can specify you field name now as per your REST specification and select your file to upload from file selector.
在 ARC(高级休息客户端)中 - 指定如下以使其工作
Content-Type multipart/form-data
(这是标题名称和标题值)这允许您将表单数据添加为键和值,您现在可以根据您的 REST 规范指定您的字段名称并选择您的要从文件选择器上传的文件。