2013-07-04 28 views
20

我爲spring 3多部分文件上傳做了POC。它的工作正常。但是當我嘗試與我的應用程序集成時,我面臨着問題。SPRING REST:請求被拒絕,因爲沒有找到多部分邊界

它拋出以下異常:

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

回答

25

的問題是不是在你的代碼 - 它在你的請求。您在多部分請求中缺少邊界。由於它在specification說:

內容類型字段,多實體需要一個參數, 「邊界」,這是用來指定封裝邊界。 封裝邊界被定義爲完全由兩個 連字符(「 - 」,十進制碼45)組成的行,後跟來自Content-Type標題字段的邊界 參數值。

This and this的帖子也應該是有幫助的。

+0

嘿@sermolaev,你能給一個邊界值的例子嗎? –

0

您使用的是安全過濾器嗎? 我的問題已通過刪除安全篩選器鏈解決。 從這:

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

這樣:

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

我打開,我解釋一下細節問題: https://jira.spring.io/browse/SPR-12114

0

@sermolaev是正確的,他的回答。

我想分享我與這個問題有關的經驗。我在Postman中遇到過這個問題,但我很長時間無法理解它的根本原因。我的請求模板似乎是正確的,因爲郵差中包含boundary ...

最終我發現當你自己指定Content-Type=multipart/form標題時,它會覆蓋郵遞員自動添加的標題。這會導致與你一樣的錯誤。我的解決方案與刪除Content-Type標題一樣簡單。

相關問題