Servlet之文件上傳與下載
簡介:在web項目中常常需要一些上傳文件,或者是下載一些文件的功能。
例如:註冊表單/保存商品等相關模塊!
--à 註冊選擇頭像 / 商品圖片
(數據庫:存儲圖片路徑 /圖片保存到服務器中指定的目錄)
一、文件的上傳
實現web開發中的文件上傳功能其實主要分爲兩步:
1.在web頁面中添加上上傳輸入項
2.在servlet中讀取上傳文件的數據,並保存到本地的硬盤中。
1.在web頁面中添加上傳輸入選項:
l 表單的method 屬性應該設置爲post方法,不能使用get方法
l 表單的enctype 屬性應該設置爲 multipart/form-data (默認類型:enctype="application/x-www-form-urlencoded")
l 表單的 action 屬性因該設置爲在後端服務器上處理文件上傳的Servlet文件。(根據該Servlet在web.xml中<url-pattern></url-pattern>的值)來上傳文件
l 上傳單個文件,您應該使用單個帶有屬性 <input type=”file”/>的標籤,
必須要設置name屬性,否則瀏覽器將不會發送上傳的文件
若要上傳多個,可設置多個該標籤,瀏覽器會爲每個 input 標籤關聯一個瀏覽按鈕但各個表情name屬性值不能一致
注意:必須把form的enctype屬值設爲multipart/form-data method
屬性設置爲post方式。設置該值後,瀏覽器在上傳文件時,將把文件數據附帶在http請求消息體中,並使用MIME協議對上傳的文件進行描述,以方便接收方對上傳數據進行解析和處理。
請求方式:
------WebKitFormBoundary3BxAgYMRb8RuMWui (每個文件的開始)
name是屬性filename是對應的文件名
------WebKitFormBoundary3BxAgYMRb8RuMWui (每個文件的結束)
3.在servlet中讀取上傳文件的數據,並保存到本地的硬盤中。
在Servlet中Request對象提供了一個可以讀取流,getInputStream方法,通過這個方法可以讀取到客戶端提交過來的數據。但是由於用戶可能會同時上傳多個文件,在servlet段編程直接讀取上傳的數據,並分別解析出相應的文件數據是一件非常麻煩的事情
|
public class UploadServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /* request.getParameter(""); // GET/POST request.getQueryString(); // 獲取GET提交的數據 request.getInputStream(); // 獲取post提交的數據 */ /***********手動獲取文件上傳表單數據************/ //1. 獲取表單數據流 InputStream in = request.getInputStream(); //2. 轉換流 InputStreamReader inStream = new InputStreamReader(in,"UTF-8"); //3. 緩衝流 BufferedReader reader = new BufferedReader(inStream); // 輸出數據 String str = null; while ((str = reader.readLine()) !=null) { System.out.println(str); } // 關閉 reader.close(); inStream.close(); in.close(); } |
輸出結果: |
------WebKitFormBoundaryGoQviatB7iM1dhPr Content-Disposition: form-data; name="userName" 【FileItem】
Jack ------WebKitFormBoundaryGoQviatB7iM1dhPr Content-Disposition: form-data; name="file_img"; filename="reamde.txt" Content-Type: text/plain 【FileItem】
test!!!!!!!!!!!!! test!!!!!!!!!!!!! ------WebKitFormBoundaryGoQviatB7iM1dhPr--
|
總結: 最終獲取數據,要對上面的結果進行解析! 文件上傳,在開發中經常用,每次都寫解析程序!(工具類) 也可以使用開源的文件上傳組件-FileUpload組件! |
|
如果我們們解析用戶上傳的文件,如果文件一多,分別解析出相應的文件會非常的麻煩
爲方便用戶處理文件上傳數據,Apache 開源組織提供了一個用來處理表單文件上傳的一個開源組件(Commons-fileupload ),該組件性能優異,並且其API使用極其簡單,可以讓開發人員輕鬆實現web文件上傳功能,因此在web開發中實現文件上傳功能,通常使用Commons-fileupload組件實現。
使用Commons-fileupload組件實現文件上傳
必須導入兩個jar包:
1.Commons-fileupload 【文件上傳組件核心jar包】
· 可以從 http://commons.apache.org/proper/commons-fileupload/ 下載。
2.Commons-io 【封裝了對文件處理的相關工具類】
(不屬於文件上傳組件開發的但是Commons-fileupload組件從1.1版本開始,需要commons-io包的支持)
可以從http://commons.apache.org/proper/commons-io/ 下載。
Fileupload組件工作流程(重點)
重要的兩個類:
1. DiskFileltemFactory工廠類
DiskFileltemFactory是Fileltem對象的工廠
常用的方法:
public DiskFileItemFactory(int sizeThreshold, java.io.File repository)
構造函數
public void setSizeThreshold(int sizeThreshold)
設置內存緩衝區的大小,默認值爲10K。當上傳文件大於緩衝區大小時,fileupload組件將使用臨時文件緩存上傳文件。
public void setRepository(java.io.File repository)
指定臨時文件目錄,默認值爲System.getProperty("java.io.tmpdir").
2. ServletFileUpload對象,將DiskFileltemFactory生成的對象傳入其中。然後會將表單中的每個輸入項封裝成工廠類中生成的Fileltem對象。常用的方法有:
boolean isMultipartContent(HttpServletRequest request)
判斷上傳表單是否爲multipart/form-data類型
List parseRequest(HttpServletRequest request)
解析request對象,並把表單中的每一個輸入項包裝成一個fileItem對象,並返回一個保存了所有FileItem的list集合。
setFileSizeMax(long fileSizeMax)
設置上傳文件的最大值
setSizeMax(long sizeMax)
設置上傳文件總量的最大值
setHeaderEncoding(java.lang.String encoding)
設置編碼格式
setProgressListener(ProgressListener pListener)
具體實現步驟:
1、創建DiskFileItemFactory對象,設置緩衝區大小和臨時文件目錄
2、使用DiskFileItemFactory對象創建ServletFileUpload對象,並設置上傳文件的大小限制。
3、調用ServletFileUpload.parseRequest方法解析request對象,得到一個保存了所有上傳內容的List對象。
4、對list進行迭代,每迭代一個FileItem對象,調用其isFormField方法判斷是否是上傳文件
爲普通表單字段,則調用getFieldName、getString方法得到字段名和字段值
爲上傳文件,則調用getInputStream方法得到數據輸入流,從而讀取上傳數據。
編碼實現文件上傳
小技巧:
每次動態增加一個文件上傳輸入框,都把它和刪除按紐放置在一個單獨的div中,並對刪除按紐的onclick事件進行響應,使之刪除刪除按紐所在的div。
如:
this.parentNode.parentNode.removeChild(this.parentNode);
針對上傳文件的細節處理:(重點)
1.中文文件亂碼問題
文件名中文亂碼問題,可調用ServletUpLoader的setHeaderEncoding方法,或者設置request的setCharacterEncoding屬性
2.臨時文件的刪除問題
由於文件大小超出DiskFileItemFactory.setSizeThreshold方法設置的內存緩衝區的大小時,Commons-fileupload組件將使用臨時文件保存上傳數據,因此在程序結束時,務必調用FileItem.delete方法刪除臨時文件。
Delete方法的調用必須位於流關閉之後,否則會出現文件佔用,而導致刪除失敗的情況。
3.文件存放位置
爲保證服務器安全,上傳文件應保存在應用程序的WEB-INF目錄下,或者不受WEB服務器管理的目錄。
爲防止多用戶上傳相同文件名的文件,而導致文件覆蓋的情況發生,文件上傳程序應保證上傳文件具有唯一文件名。
爲防止單個目錄下文件過多,影響文件讀寫速度,處理上傳文件的程序應根據可能的文件上傳總量,選擇合適的目錄結構生成算法,將上傳文件分散存儲。
4. ProgressListener顯示上傳進度
ProgressListener progressListener = new ProgressListener() {
public void update(long pBytesRead, long pContentLength, int pItems) {
System.out.println("到現在爲止, " + pBytesRead + " 字節已上傳,總大小爲 "
+ pContentLength);
}
};
upload.setProgressListener(progressListener);
以KB爲單位顯示上傳進度
long temp = -1; //temp注意設置爲類變量
long ctemp = pBytesRead /1024;
if (mBytes == ctemp)
return;
temp = mBytes;
具體代碼實現
package com.runoob.test;
import java.io.File;import java.io.IOException;import java.io.PrintWriter;import java.util.List;
import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;import org.apache.commons.fileupload.disk.DiskFileItemFactory;import org.apache.commons.fileupload.servlet.ServletFileUpload;
/**
* Servlet implementation class UploadServlet
*/@WebServlet("/UploadServlet")public class UploadServlet extends HttpServlet {
private static final long serialVersionUID= 1L;
// 上傳文件存儲目錄
private static final String UPLOAD_DIRECTORY= "upload";
// 上傳配置
private static final int MEMORY_THRESHOLD = 1024 * 1024 * 3; // 3MB
private static final int MAX_FILE_SIZE = 1024 * 1024 * 40; // 40MB
private static final int MAX_REQUEST_SIZE = 1024 * 1024 * 50; // 50MB
/**
* 上傳數據及保存文件
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// 檢測是否爲多媒體上傳
if (!ServletFileUpload.isMultipartContent(request)) {
// 如果不是則停止
PrintWriter writer= response.getWriter();
writer.println("Error: 表單必須包含 enctype=multipart/form-data");
writer.flush();
return;
}
// 配置上傳參數
DiskFileItemFactory factory= new DiskFileItemFactory();
// 設置內存臨界值 - 超過後將產生臨時文件並存儲於臨時目錄中
factory.setSizeThreshold(MEMORY_THRESHOLD);
// 設置臨時存儲目錄
factory.setRepository(new File(System.getProperty("java.io.tmpdir")));
ServletFileUpload upload= new ServletFileUpload(factory);
// 設置最大文件上傳值
upload.setFileSizeMax(MAX_FILE_SIZE);
// 設置最大請求值 (包含文件和表單數據)
upload.setSizeMax(MAX_REQUEST_SIZE);
// 中文處理
upload.setHeaderEncoding("UTF-8");
// 構造臨時路徑來存儲上傳的文件
// 這個路徑相對當前應用的目錄
String uploadPath= request.getServletContext().getRealPath("./") + File.separator+ UPLOAD_DIRECTORY;
// 如果目錄不存在則創建
File uploadDir= new File(uploadPath);
if (!uploadDir.exists()) {
uploadDir.mkdir();
}
try {
// 解析請求的內容提取文件數據
@SuppressWarnings("unchecked")
List<FileItem> formItems= upload.parseRequest(request);
if (formItems!= null && formItems.size() > 0) {
// 迭代表單數據
for (FileItem item: formItems) {
// 處理不在表單中的字段
if (!item.isFormField()) {
String fileName= new File(item.getName()).getName();
String filePath= uploadPath + File.separator+ fileName;
File storeFile= new File(filePath);
// 在控制檯輸出文件的上傳路徑
System.out.println(filePath);
// 保存文件到硬盤
item.write(storeFile);
request.setAttribute("message",
"文件上傳成功!");
}
}
}
} catch (Exception ex) {
request.setAttribute("message",
"錯誤信息: " + ex.getMessage());
}
// 跳轉到 message.jsp
request.getServletContext().getRequestDispatcher("/message.jsp").forward(
request, response);
}}
二、文件下載
Web應用中實現文件下載的兩種方式:
1.超鏈接直接指向下載資源
2.程序實現下載需要設置兩個響應頭告訴頁面響應的類型:
Content-Type響應頭
設置Content-Type的值爲:application/x-msdownload。
response.setContentType(“application/x-msdownload”);
(Web服務器需要告訴瀏覽器其所輸入的內容類型,而是一個要保存到本地的下載文件)
Content-Disposition報頭
Web 服務器希望瀏覽器不直接處理相應的實體內容,而是由用戶選擇將相應的實體內容保存到一個文件中,所以需要設置Content-Disposition報頭。該報頭指定了接收程序處理數據內容的方式,在HTTP 應用中只有attachment 是標準方式,attachment表示要求用戶干預。在 attachment後面還可以指定 filename參數,該參數是服務器建議瀏覽器將實體內容保存到文件中的文件名稱。在設置 Content-Dispostion之前一定要指定 Content-Type.
String fileName = (獲取的文件名防止傳到瀏覽器是發生亂碼問題)
//設置attachment標準方式,並且將文件名正確轉碼
String str =“attachment; filename=”+java.net.URLEncoder.Encode(fileName,”UTF-8”);
response.setHeader(“Content-Dispostioin”,str);
下載文件流的選擇
因爲要下載的文件可以是各種類型的文件,所以要將文件傳送給客戶端,其相應內容應該被當做二進制來處理,所以應該調用 response.getOutputStream()方法返回ServeltOutputStream 對象來向客戶端寫入文件內容。
ServletOutputStream sos = response.getOutputStream();
byte[] data = new byte[2048];
Int len = -1;
While((len = is.read(data))!=-1){
Sos.write(data,0,len);
}
java案例
Index.jsp |
<body> <a href="${pageContext.request.contextPath }/upload.jsp">文件上傳</a> <a href="${pageContext.request.contextPath }/fileServlet?method=downList">文件下載</a>
</body> |
Upload.jsp |
<body> <form name="frm_test" action="${pageContext.request.contextPath }/fileServlet?method=upload" method="post" enctype="multipart/form-data"> <%--<input type="hidden" name="method" value="upload">--%>
用戶名:<input type="text" name="userName"> <br/> 文件: <input type="file" name="file_img"> <br/>
<input type="submit" value="提交"> </form> </body> |
Downlist.jsp |
<body> <table border="1" align="center"> <tr> <th>序號</th> <th>文件名</th> <th>操作</th> </tr> <c:forEach var="en" items="${requestScope.fileNames}"varStatus="vs"> <tr> <td>${vs.count }</td> <td>${en.value }</td> <td> <%--<a href="${pageContext.request.contextPath }/fileServlet?method=down&..">下載</a>--%> <!-- 構建一個地址 --> <c:url var="url" value="fileServlet"> <c:param name="method" value="down"></c:param> <c:param name="fileName" value="${en.key}"></c:param> </c:url> <!-- 使用上面地址 --> <a href="${url }">下載</a> </td> </tr> </c:forEach> </table> </body> |
FileServlet.java |
/** * 處理文件上傳與下載 * @author Jie.Yuan * */ public class FileServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 獲取請求參數: 區分不同的操作類型 String method = request.getParameter("method"); if ("upload".equals(method)) { // 上傳 upload(request,response); } else if ("downList".equals(method)) { // 進入下載列表 downList(request,response); } else if ("down".equals(method)) { // 下載 down(request,response); } } /** * 1. 上傳 */ private void upload(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { // 1. 創建工廠對象 FileItemFactory factory = new DiskFileItemFactory(); // 2. 文件上傳核心工具類 ServletFileUpload upload = new ServletFileUpload(factory); // 設置大小限制參數 upload.setFileSizeMax(10*1024*1024); // 單個文件大小限制 upload.setSizeMax(50*1024*1024); // 總文件大小限制 upload.setHeaderEncoding("UTF-8"); // 對中文文件編碼處理
// 判斷 if (upload.isMultipartContent(request)) { // 3. 把請求數據轉換爲list集合 List<FileItem> list = upload.parseRequest(request); // 遍歷 for (FileItem item : list){ // 判斷:普通文本數據 if (item.isFormField()){ // 獲取名稱 String name = item.getFieldName(); // 獲取值 String value = item.getString(); System.out.println(value); } // 文件表單項 else { /******** 文件上傳***********/ // a. 獲取文件名稱 String name = item.getName(); // ----處理上傳文件名重名問題---- // a1. 先得到唯一標記 String id = UUID.randomUUID().toString(); // a2. 拼接文件名 name = id + "#" + name; // b. 得到上傳目錄 String basePath = getServletContext().getRealPath("/upload"); // c. 創建要上傳的文件對象 File file = new File(basePath,name); // d. 上傳 item.write(file); item.delete(); // 刪除組件運行時產生的臨時文件 } } } } catch (Exception e) { e.printStackTrace(); } }
/** * 2. 進入下載列表 */ private void downList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 實現思路:先獲取upload目錄下所有文件的文件名,再保存;跳轉到down.jsp列表展示 //1. 初始化map集合Map<包含唯一標記的文件名,簡短文件名> ; Map<String,String> fileNames = new HashMap<String,String>(); //2. 獲取上傳目錄,及其下所有的文件的文件名 String bathPath = getServletContext().getRealPath("/upload"); // 目錄 File file = new File(bathPath); // 目錄下,所有文件名 String list[] = file.list(); // 遍歷,封裝 if (list != null && list.length > 0){ for (int i=0; i<list.length; i++){ // 全名 String fileName = list[i]; // 短名 String shortName = fileName.substring(fileName.lastIndexOf("#")+1); // 封裝 fileNames.put(fileName, shortName); } } // 3. 保存到request域 request.setAttribute("fileNames", fileNames); // 4. 轉發 request.getRequestDispatcher("/downlist.jsp").forward(request, response);
}
/** * 3. 處理下載 */ private void down(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 獲取用戶下載的文件名稱(url地址後追加數據,get) String fileName = request.getParameter("fileName"); fileName = new String(fileName.getBytes("ISO8859-1"),"UTF-8"); // 先獲取上傳目錄路徑 String basePath = getServletContext().getRealPath("/upload"); // 獲取一個文件流 InputStream in = new FileInputStream(new File(basePath,fileName)); // 如果文件名是中文,需要進行url編碼 fileName = URLEncoder.encode(fileName, "UTF-8"); // 設置下載的響應頭 response.setHeader("content-disposition","attachment;fileName=" + fileName); // 獲取response字節流 OutputStream out = response.getOutputStream(); byte[] b = new byte[1024]; int len = -1; while ((len = in.read(b)) != -1){ out.write(b, 0, len); } // 關閉 out.close(); in.close(); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); }
}
|