文章詳情頁
jsp文件下載功能實現(xiàn)代碼
瀏覽:582日期: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. JSP開發(fā)之hibernate之單向多對一關(guān)聯(lián)的實例2. JSP Tag Library-AjaxTags 1.0, released3. JSP生成九九乘法表的簡單實例4. JSP學(xué)生信息管理系統(tǒng)設(shè)計5. 基于javaweb+jsp實現(xiàn)企業(yè)車輛管理系統(tǒng)6. html與jsp開發(fā)分離技術(shù)7. jsp 實現(xiàn)的簡易mvc模式示例8. Jsp+Servlet實現(xiàn)文件上傳下載 文件列表展示(二)9. Jsp servlet驗證碼工具類分享10. JSP頁面的靜態(tài)包含和動態(tài)包含使用方法
排行榜
