Javaweb之實現文件上傳+點擊下載功能
Javaweb之實現文件上傳+點擊下載功能
實現目標:
1、編寫index.jsp,可以進行文件上傳
2、上傳成功后跳回index.jsp,并把上傳的文件名顯示在index.jsp中(用a標簽顯示)。
3、點擊文件名,可下載文件
準備工具:
IntelliJ IDEA 2019.2.3 x64
apache-tomcat-8.5.15
JSP?標準標簽庫(JSTL)
首先,簡單編寫Index,jsp的上傳頁面
<%--?Created?by?IntelliJ?IDEA.?--%> <%@?page?contentType="text/html;charset=UTF-8"?language="java"?%> ??
????再來編寫UploadServlet.java的實現上傳的IO
package?com.blb.servlet; import?javax.servlet.ServletException; import?javax.servlet.annotation.MultipartConfig; import?javax.servlet.annotation.WebServlet; import?javax.servlet.http.HttpServlet; import?javax.servlet.http.HttpServletRequest; import?javax.servlet.http.HttpServletResponse; import?javax.servlet.http.Part; import?java.io.*; import?java.nio.file.Path; //支持二進制處理 @MultipartConfig @WebServlet("/upload")?//映射路徑 public?class?UploadServlet?extends?HttpServlet?{ ????String?path="d:/upload/";??//上傳的路徑?這里我設置的是d盤的upload文件夾下 ????@Override ????protected?void?service(HttpServletRequest?req,?HttpServletResponse?resp)?throws?ServletException,?IOException?{ ???? ????????Part?file?=?req.getPart("file"); ????????//獲取提交的文件名 ????????String?fileName?=?file.getSubmittedFileName(); ????????//獲取文件的輸入流 ????????InputStream?inputStream?=?file.getInputStream(); ????????//獲取的文件傳到path路徑 ????????FileOutputStream?fileOutputStream?=?new?FileOutputStream(path+fileName); ????????byte[]?data?=?new?byte[1024]; ????????int?len; ????????while?((len=inputStream.read(data))!=-1){???//?表示從InputStream中讀取一個數組的數據,如果返回-1?則表示數據讀取完成了。 ????????????fileOutputStream.write(data,0,len); ????????????//循環讀取數據 ????????} ???????inputStream.close(); ??????fileOutputStream.close();??//關閉IO流 ????} }
檢查一下,沒什么錯誤,試著看看效果
選擇一個圖片文件
點擊上傳后
檢查D:/upload下是否有這個文件
證明咱們這個邏輯沒問題哈,已經能實現上傳到指定路徑了
接著實現上傳成功后跳回index.jsp,并把上傳的文件名顯示在index.jsp中(用a標簽顯示)。
這里先導入一個jstl的jsp
一定要在WEB-INF下導入,然后右鍵jstl選擇 as a libray
導入完成后在jsp頭部加入一個引入標簽,然后在主體里加入forEach遍歷這個上傳的文件list 顯示在瀏覽器的頁面上
<%--?Created?by?IntelliJ?IDEA.?--%> <%@?page?contentType="text/html;charset=UTF-8"?language="java"?%> <%@?taglib?prefix="c"?uri="http://java.sun.com/jsp/jstl/core"?%>?//jstl引入標簽 ??
????文件列表
????//上傳文件的名字的顯示在a標簽內 ??
接著寫實現點擊a標簽下載上傳后的文件的功能代碼DownloadServlet.java
package?com.blb.servlet; import?javax.servlet.ServletException; import?javax.servlet.ServletOutputStream; import?javax.servlet.annotation.MultipartConfig; import?javax.servlet.annotation.WebServlet; import?javax.servlet.http.HttpServlet; import?javax.servlet.http.HttpServletRequest; import?javax.servlet.http.HttpServletResponse; import?javax.xml.ws.Service; import?java.io.FileInputStream; import?java.io.IOException; @MultipartConfig @WebServlet("/download") public?class?DownloadServlet??extends?HttpServlet?{ ????String?path="D:/"; ????@Override ????protected?void?service(HttpServletRequest?req,?HttpServletResponse?resp)?throws?ServletException,?IOException?{ ????????//要下載的文件名 ????????String?file?=?req.getParameter("file"); ????????//取得文件輸入流 ????????FileInputStream?fileInputStream?=?new?FileInputStream(path?+?file); ???????? ????????//獲得輸出流 ????????ServletOutputStream?outputStream?=?resp.getOutputStream(); ????????//將文件名轉碼 ????????String?fileName?=?new?String(file.getBytes(),?"iso-8859-1"); ????????resp.setContentType("application/octet-stream");????//告訴瀏覽器我這是一個二進制文件 ????????resp.setHeader("Content-Disposition",?"attachment;filename="?+?fileName);???//告訴瀏覽器文件名是什么 ????????resp.setContentLength(fileInputStream.available());?????????//告訴瀏覽器文件的大小是多少 ????????//IO流寫操作,向客戶端寫內容 ????????byte[]?data?=?new?byte[1024]; ????????int?len; ????????while?((len=fileInputStream.read(data))!=-1){ ????????????outputStream.write(data,?0,?len); ????????} ????????fileInputStream.close(); ????????outputStream.close(); ????} }
接著來試試效果
nice,基本的效果都已經實現了,但是還是要優化之處,比如:
1、閱讀IOUtis,把讀寫操作進行封裝起來方便調用
package?com.blb.utils; import?javax.servlet.http.HttpServletResponse; import?java.io.IOException; import?java.io.InputStream; import?java.io.OutputStream; import?java.io.UnsupportedEncodingException; public?class?IOUtils?{ ????/** ?????*?輸入輸出流讀取操作封裝 ?????*?@param?inputStream ?????*?@param?outputStream ?????*?@throws?IOException ?????*/ ????public?static?void?readAndWriter(InputStream?inputStream,?OutputStream?outputStream)?throws?IOException?{ ????????byte[]?data?=?new?byte[1024]; ????????int?len; ????????while?((len=inputStream.read(data))!=-1){ ????????????outputStream.write(data,?0,?len); ????????} ????????inputStream.close(); ????????outputStream.close(); ????} ????/** ?????*?下載配置 ?????*?@param?resp ?????*?@param?file ?????*?@param?length ?????*?@throws?UnsupportedEncodingException ?????*/ ????public?static?void?downloadConfig(HttpServletResponse?resp,?String?file,?int?length)?throws?UnsupportedEncodingException?{ ????????String?fileName?=?new?String(file.getBytes(),?"iso-8859-1"); ????????resp.setContentType("application/octet-stream"); ????????resp.setContentLength(length); ????????resp.setHeader("Content-Disposition",?"attachment;filename="?+?fileName?); ????} }
2、下載前先判斷文件是否存在
在DownloadServlet里添加如下代碼:
if?(!file1.exists())?{ ????req.setAttribute("msg",?"文件不存在"); ????req.getRequestDispatcher("index.jsp").forward(req,?resp); ????return; }?????//判斷文件是否存在
然后在index.jsp里加一個綁定數據${msg}實現傳遞
文件列表
${msg}3、服務器重啟后,文件列表丟失問題
第一次進來時,文件列表不能正常顯示問題
package?com.blb.servlet; 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?java.io.File; import?java.io.IOException; @WebServlet("/index") public?class?IndexServlet?extends?HttpServlet?{ //????String?path?=?"D:/upload/"; ????@Override ????protected?void?service(HttpServletRequest?req,?HttpServletResponse?resp)?throws?ServletException,?IOException?{ ????????File?dir?=?new?File("D:/upload/"); ????????String[]?list?=?dir.list(); ????????//把存放文件名集合的list放到session作用域中 ????????req.getSession().setAttribute("files",?list); ????????resp.sendRedirect("index.jsp");?//重定向 ????} }
最后的最后,這個小項目到這里結束了,歡迎大家指正??!
Java JSP
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。