SPRING REST:请求被拒绝,因为没有找到多部分边界

Che*_*rry 28 rest spring file-upload spring-mvc

我为春季3休息多部分文件上传做了一个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**"
Run Code Online (Sandbox Code Playgroud)

如果我的代码的任何部分出错,请告诉我.

豆子:

<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>
Run Code Online (Sandbox Code Playgroud)

控制器:

@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";
    }
}
Run Code Online (Sandbox Code Playgroud)

ser*_*aev 32

问题不在您的代码中 - 它在您的请求中.您在多部分请求中缺少边界.如规范中所述:

多部分实体的Content-Type字段需要一个参数"boundary",用于指定封装边界.封装边界定义为一个完全由两个连字符(" - ",十进制代码45)组成的行,后跟来自Content-Type头字段的边界参数值.

这个这个帖子也应该有所帮助.

  • 嘿@sermolaev,您能举一个边界值示例吗? (2认同)

naX*_*aXa 5

@sermolaev 的回答是对的。

我想分享我与这个问题相关的经验。我在 Postman 中遇到过这个问题,但是我很长时间都无法理解它的根本原因。我的请求模板似乎是正确的,因为其中包含邮递员boundary...

最终我发现当您Content-Type=multipart/form自己指定标题时,它会覆盖邮递员自动添加的标题。这会导致与您相同的错误。我的解决方案就像删除Content-Type标题一样简单。