【问题标题】:SPRING REST: The request was rejected because no multipart boundary was foundSPRING REST:请求被拒绝,因为没有找到多部分边界
【发布时间】:2013-07-02 00:11:22
【问题描述】:

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

【问题讨论】:

    标签: spring rest spring-mvc file-upload


    【解决方案1】:

    问题不在于您的代码,而在于您的请求。您在多部分请求中缺少边界。正如specification 所说:

    多部分实体的 Content-Type 字段需要一个参数, “boundary”,用于指定封装边界。这 封装边界被定义为一条完全由两个组成的线 连字符(“-”,十进制代码 45)后跟边界 Content-Type 标头字段中的参数值。

    This this的帖子也应该有帮助。

    【讨论】:

    • 嘿@sermolaev,你能举一个边界值的例子吗?
    【解决方案2】:

    @sermolaev 的回答是正确的。

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

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

    【讨论】:

    • 我浪费了 1 天的时间在网上搜索,才发现答案很简单,只需从 Retrofit 请求的 Header 中删除 Content-Type: multipart/form-data
    【解决方案3】:

    您是否使用任何安全过滤器? 我的问题是通过删除安全过滤器链解决的。 由此:

    this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).addFilters(this.springSecurityFilterChain).build();
    

    到这里:

    this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
    

    我打开了一个问题,我解释了详细信息: https://jira.spring.io/browse/SPR-12114

    【讨论】:

      【解决方案4】:

      不要在请求中提供Content-Type 标头。它会起作用的。

      【讨论】:

      • Bhai sahi bola tha
      猜你喜欢
      • 2019-03-18
      • 1970-01-01
      • 1970-01-01
      • 2020-08-27
      • 2020-11-21
      • 1970-01-01
      • 2015-09-14
      • 2013-05-10
      • 2018-01-26
      相关资源
      最近更新 更多