- 原理講解
-
簡單的HTTP POST 大家通過HTTP向服務器發送POST請求提交數據,都是通過form表單提交的,代碼如下: <form method="post"action="http://w.sohu.com" > <inputtype="text" name="txt1"> <inputtype="text" name="txt2"> </form> 提交時會向服務器端發出這樣的數據(已經去除部分不相關的頭信息),數據如下: POST / HTTP/1.1 Content-Type:application/x-www-form-urlencoded Accept-Encoding: gzip, deflate Host: w.sohu.com Content-Length: 21 Connection: Keep-Alive Cache-Control: no-cache txt1=hello&txt2=world 對於普通的HTML Form POST請求,它會在頭信息里使用Content-Length注明內容長度。頭信息每行一條,空行之后便是Body,即“內容”(entity)。它的Content-Type是application/x-www-form-urlencoded,這意味着消息內容會經過URL編碼,就像在GET請 求時URL里的QueryString那樣。txt1=hello&txt2=world POST上傳文件 最早的HTTP POST是不支持文件上傳的,給編程開發帶來很多問題。但是在1995年,ietf出台了rfc1867,也就是《RFC 1867 -Form-based File Upload in HTML》,用以支持文件上傳。所以Content-Type的類型擴充了multipart/form-data用以支持向服務器發送二進制數據。因此發送post請求時候,表單<form>屬性enctype共有二個值可選,這個屬性管理的是表單的MIME編碼: ①application/x-www-form-urlencoded(默認值) ②multipart/form-data 其實form表單在你不寫enctype屬性時,也默認為其添加了enctype屬性值,默認值是enctype="application/x- www-form-urlencoded". 通過form表單提交文件操作如下: <form method="post"action="http://w.sohu.com/t2/upload.do" enctype=”multipart/form-data”> <inputtype="text" name="desc"> <inputtype="file" name="pic"> </form> 瀏覽器將會發送以下數據: POST /t2/upload.do HTTP/1.1 User-Agent: SOHUWapRebot Accept-Language: zh-cn,zh;q=0.5 Accept-Charset: GBK,utf-8;q=0.7,*;q=0.7 Connection: keep-alive Content-Length: 60408 Content-Type:multipart/form-data; boundary=ZnGpDtePMx0KrHh_G0X99Yef9r8JZsRJSXC Host: w.sohu.com --ZnGpDtePMx0KrHh_G0X99Yef9r8JZsRJSXC Content-Disposition: form-data;name="desc" Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [......][......][......][......]........................... --ZnGpDtePMx0KrHh_G0X99Yef9r8JZsRJSXC Content-Disposition: form-data;name="pic"; filename="photo.jpg" Content-Type: application/octet-stream Content-Transfer-Encoding: binary [圖片二進制數據] --ZnGpDtePMx0KrHh_G0X99Yef9r8JZsRJSXC-- 我們來分析下數據,第一個空行之前自然還是HTTP header,之后則是Entity,而此時的Entity也比之前要復雜一些。根據RFC 1867定義,我們需要選擇一段數據作為“分割邊界”( boundary屬性),這個“邊界數據”不能在內容其他地方出現,一般來說使用一段從概率上說“幾乎不可能”的數據即可。 不同瀏覽器的實現不同,例如火狐某次post的 boundary=---------------------------32404670520626 , opera為boundary=----------E4SgDZXhJMgNE8jpwNdOAX ,每次post瀏覽器都會生成一個隨機的30-40位長度的隨機字符串,瀏覽器一般不會遍歷這次post的所有數據找到一個不可能出現在數據中的字符串,這樣代價太大了。一般都是隨機生成,如果你遇見boundary值和post的內容一樣,那樣的話這次上傳肯定失敗,不過我建議你去買彩票,你太幸運了。Rfc1867這樣說明{A boundary is selected that does not occur in any of the data. (This selection is sometimes done probabilisticly.)}。 選擇了這個邊界之后,瀏覽器便把它放在Content-Type 里面傳遞給服務器,服務器根據此邊界解析數據。下面的數據便根據boundary划分段,每一段便是一項數據。(每個field被分成小部分,而且包含一個value是"form-data"的"Content-Disposition"的頭部;一個"name"屬性對應field的ID,等等,文件的話包括一個filename) •IE和Chrome在filename的選擇策略上有所不同,前者是文件的完整路徑,而后者則僅僅是文件名。 •數據內容以兩條橫線結尾,並同樣以一個換行結束。在網絡協議中一般都以連續的CR、LF(即\r、\n,或0x0D、Ox0A)字符作為換行,這與Windows的標准一致。如果您使用其他操作系統,則需要考慮它們的換行符。
-
- 后台處理
- 使用multipart/form-data提交的數據使用HttpServletRequest對象的getParameter()等方法無法讀取。可以讀取整個請求體數據流自己解析數據。但更好的方式是使用已經有的第三方工具類,如fileupload,jspsmartupload。下面的例子中使用的是fileupload。
-
HttpServletRequeest request=.... if(ServletFileUpload.isMultipartContent(request)) { FileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); List<FileItem> items = upload.parseRequest(request); for(FileItem i: items) { i.getFieldName(); //參數名 //i.getString(); //參數值(返回字符串),如果是上傳文件,則為文件內容 //i.get(); //參數值(返回字節數組),如果是上傳文件,則為文件內容 //i.getSize(); //參數值的字節大小 //i.getName(); //上傳文件的文件名 //i.getContentType(); //上傳文件的內容類型 if(!i.isFormField()&&i.getSize()>0) //簡單參數返回true,文件返回false Files.write(Paths.get("/upload/"+Paths.get(i.getName()).getFileName()), i.get()); } }
- SpringMVC處理Multipart方式
-
MultipartResolver 用於處理文件上傳,當收到請求時 DispatcherServlet 的 checkMultipart() 方法會調用 MultipartResolver 的 isMultipart() 方法判斷請求中是否包含文件。如果請求數據中包含文件,則調用 MultipartResolver 的 resolveMultipart() 方法對請求的數據進行解析,然后將文件數據解析成 MultipartFile 並封裝在 MultipartHttpServletRequest (繼承了 HttpServletRequest) 對象中,最后傳遞給 Controller,在 MultipartResolver 接口中有如下方法:
- boolean isMultipart(HttpServletRequest request); // 是否是 multipart
- MultipartHttpServletRequest resolveMultipart(HttpServletRequest request); // 解析請求
- void cleanupMultipart(MultipartHttpServletRequest request);
MultipartFile 封裝了請求數據中的文件,此時這個文件存儲在內存中或臨時的磁盤文件中,需要將其轉存到一個合適的位置,因為請求結束后臨時存儲將被清空。在 MultipartFile 接口中有如下方法:
- String getName(); // 獲取參數的名稱
- String getOriginalFilename(); // 獲取文件的原名稱
- String getContentType(); // 文件內容的類型
- boolean isEmpty(); // 文件是否為空
- long getSize(); // 文件大小
- byte[] getBytes(); // 將文件內容以字節數組的形式返回
- InputStream getInputStream(); // 將文件內容以輸入流的形式返回
- void transferTo(File dest); // 將文件內容傳輸到指定文件中
- MultipartResolver 是一個接口,它的實現類如下圖所示,分為 CommonsMultipartResolver 類和 StandardServletMultipartResolver 類。
- 其中 CommonsMultipartResolver 使用 commons Fileupload 來處理 multipart 請求,所以在使用時,必須要引入相應的 jar 包;而 StandardServletMultipartResolver 是基於 Servlet 3.0來處理 multipart 請求的,所以不需要引用其他 jar 包,但是必須使用支持 Servlet 3.0的容器才可以,以tomcat為例,從 Tomcat 7.0.x的版本開始就支持 Servlet 3.0了。
- StandardServletMultipartResolver的使用方法
- 配置文件
-
<bean id="multipartResolver" class="org.springframework.web.multipart.support.StandardServletMultipartResolver"> </bean> 這里並沒有配置文件大小等參數,這些參數的配置在 web.xml 中 <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> <multipart-config> <!-- 臨時文件的目錄 --> <location>d:/</location> <!-- 上傳文件最大2M --> <max-file-size>2097152</max-file-size> <!-- 上傳文件整個請求不超過4M --> <max-request-size>4194304</max-request-size> </multipart-config> </servlet>
-
- 上傳表單
-
<form action="${pageContext.request.contextPath}/test/file-upload.do" method="post" enctype="multipart/form-data"> <input type="file" name="file"> <input type="submit" value="提交"> </form>
-
- 處理方式有兩種
- 通過 MultipartFile 類型的參數處理
-
@RequestMapping("/file-upload") public ModelAndView upload(@RequestParam(value = "file", required = false) MultipartFile file, HttpServletRequest request, HttpSession session) { // 文件不為空 if(!file.isEmpty()) { // 文件存放路徑 String path = request.getServletContext().getRealPath("/"); // 文件名稱 String name = String.valueOf(new Date().getTime()+"_"+file.getOriginalFilename()); File destFile = new File(path,name); // 轉存文件 try { file.transferTo(destFile); } catch (IllegalStateException | IOException e) { e.printStackTrace(); } // 訪問的url String url = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/" + name; } ModelAndView mv = new ModelAndView(); mv.setViewName("other/home"); return mv; }
-
- 通過 MultipartHttpServletRequest 類型的參數
-
@RequestMapping("/file-upload") public ModelAndView upload(MultipartHttpServletRequest request, HttpSession session) { // 根據頁面input標簽的name MultipartFile file = request.getFile("file"); // 文件不為空 if(!file.isEmpty()) { // 文件存放路徑 String path = request.getServletContext().getRealPath("/"); // 文件名稱 String name = String.valueOf(new Date().getTime()+"_"+file.getOriginalFilename()); File destFile = new File(path,name); // 轉存文件 try { file.transferTo(destFile); } catch (IllegalStateException | IOException e) { e.printStackTrace(); } // 訪問的url String url = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/" + name; } ModelAndView mv = new ModelAndView(); mv.setViewName("other/home"); return mv; }
-
- 通過 MultipartFile 類型的參數處理
- 配置文件
- CommonsMultipartResolver的使用方法
- 配置文件
-
<!-- 定義文件上傳解析器 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 設定默認編碼 --> <property name="defaultEncoding" value="UTF-8"></property> <!-- 設定文件上傳的最大值為5MB,5*1024*1024 --> <property name="maxUploadSize" value="5242880"></property> <!-- 設定文件上傳時寫入內存的最大值,如果小於這個參數不會生成臨時文件,默認為10240 --> <property name="maxInMemorySize" value="40960"></property> <!-- 上傳文件的臨時路徑 --> <property name="uploadTempDir" value="fileUpload/temp"></property> <!-- 延遲文件解析 --> <property name="resolveLazily" value="true"/> </bean>
-
- 上傳表單
-
<form action="${pageContext.request.contextPath}/test/file-upload.do" method="post" enctype="multipart/form-data"> <input type="file" name="file"> <input type="submit" value="提交"> </form>
-
- 處理文件
-
@RequestMapping("/file-upload") public ModelAndView upload(@RequestParam(value = "file", required = false) MultipartFile file, HttpServletRequest request, HttpSession session) { // 文件不為空 if(!file.isEmpty()) { // 文件存放路徑 String path = request.getServletContext().getRealPath("/"); // 文件名稱 String name = String.valueOf(new Date().getTime()+"_"+file.getOriginalFilename()); File destFile = new File(path,name); // 轉存文件 try { file.transferTo(destFile); } catch (IllegalStateException | IOException e) { e.printStackTrace(); } // 訪問的url String url = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/" + name; } ModelAndView mv = new ModelAndView(); mv.setViewName("other/home"); return mv; }
-
- 配置文件
-