10.1 文件上傳
文件上傳是項目開發中最常用的功能。爲了實現上傳文件,必須將表單的 method 設置爲 POST,並將 enctype 設置爲 multipart/form-data。這樣,瀏覽器纔會把用戶選擇的文件二進制數據發送給服務器。
設置 enctype 爲 multipart/form-data,瀏覽器即會採用二進制流的方式來處理表單數據,而對於文件上傳的處理涉及在服務器端解析原始的 HTTP 響應。2003年,Apache Software Foundation 發佈了開源的 Commons FileUpload 組件,其很快成爲了 Servlet/JSP 程序員上傳文件的最佳選擇。Servlet 3.0 規範已經提供了方法來處理文件上傳,但這種上傳需要在 Servlet 中完成。而 Spring MVC 則提供了更簡單的封裝。
Spring MVC 爲文件上傳提供了直接的支持,這種支持是用即插即用的 MultipartResolver 實現的。Spring MVC 使用 Apache Commons FileUpload 技術實現了一個 MultipartResolver 實現類:CommonsMultipartResolver。因此,Spring MVC 的文件上傳還需要依賴 Apache Commons FileUpload 的組件。
10.1.1 Spring MVC 的文件上傳
- 準備 Apache 的 Commons FileUpload 的 jar 包放到項目的類路徑下,其中包括 commons-fileupload.jar 和 commons-io.jar。
- 新建文件上傳頁面 uploadForm.jsp,負責上傳文件的表單編碼類型必須爲 “multipart/form-data”。
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>文件上傳</title>
</head>
<body>
<h2>文件上傳</h2>
<form:form action="upload" enctype="multipart/form-data" method="post">
<table>
<tr>
<td>文件描述:</td>
<td><input type="text" name="description"></td>
</tr>
<tr>
<td>請選擇文件:</td>
<td><input type="file" name="file"></td>
</tr>
<tr>
<td><input type="submit" value="上傳"></td>
</tr>
</table>
</form:form>
</body>
</html>
- 新建 FileUploadController 類對頁面跳轉和上傳文件進行處理。
package controller;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
@Controller
public class FileUploadController {
private final static Log logger = LogFactory.getLog(FileUploadController.class);
@RequestMapping(value = "/{formName}")
public String autoForm(@PathVariable String formName){
return formName;
}
// 上傳文件會自動綁定到 MultipartFile 中
@RequestMapping(value = "/upload" , method = RequestMethod.POST)
public String upload(
HttpServletRequest request,
@RequestParam("description") String description,
@RequestParam("file") MultipartFile file
) throws Exception{
// 如果文件不爲空,寫入上傳路徑
if (!file.isEmpty()){
// 上傳文件路徑
String path = request.getServletContext().getRealPath("/images/");
// 上傳文件名
String filename = file.getOriginalFilename();
File filepath = new File(path,filename);
// 判斷路徑是否存在,如果不存在就創建一個
if (!filepath.getParentFile().exists()){
filepath.getParentFile().mkdirs();
}
//將上傳文件保存到一個目標文件當中
file.transferTo(new File(path+File.separator+filename));
return "success";
}else {
return "error";
}
}
}
Spring MVC 會將上傳文件綁定到 MultipartFile 對象中。MultipartFile 提供了獲取上傳文件內容、文件名等方法。通過 transferTo() 方法還可以將文件存儲到硬件中,MultipartFile 對象中的常用方法如下:
- byte[] getBytes()。獲取文件數據。
- String getContentType()。獲取文件 MIME 類型,如 image/jpeg 等。
- InputStream getInputStream()。獲取文件流。
- String getName()。獲取表單中文件組件的名字。
- String getOriginalFilename()。獲取上傳文件的原名。
- long getSize()。獲取文件的字節大小,單位爲 byte。
- boolean isEmpty()。是否有上傳的文件。
- void transferTo(FIle dest)。將上傳文件保存到一個目標文件中。
- 修改 Spring MVC 配置文件。由於 Spring MVC 上下文中默認沒有裝配 MultipartResolver,因此默認情況下其不能處理文件上傳工作。如果想使用 Spring 的文件上傳功能,則需要在上下文中配置 MultipartResolver。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="controller"/>
<mvc:annotation-driven/>
<mvc:default-servlet-handler/>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/content/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 上傳文件大小上限,單位爲字節(10MB) -->
<property name="maxUploadSize">
<value>10485760</value>
</property>
<!-- 請求的編碼格式,必須和 jsp 的 pageEncoding 屬性一致,以便正確讀取表單內容,默認爲 ISO-8859-1 -->
<property name="defaultEncoding">
<value>UTF-8</value>
</property>
</bean>
</beans>
- 部署 FileUploadTest 應用,在瀏覽器中輸入如下 URL 來測試:
http://localhost:8080/FileUploadTest/uploadForm
結果如下:
目錄中文件存在,上傳成功。
注: 在程序運行時,如果未上傳 commons-io.jar,則瀏覽器會報 Handler dispatch failed; nested exception is java.lang.NoClassDefFoundError: org/apache/commons/io/IOUtils
錯誤。如果出現此錯誤,將 commons-io.jar 導入項目即可修復。
10.1.2 使用對象接收上傳文件
在實際的項目開發中,很多時候上傳的文件會作爲對象的屬性被保存。Spring MVC 的處理也非常簡單。
- 新建 registerForm.jsp 用於用戶上傳文件。
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>用戶註冊</title>
</head>
<body>
<h2>用戶註冊</h2>
<form:form action="register" method="post" enctype="multipart/form-data">
<table>
<tr>
<td>用戶名:</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>請上傳頭像:</td>
<td><input type="file" name="image"></td>
</tr>
<tr>
<td><input type="submit" value="註冊"></td>
</tr>
</table>
</form:form>
</body>
</html>
- 新建 User 類用於接收頁面信息。
package domain;
import org.springframework.web.multipart.MultipartFile;
import java.io.Serializable;
public class User implements Serializable {
private String username;
// 對應上傳的 file,類型爲 MultipartFile ,上傳文件會自動綁定到 image 屬性當中。
private MultipartFile image;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public MultipartFile getImage() {
return image;
}
public void setImage(MultipartFile image) {
this.image = image;
}
}
- 新建 FIleUploadController 類,負責處理頁面跳轉和文件上傳。
package controller;
import domain.User;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
@Controller
public class FileUploadController {
private final static Log logger = LogFactory.getLog(FileUploadController.class);
@RequestMapping(value = "/{formName}")
public String autoForm(@PathVariable String formName){
return formName;
}
@RequestMapping(value = "/register")
public String register(
HttpServletRequest request,
Model model,
@ModelAttribute User user){
System.out.println(user.getUsername());
// 如果文件不爲空的話,寫入上傳路徑
if (!user.getImage().isEmpty()){
// 上傳文件路徑
String path = request.getServletContext().getRealPath("/images/");
System.out.println(path);
// 上傳文件名
String filename = user.getImage().getOriginalFilename();
System.out.println(filename);
File filepath = new File(path,filename);
// 判斷路徑是否存在,如果不存在就創建一個
if (!filepath.getParentFile().exists()){
filepath.getParentFile().mkdirs();
}
//將上傳文件保存到一個目標文件當中
try {
user.getImage().transferTo(new File(path+File.separator+filename));
}catch (Exception e){
e.printStackTrace();
}
// 將用戶添加到 model
model.addAttribute("user",user);
//跳轉到下載頁面
return "userInfo";
}else {
return "error";
}
}
}
- 新建用戶下載頁面 userInfo.jsp 。
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>文件下載</title>
</head>
<body>
<h3>文件下載</h3>
<a href="download?filename=${requestScope.user.image.originalFilename}">
${requestScope.user.image.originalFilename }
</a>
</body>
</html>
- 部署應用,並在瀏覽器輸入如下 URL 測試:
http://localhost:8080/FileUploadTest/registerForm
結果如下:
10.2 文件下載
Spring MVC 的文件下載
Spring MVC 的文件下載比較簡單,直接在頁面給出一個超鏈接,該鏈接 href 的屬性等於要下載文件的文件名,就可以實現文件下載了。
Spring MVC 提供了一個 ResponseEntity 類型,使用它可以很方便的定義返回的 HttpHeaders 和 HttpStatus。
- 在 FileUploadController 類中添加下載處理。
@RequestMapping(value = "/download")
public ResponseEntity<byte[]> download(
HttpServletRequest request,
@RequestParam("filename") String filename,
Model model) throws Exception {
// 下載文件路徑
String path = request.getServletContext().getRealPath("/images/");
File file = new File(path+File.separator+filename);
HttpHeaders headers = new HttpHeaders();
// 下載顯示的文件名,解決中文名稱亂碼問題
String downloadFileName = new String(filename.getBytes("UTF-8"),"iso-8859-1");
// 通知瀏覽器以 attachment(下載方式)打開圖片
headers.setContentDispositionFormData("attachment",downloadFileName);
// application/octet-stream:二進制流數據(最常見的文件下載)。
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
// 201 HttpStatus.CREATED
return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),headers, HttpStatus.CREATED);
}
download 處理方法接收到頁面傳遞的文件名 filename 後,使用 Apache Commons FileUpload 組件的 FileUtils 讀取項目的 iamges 文件夾下的該文件,並將其構建成 ResponseEntity 對象返回客戶端下載。
使用 ResponseEntity 對象,可以很方便地定義返回的 HttpHeaders 和 HttpStatus。上面代碼中的 MadiaType,代表的是 Internet Media Type,即互聯網媒體類型,也叫做 MIME 類型。在 Http 協議消息頭中,使用 Content-Type 來表示具體請求中的媒體類型信息。HttpStatus 類型代表的是 Http 協議中的狀態。有關 MediaType 和 HttpStatus 類的詳細信息參考 Spring MVC 的 API 文檔。
點擊下載頁面的超鏈接,顯示文件下載: