I did a POC for spring 3 rest multipart file upload. Its working fine. But when i tried integrating with my application i am facing issues.
我为春季3休息多部分文件上传做了一个POC。它的工作正常。但当我尝试与我的应用程序集成时,我面临着问题。
It throws following exception:
它抛出以下异常:
org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request;
nested exception is org.apache.commons.fileupload.FileUploadException:
the request was rejected because no multipart boundary was found**"
Please let me know if I am wrong in any part of my code.
如果我的代码的任何部分出错,请告诉我。
Beans:
豆:
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="order" value="1" />
<property name="mediaTypes">
<map>
<entry key="json" value="application/json" />
<entry key="xml" value="application/xml" />
<entry key="file" value="multipart/mixed" />
</map>
</property>
</bean>
<!-- multipart resolver -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- one of the properties available; the maximum file size in bytes -->
<property name="maxUploadSize" value="50000000" />
</bean>
Controller:
控制器:
@Controller
public class MultipleFilesRecieve {
@RequestMapping ( value = "/saveMultiple", method = RequestMethod.POST )
public String save( FileUploadForm uploadForm ) {
List<MultipartFile> files = uploadForm.getFiles( );
List<String> fileNames = new ArrayList<String>( );
if ( null != files && files.size( ) > 0 ) {
for ( MultipartFile multipartFile : files ) {
String fileName = multipartFile.getOriginalFilename( );
fileNames.add( fileName );
}
}
return "multifileSuccess";
}
}
4 个解决方案
#1
29
The problem isn't in your code - it's in your request. You're missing boundary in your multipart request. As it said in specification:
问题不在您的代码中 - 它在您的请求中。您在多部分请求中缺少边界。如规范中所述:
The Content-Type field for multipart entities requires one parameter, "boundary", which is used to specify the encapsulation boundary. The encapsulation boundary is defined as a line consisting entirely of two hyphen characters ("-", decimal code 45) followed by the boundary parameter value from the Content-Type header field.
多部分实体的Content-Type字段需要一个参数“boundary”,用于指定封装边界。封装边界定义为一条完全由两个连字符(“ - ”,十进制代码45)组成的行,后跟来自Content-Type头字段的边界参数值。
This and this posts should also be helpful.
这个和这个帖子也应该有所帮助。
#2
1
@sermolaev is right in his answer.
@sermolaev的答案是正确的。
I want to share my experience related to this problem. I've encountered this problem in Postman, but I could not understand the root cause for it for a long time. My request template seemed to be correct cause Postman included boundary
in it...
我想分享一下与此问题相关的经验。我在邮递员中遇到过这个问题,但很长一段时间我无法理解它的根本原因。我的请求模板似乎是正确的,因为Postman包含了边界...
Eventually I've discovered that when you're specifying Content-Type=multipart/form
header by yourself, it overrides the one added automatically by Postman. And this leads to the same error as yours. My solution was as simple as removing Content-Type
header.
最后我发现当你自己指定Content-Type = multipart / form标题时,它会覆盖Postman自动添加的标题。这会导致与您相同的错误。我的解决方案就像删除Content-Type标头一样简单。
#3
1
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,让它为空。谷歌浏览器将为您完成。 multipart Content-Type需要知道文件边界,当你删除Content-Type时,Postman会自动为你做。
#4
0
Are you using any security filters? My problem was solved by removing the Security Filter Chain. From this:
您使用的是安全过滤器吗?通过删除安全过滤器链解决了我的问题。由此:
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).addFilters(this.springSecurityFilterChain).build();
to this:
对此:
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
I opened an issue where I explain the details: https://jira.spring.io/browse/SPR-12114
我打开了一个问题,我解释了细节:https://jira.spring.io/browse/SPR-12114
#1
29
The problem isn't in your code - it's in your request. You're missing boundary in your multipart request. As it said in specification:
问题不在您的代码中 - 它在您的请求中。您在多部分请求中缺少边界。如规范中所述:
The Content-Type field for multipart entities requires one parameter, "boundary", which is used to specify the encapsulation boundary. The encapsulation boundary is defined as a line consisting entirely of two hyphen characters ("-", decimal code 45) followed by the boundary parameter value from the Content-Type header field.
多部分实体的Content-Type字段需要一个参数“boundary”,用于指定封装边界。封装边界定义为一条完全由两个连字符(“ - ”,十进制代码45)组成的行,后跟来自Content-Type头字段的边界参数值。
This and this posts should also be helpful.
这个和这个帖子也应该有所帮助。
#2
1
@sermolaev is right in his answer.
@sermolaev的答案是正确的。
I want to share my experience related to this problem. I've encountered this problem in Postman, but I could not understand the root cause for it for a long time. My request template seemed to be correct cause Postman included boundary
in it...
我想分享一下与此问题相关的经验。我在邮递员中遇到过这个问题,但很长一段时间我无法理解它的根本原因。我的请求模板似乎是正确的,因为Postman包含了边界...
Eventually I've discovered that when you're specifying Content-Type=multipart/form
header by yourself, it overrides the one added automatically by Postman. And this leads to the same error as yours. My solution was as simple as removing Content-Type
header.
最后我发现当你自己指定Content-Type = multipart / form标题时,它会覆盖Postman自动添加的标题。这会导致与您相同的错误。我的解决方案就像删除Content-Type标头一样简单。
#3
1
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,让它为空。谷歌浏览器将为您完成。 multipart Content-Type需要知道文件边界,当你删除Content-Type时,Postman会自动为你做。
#4
0
Are you using any security filters? My problem was solved by removing the Security Filter Chain. From this:
您使用的是安全过滤器吗?通过删除安全过滤器链解决了我的问题。由此:
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).addFilters(this.springSecurityFilterChain).build();
to this:
对此:
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
I opened an issue where I explain the details: https://jira.spring.io/browse/SPR-12114
我打开了一个问题,我解释了细节:https://jira.spring.io/browse/SPR-12114