在工作上做API串接測試,遇到問題,特此紀錄~
首先是postman的設定:
要注意的點有
1.在Headers設定:
- Content-Type:multipart/form-data; boundary=<calculated when request is sent>
- Content-Length:<calculated when request is sent>

2.在Body設定:
- form-data模式
- 勾選傳入參數,檔案形式選File,輸入Key,Value處可上傳本地檔案

再來是程式方面的:
1.由於Content-Type:multipart/form-data,有別於enctype : application/x-www-form-urlencoded。
先來了解什麼是enctype : application/x-www-form-urlencoded
這應該是最常見的 POST 提交數據的方式了。瀏覽器的原生 form 表單,如果不設置 enctype 屬性,那麽最終就會以 application/x-www-form-urlencoded 方式提交數據
再回來看multipart/form-data
這又是一個常見的 POST 數據提交的方式。我們使用表單上傳文件時,必須讓 form 的 enctyped 等於這個值。
2.再來是上傳文件會用到MultipartFile這個類別,若要透過request接到MultipartFile類別的檔案,就一定要搭配@RequestPart這個annotation

或是若想繼續使用HttpServletRequest api,也可透過getFile()來接值。

如有敘述錯誤,還請不吝嗇留言指教,thanks!