文章詳情頁
jsp文件下載功能實現(xiàn)代碼
瀏覽:458日期:2022-06-07 09:14:49
本文實例為大家分享了jsp實現(xiàn)文件下載功能的3種方法,供大家參考,具體內(nèi)容如下
第一種、采用轉(zhuǎn)發(fā)的方式:
package cn.jbit.download.servlet; import java.io.IOException; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class DownloadServlet extends HttpServlet { private static final long serialVersionUID = 6765085208899952414L; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String filedownload = "/upload/1/10213.jpg";//即將下載的文件的相對路徑 String filedisplay = "10213.jpg";//下載文件時顯示的文件保存名稱 response.setContentType("application/x-download");//設(shè)置為下載application/x-download //response.setContentType("application/x-msdownload");//設(shè)置為下載application/x-msdownload //response.setContentType("application/octet-stream");//設(shè)置為下載application/octet-stream response.addHeader("Content-Disposition", "attachment;filename=" + filedisplay); try { RequestDispatcher rd = request.getRequestDispatcher(filedownload); if(rd != null) { rd.forward(request,response); } response.flushBuffer(); } catch (Exception e) { e.printStackTrace(); } } }
二、通過輸出流的方式:
package cn.jbit.download.servlet; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class DownloadOfIOServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String basePath = request.getSession().getServletContext().getRealPath("/upload"); String filedisplay = "helloworld.jpg"; String filedownload = basePath + File.separator + "helloworld.jpg"; response.setContentType("applicaiton/x-download"); response.addHeader("Content-Disposition", "attachment;filename="+filedisplay); InputStream is = null; OutputStream os = null; BufferedInputStream bis = null; BufferedOutputStream bos = null; is = new FileInputStream(new File(filedownload)); bis = new BufferedInputStream(is); os = response.getOutputStream(); bos = new BufferedOutputStream(os); byte[] b = new byte[1024]; int len = 0; while((len = bis.read(b)) != -1){ bos.write(b,0,len); } bis.close(); is.close(); bos.close(); os.close(); } }
第三種、通過超鏈接的方式(注意不推薦,因為會暴露下載文件的位置)
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持。
標(biāo)簽:
JSP
相關(guān)文章:
1. 基于javaweb+jsp實現(xiàn)企業(yè)財務(wù)記賬管理系統(tǒng)2. 詳解JSP 內(nèi)置對象request常見用法3. JSP+Servlet實現(xiàn)文件上傳到服務(wù)器功能4. jsp實現(xiàn)textarea中的文字保存換行空格存到數(shù)據(jù)庫的方法5. jsp+servlet實現(xiàn)猜數(shù)字游戲6. 關(guān)于IDEA使用jsp可以訪問頁面轉(zhuǎn)換為html彈出頁面為404的問題7. jsp EL表達(dá)式詳解8. 在JSP中使用formatNumber控制要顯示的小數(shù)位數(shù)方法9. jsp實現(xiàn)登錄驗證的過濾器10. JSP標(biāo)簽庫介紹
排行榜
