13

因爲我正在嘗試使用Spring引導和Web服務與郵遞員Chrome附加組件。該請求被拒絕,因爲沒有在springboot中找到多部分邊界

在郵遞員content-type="multipart/form-data"和我得到以下例外。

HTTP Status 500 - Request processing failed; 
nested exception is org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request; 
nested exception is java.io.IOException: 
org.apache.tomcat.util.http.fileupload.FileUploadException: the request was rejected because no multipart boundary was found 

在控制器I指定下面的代碼

@ResponseBody 
@RequestMapping(value = "/file", headers = "Content-Type= multipart/form-data", method = RequestMethod.POST) 

public String upload(@RequestParam("name") String name, 
     @RequestParam(value = "file", required = true) MultipartFile file) 
//@RequestParam()CommonsMultipartFile[] fileUpload 
{ 
    // @RequestMapping(value="/newDocument", , method = RequestMethod.POST) 
    if (!file.isEmpty()) { 
     try { 
      byte[] fileContent = file.getBytes(); 
      fileSystemHandler.create(123, fileContent, name); 
      return "You successfully uploaded " + name + "!"; 
     } catch (Exception e) { 
      return "You failed to upload " + name + " => " + e.getMessage(); 
     } 
    } else { 
     return "You failed to upload " + name + " because the file was empty."; 
    } 
} 

在這裏,我在您的要求與指定文件處理程序代碼

public String create(int jonId, byte[] fileContent, String name) { 
    String status = "Created file..."; 
    try { 
     String path = env.getProperty("file.uploadPath") + name; 
     File newFile = new File(path); 
     newFile.createNewFile(); 
     BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(newFile)); 
     stream.write(fileContent); 
     stream.close(); 
    } catch (IOException ex) { 
     status = "Failed to create file..."; 
     Logger.getLogger(FileSystemHandler.class.getName()).log(Level.SEVERE, null, ex); 
    } 
    return status; 
} 
+0

我也面臨同樣的問題,而且它在郵遞員工作中的唯一工作就是不工作ith其他工具,如「高級休息客戶端」。我可以知道爲什麼嗎? – Narendhran

回答

0

問題是不是在你的代碼.problem。你在多部分請求中沒有得到任何有限的邊界。

+1

如何獲取? –

+0

其服務器錯誤 500是用於服務器 沒有boundry集時,服務器接收圖像 –

41

問題是你自己設置Content-Type,讓它變成空白。谷歌瀏覽器會爲你做。多部分內容類型需要知道文件邊界,並且當您刪除內容類型時,郵差會自動爲您執行此操作。

+2

謝謝。這在Postman中適用於我。此外,來自tomeokin的回答有助於說明郵差不適合所有的測試場景。 –

+0

謝謝解決我的問題 – erhun

+0

謝謝解決我的問題了... –

0

我有同樣的問題,同時使從郵差POST請求,後來我可以通過設置自定義的內容類型與隨之像這樣設定的邊界值解決問題。

我認爲人們會遇到類似的問題,因此,我分享我的解決方案。

postman

5

中未經檢查郵差內容類型和郵遞員自動檢測基於在運行時你輸入的內容類型。

Sample

+0

爲我工作,謝謝! – mangotang

1

這爲我工作: 上傳通過郵差文件,到用SpringMVC後端web應用:

後端: Endpoint controller definition

郵差: Headers setup POST Body setup

相關問題