我为 Spring 3 Rest 多部分文件上传做了一个 POC。它工作正常。但是当我尝试与我的应用程序集成时,我遇到了问题。
它抛出以下异常:
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**"
如果我的代码的任何部分有错误,请告诉我。
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
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";
}
}
问题不在于您的代码,而在于您的请求。您的多部分请求中缺少边界。正如它所说规格 http://www.w3.org/Protocols/rfc1341/7_2_Multipart.html:
多部分实体的 Content-Type 字段需要一个参数,
“boundary”,用于指定封装边界。这
封装边界定义为完全由两个组成的线
连字符(“-”,十进制代码 45)后跟边界
Content-Type 标头字段中的参数值。
This https://stackoverflow.com/questions/9081079/rest-http-post-multipart-with-jsonand this https://stackoverflow.com/questions/9344455/unable-to-send-a-multipart-mixed-request-to-spring-mvc-based-rest-service帖子也应该有帮助。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)