詳解Spring Boot中PATCH上傳文件的問題
Spring Boot中上傳multipart/form-data文件只能是Post提交,而不針對PATCH,這個問題花了作者26個小時才解決這個問題,最后不得不調試Spring源代碼來解決這個問題。
需求:在網頁中構建一個表單,其中包含一個文本輸入字段和一個用于文件上載的輸入。很簡單。這是表單:
<form id=”data” method=”PATCH” action=”/f” > <input type="text" required name="company" > <input type="file" required name="definition" /> </form>
RestController中的方法:
@RequestMapping(value = "/f",method = PATCH)
public void upload(
@RequestPart("definition") MultipartFile definition,
@RequestPart("company") String company
) {...}
注意它是PATCH的方法(根據(jù)要求)而不是POST,部分要求是提交的ajax請求,并不是表單提交,代碼如下:
var fileInput = ...; //this is html element that holds the files
var textInput = ...; //thi is the string
var fd = new FormData();
fd.append('definition',fileInput.files[0]);
fd.append('name', textInput );
xhr = new XMLHttpRequest();
xhr.open( 'PATCH', uploadForm.action, true );
xhr.send( fd );
但無論怎么做,我都無法讓它發(fā)揮作用??偸怯龅揭韵庐惓#?/p>
MissingServletRequestPartException: Required request part ‘definition' is not present
我做的第一件事就是將這個問題分解為最簡單的問題。所以我將請求類型更改為POST,并刪除了textInput。將MultiPart解析器的實現(xiàn)進行更改,從org.springframework.web.multipart.support.StandardServletMultipartResolver 改為org.springframework.web.multipart.commons.CommonsMultipartResolver
@Configuration
public class MyConfig {
@Bean
public MultipartResolver multipartResolver() {
return new CommonsMultipartResolver();
}
}
這還需要將commons-fileupload庫添加到類路徑中。
但每當我添加一個字符串變量返回錯誤:the string field not the file field
這說明multi part request resolver 沒有發(fā)現(xiàn)這部分字段。
這是由于Javascript的FormData問題,在FormData對象上調用的Append方法接受兩個參數(shù)name和value(有第三個但不重要),該value字段可以是一個 USVString或Blob(包括子類等File)。更改代碼為:
var fileInput = ...; //this is html element that holds the files
var textInput = = new Blob(['the info'], {
type: 'text/plain'
});
; //thi is the string
var fd = new FormData();
fd.append('definition',fileInput.files[0]);
fd.append('name', textInput );
xhr = new XMLHttpRequest();
xhr.open( 'PATCH', uploadForm.action, true );
xhr.send( fd );
它突然開始工作:)。
看一下瀏覽器發(fā)送的內容:
— — — WebKitFormBoundaryHGN3YjdgsELbgmZH
Content-Disposition: form-data; name=”definition”; filename=”test.csv” Content-Type: text/csv
this is the content of a file, browser hides it.
— — — WebKitFormBoundaryHGN3YjdgsELbgmZH Content-Disposition: form-data; name=”name”
this is the string
— — — WebKitFormBoundaryHGN3YjdgsELbgmZH —
你能注意到內容處置標題中缺少的內容嗎?文件名和內容類型。在servlet處理期間,multi-part表單變成MultipartFile。在commons-fileupload中有一行:
String subContentType = headers.getHeader(CONTENT_TYPE);
if (subContentType != null ... ){}
這是get的內容類型,如果它是null,則處理是通過不同的路由將我們的上傳部分不是轉為MultipartFile,而是轉換為MultipartParameter(放在不同的Map中,而spring沒有找到它),然后spring為每個參數(shù)創(chuàng)建單獨的實例,形成在調用rest方法時實現(xiàn)綁定的表單。
RequestPartServletServerHttpRequest構造函數(shù)中可以找到拋出異常的位置:
HttpHeaders headers = this.multipartRequest.getMultipartHeaders(this.partName);
if (headers == null) {
throw new MissingServletRequestPartException(partName);
}
重要的是getMultipartHeaders只查看multipart的文件files而不是參數(shù)parameters。
這就是為什么添加具有特定類型的blob解決了問題的原因:
var textInput = = new Blob(['the info'], {
type: 'text/plain'
});
現(xiàn)在回過來,前面我提到我必須切換到使用POST才正常,但當我改為PATCH時,問題又回來了。錯誤是一樣的。
我很困惑。所以找到了源代碼(畢竟這是最終的文檔)。
請記住,在本文開頭切換到了CommonsMultipartResolver。事實證明,在請求處理期間,調用此方法:
public static final boolean isMultipartContent(
HttpServletRequest request) {
if (!POST_METHOD.equalsIgnoreCase(request.getMethod())) {
return false;
}
return FileUploadBase.isMultipartContent(new ServletRequestContext(request));
}
如果它不是POST請求,則立即確定該請求沒有multipart內容。
那么久通過覆蓋調用上面靜態(tài)方法的方法解決了這個問題。
所以現(xiàn)在config bean看起來像這樣:
@Bean
public MultipartResolver multipartResolver() {
return new CommonsMultipartResolverMine();
}
public static class CommonsMultipartResolverMine extends CommonsMultipartResolver {
@Override
public boolean isMultipart(HttpServletRequest request) {
final String header = request.getHeader("Content-Type");
if(header == null){
return false;
}
return header.contains("multipart/form-data");
}
}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
springboot?實現(xiàn)不同context-path下的會話共享
這篇文章主要介紹了springboot?實現(xiàn)不同context-path下的會話共享,基于很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01

