文章詳情頁
jsp文件下載功能實(shí)現(xiàn)代碼
瀏覽:607日期:2022-06-07 09:14:49
本文實(shí)例為大家分享了jsp實(shí)現(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";//即將下載的文件的相對(duì)路徑 String filedisplay = "10213.jpg";//下載文件時(shí)顯示的文件保存名稱 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(); } }
第三種、通過超鏈接的方式(注意不推薦,因?yàn)闀?huì)暴露下載文件的位置)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持。
標(biāo)簽:
JSP
相關(guān)文章:
1. 詳解Django關(guān)于StreamingHttpResponse與FileResponse文件下載的最優(yōu)方法2. JSP+Servlet實(shí)現(xiàn)文件上傳到服務(wù)器功能3. 如何在jsp界面中插入圖片4. 基于javaweb+jsp實(shí)現(xiàn)企業(yè)財(cái)務(wù)記賬管理系統(tǒng)5. 使用IDEA編寫jsp時(shí)EL表達(dá)式不起作用的問題及解決方法6. jsp網(wǎng)頁實(shí)現(xiàn)貪吃蛇小游戲7. JSP使用過濾器防止Xss漏洞8. JSP實(shí)現(xiàn)彈出登陸框以及陰影效果9. Jsp servlet驗(yàn)證碼工具類分享10. JSP中param動(dòng)作的實(shí)例詳解
排行榜
