Java 檔案或者圖片上傳下載
上傳和下載 以前覺得很簡單 印象中使用外掛實現上傳下載的 直到今天。。。。
今天看了一篇博文,內容是上傳下載功能,三個Java檔案(上傳功能,下載列表顯示功能,下載功能),三個jsp檔案(上傳頁面,下載列表顯示頁面,成功頁面)。
我本地測試過了,分享給你們,有問題你們提出來
加入Apache的commons-fileupload檔案上傳元件的相關Jar包:
A1:上傳功能 ---Java 程式碼(UploadHandleServlet):
package com.bailian.arch.amos.demo.servlet; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileUploadBase; import org.apache.commons.fileupload.ProgressListener; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.List; import java.util.UUID; /** * Created by user on 2016/9/30. */ public class UploadHandleServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //得到上傳檔案的儲存目錄,將上傳的檔案存放於WEB-INF目錄下,不允許外界直接訪問,保證上傳檔案的安全 String savePath = this.getServletContext().getRealPath("/WEB-INF/upload"); //上傳時生成的臨時檔案儲存目錄 String tempPath = this.getServletContext().getRealPath("/WEB-INF/temp"); File tmpFile = new File(tempPath); if (!tmpFile.exists()) { //建立臨時目錄 tmpFile.mkdir(); } //訊息提示 String message = ""; try{ //使用Apache檔案上傳元件處理檔案上傳步驟: //1、建立一個DiskFileItemFactory工廠 DiskFileItemFactory factory = new DiskFileItemFactory(); //設定工廠的緩衝區的大小,當上傳的檔案大小超過緩衝區的大小時,就會生成一個臨時檔案存放到指定的臨時目錄當中。 factory.setSizeThreshold(1024*100);//設定緩衝區的大小為100KB,如果不指定,那麼緩衝區的大小預設是10KB //設定上傳時生成的臨時檔案的儲存目錄 factory.setRepository(tmpFile); //2、建立一個檔案上傳解析器 ServletFileUpload upload = new ServletFileUpload(factory); //監聽檔案上傳進度 upload.setProgressListener(new ProgressListener(){ public void update(long pBytesRead, long pContentLength, int arg2) { System.out.println("檔案大小為:" + pContentLength + ",當前已處理:" + pBytesRead); /** * 檔案大小為:14608,當前已處理:4096 檔案大小為:14608,當前已處理:7367 檔案大小為:14608,當前已處理:11419 檔案大小為:14608,當前已處理:14608 */ } }); //解決上傳檔名的中文亂碼 upload.setHeaderEncoding("UTF-8"); //3、判斷提交上來的資料是否是上傳表單的資料 if(!ServletFileUpload.isMultipartContent(request)){ //按照傳統方式獲取資料 return; } //設定上傳單個檔案的大小的最大值,目前是設定為1024*1024位元組,也就是1MB upload.setFileSizeMax(1024*1024); //設定上傳檔案總量的最大值,最大值=同時上傳的多個檔案的大小的最大值的和,目前設定為10MB upload.setSizeMax(1024*1024*10); //4、使用ServletFileUpload解析器解析上傳資料,解析結果返回的是一個List<FileItem>集合,每一個FileItem對應一個Form表單的輸入項 List<FileItem> list = upload.parseRequest(request); for(FileItem item : list){ //如果fileitem中封裝的是普通輸入項的資料 if(item.isFormField()){ String name = item.getFieldName(); //解決普通輸入項的資料的中文亂碼問題 String value = item.getString("UTF-8"); //value = new String(value.getBytes("iso8859-1"),"UTF-8"); System.out.println(name + "=" + value); }else{//如果fileitem中封裝的是上傳檔案 //得到上傳的檔名稱, String filename = item.getName(); System.out.println(filename); if(filename==null || filename.trim().equals("")){ continue; } //注意:不同的瀏覽器提交的檔名是不一樣的,有些瀏覽器提交上來的檔名是帶有路徑的,如: c:\a\b\1.txt,而有些只是單純的檔名,如:1.txt //處理獲取到的上傳檔案的檔名的路徑部分,只保留檔名部分 filename = filename.substring(filename.lastIndexOf("\\")+1); //得到上傳檔案的副檔名 String fileExtName = filename.substring(filename.lastIndexOf(".")+1); //如果需要限制上傳的檔案型別,那麼可以通過檔案的副檔名來判斷上傳的檔案型別是否合法 System.out.println("上傳的檔案的副檔名是:"+fileExtName); //獲取item中的上傳檔案的輸入流 InputStream in = item.getInputStream(); //得到檔案儲存的名稱 String saveFilename = makeFileName(filename); //得到檔案的儲存目錄 String realSavePath = makePath(saveFilename, savePath); //建立一個檔案輸出流 FileOutputStream out = new FileOutputStream(realSavePath + "\\" + saveFilename); //建立一個緩衝區 byte buffer[] = new byte[1024]; //判斷輸入流中的資料是否已經讀完的標識 int len = 0; //迴圈將輸入流讀入到緩衝區當中,(len=in.read(buffer))>0就表示in裡面還有資料 while((len=in.read(buffer))>0){ //使用FileOutputStream輸出流將緩衝區的資料寫入到指定的目錄(savePath + "\\" + filename)當中 out.write(buffer, 0, len); } //關閉輸入流 in.close(); //關閉輸出流 out.close(); //刪除處理檔案上傳時生成的臨時檔案 //item.delete(); message = "檔案上傳成功!"; } } }catch (FileUploadBase.FileSizeLimitExceededException e) { e.printStackTrace(); request.setAttribute("message", "單個檔案超出最大值!!!"); request.getRequestDispatcher("/message.jsp").forward(request, response); return; }catch (FileUploadBase.SizeLimitExceededException e) { e.printStackTrace(); request.setAttribute("message", "上傳檔案的總的大小超出限制的最大值!!!"); request.getRequestDispatcher("/message.jsp").forward(request, response); return; }catch (Exception e) { message= "檔案上傳失敗!"; e.printStackTrace(); } request.setAttribute("message",message); request.getRequestDispatcher("/message.jsp").forward(request, response); } /** * @Method: makeFileName * @Description: 生成上傳檔案的檔名,檔名以:uuid+"_"+檔案的原始名稱 * @Anthor: * @param filename 檔案的原始名稱n * @return uuid+"_"+檔案的原始名稱 */ private String makeFileName(String filename){ //2.jpg //為防止檔案覆蓋的現象發生,要為上傳檔案產生一個唯一的檔名 return UUID.randomUUID().toString() + "_" + filename; } /** * 為防止一個目錄下面出現太多檔案,要使用hash演算法打散儲存 * @Method: makePath * @Description: * @Anthor: * * @param filename 檔名,要根據檔名生成儲存目錄 * @param savePath 檔案儲存路徑 * @return 新的儲存目錄 */ private String makePath(String filename,String savePath){ //得到檔名的hashCode的值,得到的就是filename這個字串物件在記憶體中的地址 int hashcode = filename.hashCode(); int dir1 = hashcode&0xf; //0--15 int dir2 = (hashcode&0xf0)>>4; //0-15 //構造新的儲存目錄 String dir = savePath + "\\" + dir1 + "\\" + dir2; //upload\2\3 upload\3\5 //File既可以代表檔案也可以代表目錄 File file = new File(dir); //如果目錄不存在 if(!file.exists()){ //建立目錄 file.mkdirs(); } return dir; } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
A2:上傳功能--在Web.xml檔案中註冊UploadHandleServlet
<servlet> <servlet-name>UploadHandleServlet</servlet-name> <servlet-class>com.bailian.arch.amos.demo.servlet.UploadHandleServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>UploadHandleServlet</servlet-name> <url-pattern>/servlet/UploadHandleServlet</url-pattern> </servlet-mapping>
A3:上傳功能-- jsp頁面程式碼:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%-- Created by IntelliJ IDEA. User: user Date: 2016/9/29 Time: 16:10 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Dw</title> </head> <body> <c:forEach items="${userInfos}" var="user" > ${user.id} ${user.uname} </c:forEach> <form action="${pageContext.request.contextPath}/servlet/UploadHandleServlet" 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> <a href="${pageContext.request.contextPath}/servlet/ListFileServlet">下載列表</a> </body> </html>
A4:狀態頁面程式碼(message.jsp):
<%-- Created by IntelliJ IDEA. User: user Date: 2016/9/30 Time: 16:33 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> ${message} </body> </html>
如果你已經完成上邊步驟後,現在你可以測試一下上傳功能!
B1:下載列表顯示---Java程式碼(ListFileServlet):
package com.bailian.arch.amos.demo.servlet; /** * Created by user on 2016/10/9. */ import java.io.File; import java.io.IOException; import java.util.HashMap; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * @ClassName: ListFileServlet * @Description: 列出Web系統中所有下載檔案 * @author: * @date: 2016/10/9 * */ public class ListFileServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //獲取上傳檔案的目錄 String uploadFilePath = this.getServletContext().getRealPath("/WEB-INF/upload"); //儲存要下載的檔名 Map<String,String> fileNameMap = new HashMap<String,String>(); //遞迴遍歷filepath目錄下的所有檔案和目錄,將檔案的檔名儲存到map集合中 listfile(new File(uploadFilePath),fileNameMap);//File既可以代表一個檔案也可以代表一個目錄 //將Map集合傳送到listfile.jsp頁面進行顯示 request.setAttribute("fileNameMap", fileNameMap); request.getRequestDispatcher("/listfile.jsp").forward(request, response); } /** * @Method: listfile * @Description: 遞迴遍歷指定目錄下的所有檔案 * @Anthor: * @param file 即代表一個檔案,也代表一個檔案目錄 * @param map 儲存檔名的Map集合 */ public void listfile(File file,Map<String,String> map){ //如果file代表的不是一個檔案,而是一個目錄 if(!file.isFile()){ //列出該目錄下的所有檔案和目錄 File files[] = file.listFiles(); //遍歷files[]陣列 for(File f : files){ //遞迴 listfile(f,map); } }else{ /** * 處理檔名,上傳後的檔案是以uuid_檔名的形式去重新命名的,去除檔名的uuid_部分 file.getName().indexOf("_")檢索字串中第一次出現"_"字元的位置,如果檔名類似於:9349249849-88343-8344_阿_凡_達.avi 那麼file.getName().substring(file.getName().indexOf("_")+1)處理之後就可以得到阿_凡_達.avi部分 */ String realName = file.getName().substring(file.getName().indexOf("_")+1); //file.getName()得到的是檔案的原始名稱,這個名稱是唯一的,因此可以作為key,realName是處理過後的名稱,有可能會重複 map.put(file.getName(), realName); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
B2:下載列表顯示---web.xml檔案中註冊ListFileServlet:
<servlet> <servlet-name>ListFileServlet</servlet-name> <servlet-class>com.bailian.arch.amos.demo.servlet.ListFileServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>ListFileServlet</servlet-name> <url-pattern>/servlet/ListFileServlet</url-pattern> </servlet-mapping>
B3:下載列表顯示-jsp頁面程式碼(listfile.jsp):
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE HTML> <html> <head> <title>下載檔案顯示頁面</title> </head> <body> <!-- 遍歷Map集合 --> <c:forEach var="me" items="${fileNameMap}"> <c:url value="/servlet/DownLoadServlet" var="downurl"> <c:param name="filename" value="${me.key}"></c:param> </c:url> ${me.value}<a href="${downurl}">下2載</a> <br/> <br/> </c:forEach> </body> </html>
C1:下載功能---Java程式碼(DownLoadServlet):
package com.bailian.arch.amos.demo.servlet; /** * Created by user on 2016/10/9. */ import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.OutputStream; import java.net.URLEncoder; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class DownLoadServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //得到要下載的檔名 String fileName = request.getParameter("filename"); //23239283-92489-阿凡達.avi // 需要轉換的時候再轉換 fileName = new String(fileName.getBytes("iso8859-1"),"UTF-8"); //上傳的檔案都是儲存在/WEB-INF/upload目錄下的子目錄當中 String fileSaveRootPath=this.getServletContext().getRealPath("/WEB-INF/upload"); //通過檔名找出檔案的所在目錄 String path = findFileSavePathByFileName(fileName,fileSaveRootPath); //得到要下載的檔案 File file = new File(path + "\\" + fileName); //如果檔案不存在 if(!file.exists()){ request.setAttribute("message", "您要下載的資源已被刪除!!"); request.getRequestDispatcher("/message.jsp").forward(request, response); return; } //處理檔名 String realname = fileName.substring(fileName.indexOf("_")+1); //設定響應頭,控制瀏覽器下載該檔案 response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(realname, "UTF-8")); //讀取要下載的檔案,儲存到檔案輸入流 FileInputStream in = new FileInputStream(path + "\\" + fileName); //建立輸出流 OutputStream out = response.getOutputStream(); //建立緩衝區 byte buffer[] = new byte[1024]; int len = 0; //迴圈將輸入流中的內容讀取到緩衝區當中 while((len=in.read(buffer))>0){ //輸出緩衝區的內容到瀏覽器,實現檔案下載 out.write(buffer, 0, len); } //關閉檔案輸入流 in.close(); //關閉輸出流 out.close(); } /** * @Method: findFileSavePathByFileName * @Description: 通過檔名和儲存上傳檔案根目錄找出要下載的檔案的所在路徑 * @Anthor: * @param filename 要下載的檔名 * @param saveRootPath 上傳檔案儲存的根目錄,也就是/WEB-INF/upload目錄 * @return 要下載的檔案的儲存目錄 */ public String findFileSavePathByFileName(String filename,String saveRootPath){ int hashcode = filename.hashCode(); int dir1 = hashcode&0xf; //0--15 int dir2 = (hashcode&0xf0)>>4; //0-15 String dir = saveRootPath + "\\" + dir1 + "\\" + dir2; //upload\2\3 upload\3\5 File file = new File(dir); if(!file.exists()){ //建立目錄 file.mkdirs(); } return dir; } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
C2:下載功能web.xml檔案中註冊DownLoadServlet:
<servlet> <servlet-name>DownLoadServlet</servlet-name> <servlet-class>com.bailian.arch.amos.demo.servlet.DownLoadServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>DownLoadServlet</servlet-name> <url-pattern>/servlet/DownLoadServlet</url-pattern> </servlet-mapping>
C3:返回狀態頁面message.jsp
完成這些操作後,你就可以,測試下載功能了
觸發 “顯示下載列表” 的按鈕我放在上傳檔案頁面了。
溫馨提示:
在servlet中,不是有指定頁面跳轉嗎,直接用getRequestDispatcher跳轉的:
request.getRequestDispatcher("/message.jsp").forward(request, response);
這個方式跳轉的話,是訪問不到WEB-INF下邊的jsp檔案的,所以,用這種方式跳轉的檔案應該放在WEB-INF外邊。下邊是我的截圖,給新手看的:
(第一次進入上傳功能頁面我是用controller訪問進去的,所以,寫在WEB-INF裡邊了。)