預設表單編碼 application/x-www-form-urlencoded 僅支援鍵值對字串,**無法傳輸二進位檔案內容**。multipart/form-data 將表單拆分為多個「部分」,每部分獨立擁有 Header (Content-Disposition, Content-Type) 與 Body,使伺服器能正確解析檔案二進位流。
boundary=----WebKitFormBoundaryxxx,伺服器依此分割各欄位。name="username") 與檔案欄位 (name="avatar"; filename="a.png")。<form action="/upload" method="post" enctype="multipart/form-data">
<input type="text" name="username" />
<input type="file" name="avatar" />
<button type="submit">上傳</button>
</form>
@RequestParam("avatar") MultipartFile file + MultipartResolver Bean。request.getPart("avatar") 需在 web.xml 或 @MultipartConfig 設定 maxFileSize、location (暫存目錄)。multer、busboy 中介軟體解析 Stream。enctype → 後端收到檔案欄位為空或檔案名字串。FormData 手動組裝時,不要 手動設定 Content-Type,瀏覽器會自動帶入 boundary。