[英]Spring Boot + Angular - MultipartException: Current request is not a multipart request
[英]MultipartException: Current request is not a multipart request
我正在嘗試制作一個寧靜的 controller 來上傳文件。 我已經看到這個並做了這個 controller:
@RestController
public class MaterialController {
@RequestMapping(value="/upload", method= RequestMethod.POST)
public String handleFileUpload(
@RequestParam("file") MultipartFile file){
String name = "test11";
if (!file.isEmpty()) {
try {
byte[] bytes = file.getBytes();
BufferedOutputStream stream =
new BufferedOutputStream(new FileOutputStream(new File(name + "-uploaded")));
stream.write(bytes);
stream.close();
return "You successfully uploaded " + name + " into " + name + "-uploaded !";
} catch (Exception e) {
return "You failed to upload " + name + " => " + e.getMessage();
}
} else {
return "You failed to upload " + name + " because the file was empty.";
}
}
}
然后我使用 postman 發送 pdf:
但是服務器因錯誤而崩潰:
.MultipartException: Current request is not a multipart request
我再次找到了這個,並添加了一個bean.xml
文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
</bean>
</beans>
不幸的是,它仍然抱怨同樣的錯誤。
當您使用 Postman 進行多部分請求時,請不要在 Header 中指定自定義 Content-Type。 所以你在 Postman 中的 Header 標簽應該是空的。 郵遞員將確定表單數據邊界。 在 Postman 的 Body 選項卡中,您應該選擇 form-data 並選擇文件類型。 您可以在https://github.com/postmanlabs/postman-app-support/issues/576找到相關討論
看起來問題是對服務器的請求不是多部分請求。 基本上你需要修改你的客戶端表單。 例如:
<form action="..." method="post" enctype="multipart/form-data">
<input type="file" name="file" />
</form>
希望這可以幫助。
我也面臨與Postman
相同的multipart
。 我通過執行以下步驟修復了它:
Headers
部分選擇Content-Type
。Postman
Body
選項卡中,您應該選擇form-data
並選擇file type
。它對我有用。
在 application.properties 中,請添加以下內容:
spring.servlet.multipart.max-file-size=128KB
spring.servlet.multipart.max-request-size=128KB
spring.http.multipart.enabled=false
在您的 html 表單中,您需要一個 : enctype="multipart/form-data"
。 例如:
<form method="POST" enctype="multipart/form-data" action="/">
希望這有幫助!
檢查您在請求中選擇的文件。
對我來說,我收到錯誤是因為系統中不存在該文件,因為我從其他機器導入了請求。
就我而言,我刪除了:
'內容類型':'應用程序/json',
從我的Interceptor ,一切正常。
intercept(httpRequest: HttpRequest<any>, httpHandler: HttpHandler): Observable<HttpEvent<any>> {
if (this.authService.isUserLoggedIn() && httpRequest.url.indexOf('login') === -1) {
const authReq = httpRequest.clone({
headers: new HttpHeaders({
'Content-Type': 'application/json',
Authorization: this.authService.getBasicAuth()
})
});
return httpHandler.handle(authReq);
} else {
return httpHandler.handle(httpRequest);
}}
在 ARC(高級休息客戶端)中 - 指定如下以使其工作Content-Type multipart/form-data
(這是標題名稱和標題值)這允許您將表單數據添加為鍵和值,您現在可以指定您的字段名稱根據您的 REST 規范並從文件選擇器中選擇要上傳的文件。
這發生在我身上一次:我有一個完美的 Postman 配置,但是沒有改變任何東西,即使我沒有在 Postman 上手動通知Content-Type
,它也停止了工作; 按照這個問題的答案,我嘗試禁用標題並讓郵遞員自動添加它,但兩個選項都不起作用。
我最終通過轉到Body
選項卡來解決它,將參數類型從File
更改為Text
,然后返回File
,然后重新選擇要發送的文件; 不知何故,這使它再次起作用。 聞起來像郵遞員的蟲子,在那個特定的情況下,也許?
我遇到了拼寫錯誤的 enctype="multipart/form-data" 的同樣問題,我通過正確拼寫來解決這個異常。 當前請求不是多部分請求客戶端錯誤,因此請檢查您的表單。
您需要將消耗 = {MULTIPART_FORM_DATA_VALUE}添加到您的映射中。 完整示例:
@PostMapping(path = "/{idDocument}/attachments", consumes = {MULTIPART_FORM_DATA_VALUE})
ResponseEntity<String> addAttachmentsToDocumentForm(@PathVariable Long idDocument, @RequestParam("file") MultipartFile[] files){
documentService.addAttachments(idDocument, files);
return ok("your response");
}
我遇到了這個問題,但這是因為我在郵遞員中上傳的文件不在郵遞員工作目錄中轉到設置-> 常規並向下滾動到位置以找到它的位置文件夾並將您的文件上傳到那里。
請參閱:設置 Potman 工作目錄
@PostMapping("/upload")
public ResponseEntity<ResponseMessage> uploadFile(@RequestParam("file") MultipartFile uploadFile) {
String message = "";
try {
service.save(uploadFile);
message = "Uploaded the file successfully: " + uploadFile.getOriginalFilename();
return ResponseEntity.status(HttpStatus.OK).body(new ResponseMessage(message));
} catch (Exception e) {
message = "Could not upload the file: " + uploadFile.getOriginalFilename() + "!";
return ResponseEntity.status(HttpStatus.EXPECTATION_FAILED).body(new ResponseMessage(message));
}
}
添加 required=false 參數 @RequestParam(required = false,value = "file") MultipartFile 文件
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.