Spring 休息 : The request was rejected because no multipart boundary was found

标签 spring rest spring-mvc file-upload

我为 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**"

如果我的代码有任何错误,请告诉我。

bean :

<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";
    }
}

最佳答案

问题不在于您的代码,而在于您的请求。您在多部分请求中缺少边界。正如 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.

This this帖子也应该有帮助。

关于 Spring 休息 : The request was rejected because no multipart boundary was found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17462642/

相关文章:

java - Spring Boot和Spring Cloud配置客户端错误

rest - Swagger - 调用返回文件的服务无法正常工作

wcf - 使用 WCF REST 服务对 Windows 帐户以外的其他内容进行基本身份验证?

Java : How to accommodate special characters in the filename while uploading and downloading the file?

spring - 启动 resourceHandlerMapping bean 时未设置 ServletContext

java - 内存泄漏 - com.mysql.jdbc.ConnectionPropertiesImpl$*ANY*ConnectionProperty

java - 长时间运行的 HTTP 请求和线程同步

spring - 使用 Feign 将数据文件上传为字节数组

Java Rest 基本身份验证

java - 使用注释的构造函数注入(inject)