我为Spring 3 Rest Multipart文件上载了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:
@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"; } }
问题不在你的代码中,而是在你的请求中。你的多部分请求中缺少边界。正如规范中所说:
多部分实体的Content-Type字段需要一个参数“边界”,该参数用于指定封装边界。封装边界定义为一行,该行完全由两个连字符(“-”,十进制代码45)组成,后跟来自Content-Type标头字段的边界参数值。