spring boot中上傳multipart/form-data文件只能是post提交,而不針對patch,這個問題花了作者26個小時才解決這個問題,最后不得不調試spring源代碼來解決這個問題。
需求:在網頁中構建一個表單,其中包含一個文本輸入字段和一個用于文件上載的輸入。很簡單。這是表單:
1
2
3
4
|
<form id=”data” method=”patch” action=”/f” > <input type= "text" required name= "company" > <input type= "file" required name= "definition" /> </form> |
restcontroller中的方法:
1
2
3
4
5
|
@requestmapping (value = "/f" ,method = patch) public void upload( @requestpart ( "definition" ) multipartfile definition, @requestpart ( "company" ) string company ) {...} |
注意它是patch的方法(根據要求)而不是post,部分要求是提交的ajax請求,并不是表單提交,代碼如下:
1
2
3
4
5
6
7
8
|
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解析器的實現進行更改,從org.springframework.web.multipart.support.standardservletmultipartresolver 改為org.springframework.web.multipart.commons.commonsmultipartresolver
1
2
3
4
5
6
7
8
|
@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ā)現這部分字段。
這是由于javascript的formdata問題,在formdata對象上調用的append方法接受兩個參數name和value(有第三個但不重要),該value字段可以是一個 USVString或Blob(包括子類等File)。更改代碼為:
1
2
3
4
5
6
7
8
9
10
11
|
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中有一行:
1
2
|
string subcontenttype = headers.getheader(content_type); if (subcontenttype != null ... ){} |
這是get的內容類型,如果它是null,則處理是通過不同的路由將我們的上傳部分不是轉為multipartfile,而是轉換為multipartparameter(放在不同的map中,而spring沒有找到它),然后spring為每個參數創(chuàng)建單獨的實例,形成在調用rest方法時實現綁定的表單。
requestpartservletserverhttprequest構造函數中可以找到拋出異常的位置:
1
2
3
4
|
httpheaders headers = this .multipartrequest.getmultipartheaders( this .partname); if (headers == null ) { throw new missingservletrequestpartexception(partname); } |
重要的是getmultipartheaders只查看multipart的文件files而不是參數parameters。
這就是為什么添加具有特定類型的blob解決了問題的原因:
1
2
3
|
var textinput = = new blob([ 'the info' ], { type: 'text/plain' }); |
現在回過來,前面我提到我必須切換到使用post才正常,但當我改為patch時,問題又回來了。錯誤是一樣的。
我很困惑。所以找到了源代碼(畢竟這是最終的文檔)。
請記住,在本文開頭切換到了commonsmultipartresolver。事實證明,在請求處理期間,調用此方法:
1
2
3
4
5
6
7
|
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)方法的方法解決了這個問題。
所以現在config bean看起來像這樣:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
@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" ); } } |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://www.jdon.com/51169#