一、實現web開發中的文件上傳功能,需完成以下二步操做:html
•在web頁面中添加上傳輸入項
•在servlet中讀取上傳文件的數據,並保存到本地硬盤中.java
二、如何在web頁面中添加上傳輸入項? 程序員
<input type=「file」>標籤用於在web頁面中添加文件上傳輸入項,設置文件上傳輸入項時須注意:
•必需要設置input輸入項的name屬性,不然瀏覽器將不會發送上傳文件的數據.
•必須把form的enctype屬值設爲multipart/form-data.設置該值後,瀏覽器在上傳文件時,將把文件數據附帶在http請求消息體中,並使用MIME協議對上傳的文件進行描述,以方便接收方對上傳數據進行解析和處理.web
三、如何在Servlet中讀取文件上傳數據,並保存到本地硬盤中?apache
•Request對象提供了一個getInputStream方法,經過這個方法能夠讀取到客戶端提交過來的數據.但因爲用戶可能會同時上傳多個文件,在servlet端編程直接讀取上傳數據,並分別解析出相應的文件數據是一項很是麻煩的工做.例:編程
<!--upload.jsp -->
<form action="${pageContext.request.contextPath }/servlet/UploadServlet3" enctype="multipart/form-data" method="post"> 上傳用戶:<input type="text" name="username"><br/> 上傳文件1:<input type="file" name="file1"><br/> 上傳文件2:<input type="file" name="file2"><br/>
<input type="submit" value="上傳">
</form>
public class UploadServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //若是表單類型爲multipart/form-data的話,在servlet中注意就不能採用傳統方式獲取數據
/*String username = request.getParameter("username"); System.out.println(username);*/ InputStream in = request.getInputStream(); int len = 0; byte buffer[] = new byte[1024]; while((len=in.read(buffer))>0){ System.out.println(new String(buffer,0,len));//解析麻煩 } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
•爲方便用戶處理文件上傳數據,Apache 開源組織提供了一個用來處理表單文件上傳的一個開源組件( Commons-fileupload ),該組件性能優異,而且其API使用極其簡單,可讓開發人員輕鬆實現web文件上傳功能,所以在web開發中實現文件上傳功能,一般使用Commons-fileupload組件實現.使用Commons-fileupload組件實現文件上傳,須要導入該組件相應的支撐jar包:Commons-fileupload和commons-io.commons-io 不屬於文件上傳組件的開發jar文件,但Commons-fileupload 組件從1.1 版本開始,它工做時須要commons-io包的支持.瀏覽器
一、核心API—DiskFileItemFactory緩存
DiskFileItemFactory 是建立 FileItem 對象的工廠,這個工廠類經常使用方法:服務器
//設置內存緩衝區的大小,默認值爲10K.當上傳文件大於緩衝區大小時, fileupload組件將使用臨時文件緩存上傳文件. public void setSizeThreshold(int sizeThreshold) //指定臨時文件目錄,默認值爲System.getProperty("java.io.tmpdir"). public void setRepository(java.io.File repository) //構造函數 public DiskFileItemFactory(int sizeThreshold, java.io.File repository)
二、核心API—ServletFileUploaddom
ServletFileUpload 負責處理上傳的文件數據,並將表單中每一個輸入項封裝成一個 FileItem 對象中.經常使用方法有:
//判斷上傳表單是否爲multipart/form-data類型
boolean isMultipartContent(HttpServletRequest request) //解析request對象,並把表單中的每個輸入項包裝成一個fileItem 對象,並返回一個保存了全部FileItem的list集合.
List parseRequest(HttpServletRequest request) //設置上傳文件的最大值
setFileSizeMax(long fileSizeMax) //設置上傳文件總量的最大值
setSizeMax(long sizeMax) //設置編碼格式
setHeaderEncoding(java.lang.String encoding) //上傳進度監聽器
setProgressListener(ProgressListener pListener)
原理版:
//處理上傳數據
public class UploadServlet2 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try{ DiskFileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); List<FileItem> list = upload.parseRequest(request); for(FileItem item : list){ if(item.isFormField()){ //爲普通輸入項
String inputName = item.getFieldName(); String inputValue = item.getString(); System.out.println(inputName + "=" + inputValue); }else{ //表明當前處理的item裏面封裝的是上傳文件 //C:\Documents and Settings\ThinkPad\桌面\a.txt a.txt
String filename = item.getName().substring(item.getName().lastIndexOf("\\")+1); InputStream in = item.getInputStream(); int len = 0; byte buffer[] = new byte[1024]; FileOutputStream out = new FileOutputStream("c:\\" + filename); while((len=in.read(buffer))>0){ out.write(buffer, 0, len); } in.close(); out.close(); } } }catch (Exception e) { throw new RuntimeException(e);//直接拋給頁面不太好 } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
一、上傳文件的中文亂碼
1>解決文件的亂碼
ServletFileUpload.setHeaderEncoding("UTF-8"),或者request的setCharacterEncoding
2>解決普通輸入項的亂碼(注意,表單類型爲multipart/form-data的時候,設置request的編碼是無效的)
FileItem.setString("UTF-8"); //解決亂碼
二、在處理表單以前,要記得調用isMultipartContent:
ServletFileUpload.isMultipartContent方法判斷提交表單的類型,若是該方法返回true,則按上傳方式處理,不然按照傳統方式處理表單便可.
三、設置解析器緩衝區的大小,以及臨時文件的刪除
因爲文件大小超出DiskFileItemFactory.setSizeThreshold方法設置的內存緩衝區的大小時,Commons-fileupload組件將使用臨時文件保存上傳數據,所以在程序結束時,務必調用FileItem.delete方法刪除臨時文件.Delete方法的調用必須位於流關閉以後,不然會出現文件佔用,而致使刪除失敗的狀況.
四、文件存放位置
在作上傳系統時,千萬要注意上傳文件的保存目錄,這個上傳文件的保存目錄絕對不能讓外界直接訪問到.好比用戶上傳一個jsp頁面,而後能夠在裏面操做服務器關閉,格式化C盤等.
五、限制上傳文件的類型
在處理上傳文件時,判斷上傳文件的後綴名是否是容許的
六、限制上傳文件的大小
調用解析器的ServletFileUpload.setFileSizeMax(1024*1024*5);就能夠限制上傳文件的大小,若是上傳文件超出限制,則解析器會拋FileUploadBase.FileSizeLimitExceededException異常,程序員經過是否抓到這個異常,進而就能夠給用戶友好提示.
七、爲避免上傳文件的覆蓋,程序在保存上傳文件時,要爲每個文件生成一個惟一的文件名.
八、爲避免在一個文件夾下面保存超過1000個文件,影響文件訪問性能,程序應該把上傳文件打散後存儲.
九、監聽上傳進度,進度條之類,要結合js.
完善後的上傳案例:
public class UploadServlet3 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { List<String> types = Arrays.asList("jpg", "gif", "avi", "txt"); try { DiskFileItemFactory factory = new DiskFileItemFactory(); factory.setSizeThreshold(1024 * 1024); factory.setRepository(new File(this.getServletContext().getRealPath("/temp"))); ServletFileUpload upload = new ServletFileUpload(factory); // setProgressListener具體用法可參考apache用戶指導 upload.setProgressListener(new ProgressListener() { public void update(long pBytesRead, long pContentLength,int pItems) { System.out.println("當前已解析:" + pBytesRead); } }); upload.setFileSizeMax(1024 * 1024 * 5); if (!upload.isMultipartContent(request)) { // 按照傳統方式獲取表單數據 request.getParameter("username"); return; } upload.setHeaderEncoding("UTF-8"); List<FileItem> list = upload.parseRequest(request); for (FileItem item : list) { if (item.isFormField()) { // 爲普通輸入項 String inputName = item.getFieldName(); String inputValue = item.getString("UTF-8"); // inputValue = new String(inputValue.getBytes("iso8859-1"),"UTF-8"); System.out.println(inputName + "=" + inputValue); } else { String filename = item.getName().substring(item.getName().lastIndexOf("\\")+1); if (filename == null || filename.trim().equals("")) { continue; } // 設置上傳文件類型 String ext = filename.substring(filename.lastIndexOf(".") + 1); if (!types.contains(ext)) { request.setAttribute("message", "本系統不支持" + ext + "這種類型"); request.getRequestDispatcher("/message.jsp").forward(request, response); return; } InputStream in = item.getInputStream(); int len = 0; byte buffer[] = new byte[1024]; String saveFileName = generateFileName(filename); String savepath = generateSavePath(this.getServletContext() .getRealPath("/WEB-INF/upload"), saveFileName); FileOutputStream out = new FileOutputStream(savepath + File.separator + saveFileName); while ((len = in.read(buffer)) > 0) { out.write(buffer, 0, len); } in.close(); out.close(); item.delete(); // 刪除臨時文件 } } } catch (FileUploadBase.FileSizeLimitExceededException e) { request.setAttribute("message", "文件大小不能超過5m"); request.getRequestDispatcher("/message.jsp").forward(request,response); return; } catch (Exception e) { throw new RuntimeException(e); } request.setAttribute("message", "上傳成功!!"); request.getRequestDispatcher("/message.jsp").forward(request, response); } public String generateSavePath(String path, String filename) { int hashcode = filename.hashCode(); int dir1 = hashcode & 15; int dir2 = (hashcode >> 4) & 0xf; String savepath = path + File.separator + dir1 + File.separator + dir2; File file = new File(savepath); if (!file.exists()) { file.mkdirs(); } return savepath; } public String generateFileName(String filename) { // 83434-83u483-934934 return UUID.randomUUID().toString() + "_" + filename; } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
//列出網站全部文件 public class ListFileServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String path = this.getServletContext().getRealPath("/WEB-INF/upload"); Map map = new HashMap(); listfile(new File(path),map); request.setAttribute("map", map); request.getRequestDispatcher("/listfile.jsp").forward(request, response); } //*如何保存遞歸出來的資源 public void listfile(File file,Map map){ if(!file.isFile()){ File children[] = file.listFiles(); for(File f : children){ listfile(f,map); } }else{ String filename = file.getName().substring(file.getName().indexOf("_")+1); //頁面中要這麼顯示:<a href="/servlet?filename=文件在服務器的名稱">文件的原始文件名</a> map.put(file.getName(),filename); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } <!--listfile.jsp --> <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>文件列表</title> </head> <body> 下載文件有:<br/> <c:forEach var="entry" items="${requestScope.map}"> <!--c:url標籤自動進行url編碼 --> <c:url var="url" value="/servlet/DownLoadServlet"> <c:param name="filename" value="${entry.key}"></c:param> </c:url> ${entry.value } <a href="${url }">下載</a><br/> </c:forEach> </body> </html> public class DownLoadServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //獲得要下載的文件名uuid,url編碼過來的數據只能手動解碼 String filename = request.getParameter("filename"); filename = new String(filename.getBytes("iso8859-1"), "UTF-8"); // 找出這個文件 url,"/WEB-INF/upload"這裏不必用File.separator,c:\\盤地址才使用 String path = this.getServletContext().getRealPath("/WEB-INF/upload") + File.separator + getpath(filename); File file = new File(path + File.separator + filename); if (!file.exists()) { request.setAttribute("message", "對不起,您要下載的資源已被刪除"); request.getRequestDispatcher("/message.jsp").forward(request,response); return; } // 獲得文件的原始文件名 String oldname = file.getName().substring(file.getName().indexOf("_") + 1); // 通知瀏覽器如下載方式打開下面發送的數據,servlet中要URLEncoder編碼 response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(oldname, "UTF-8")); FileInputStream in = new FileInputStream(file); int len = 0; byte buffer[] = new byte[1024]; OutputStream out = response.getOutputStream(); while ((len = in.read(buffer)) > 0) { out.write(buffer, 0, len); } in.close(); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } public String getpath(String filename) { int hashcode = filename.hashCode(); int dir1 = hashcode & 15; int dir2 = (hashcode >> 4) & 0xf; return dir1 + File.separator + dir2; // 3/5 } }
ps:文件上傳下載經典案例,見文件.