溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

詳解Spring Boot中PATCH上傳文件的問題

發(fā)布時間:2020-08-22 21:09:13 來源:腳本之家 閱讀:230 作者:banq 欄目:編程語言

Spring Boot中上傳multipart/form-data文件只能是Post提交,而不針對PATCH,這個問題花了作者26個小時才解決這個問題,最后不得不調(diào)試Spring源代碼來解決這個問題。

需求:在網(wǎng)頁中構(gòu)建一個表單,其中包含一個文本輸入字段和一個用于文件上載的輸入。很簡單。這是表單:

<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ā)揮作用。總是遇到以下異常:

MissingServletRequestPartException: Required request part ‘definition' is not present

我做的第一件事就是將這個問題分解為最簡單的問題。所以我將請求類型更改為POST,并刪除了textInput。將MultiPart解析器的實現(xiàn)進(jì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庫添加到類路徑中。

但每當(dāng)我添加一個字符串變量返回錯誤:the string field not the file field

這說明multi part request resolver 沒有發(fā)現(xiàn)這部分字段。

這是由于Javascript的FormData問題,在FormData對象上調(diào)用的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ā)送的內(nèi)容:

— — — 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 —

你能注意到內(nèi)容處置標(biāo)題中缺少的內(nèi)容嗎?文件名和內(nèi)容類型。在servlet處理期間,multi-part表單變成MultipartFile。在commons-fileupload中有一行:

String subContentType = headers.getHeader(CONTENT_TYPE);
if (subContentType != null ... ){}

這是get的內(nèi)容類型,如果它是null,則處理是通過不同的路由將我們的上傳部分不是轉(zhuǎn)為MultipartFile,而是轉(zhuǎn)換為MultipartParameter(放在不同的Map中,而spring沒有找到它),然后spring為每個參數(shù)創(chuàng)建單獨的實例,形成在調(diào)用rest方法時實現(xiàn)綁定的表單。

RequestPartServletServerHttpRequest構(gòu)造函數(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才正常,但當(dāng)我改為PATCH時,問題又回來了。錯誤是一樣的。

我很困惑。所以找到了源代碼(畢竟這是最終的文檔)。

請記住,在本文開頭切換到了CommonsMultipartResolver。事實證明,在請求處理期間,調(diào)用此方法:

public static final boolean isMultipartContent(
    HttpServletRequest request) {
  if (!POST_METHOD.equalsIgnoreCase(request.getMethod())) {
    return false;
  }
  return FileUploadBase.isMultipartContent(new ServletRequestContext(request));
}

如果它不是POST請求,則立即確定該請求沒有multipart內(nèi)容。

那么久通過覆蓋調(diào)用上面靜態(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");
  }

}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI