jsp文件下載功能實現代碼
本文實例為大家分享了jsp實現文件下載功能的3種方法,供大家參考,具體內容如下
第一種、采用轉發的方式:
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");//設置為下載application/x-download //response.setcontenttype("application/x-msdownload");//設置為下載application/x-msdownload //response.setcontenttype("application/octet-stream");//設置為下載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(); } }
第三種、通過超鏈接的方式(注意不推薦,因為會暴露下載文件的位置)
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持碩編程。
相關文章
- jsp+servlet實現文件上傳與下載功能
- EJB3.0部署消息驅動Bean拋javax.naming.NameNotFoundException異常
- 在JSP中使用formatNumber控制要顯示的小數位數方法
- 秒殺系統Web層設計的實現方法
- 將properties文件的配置設置為整個Web應用的全局變量實現方法
- JSP使用過濾器防止Xss漏洞
- 在JSP頁面中動態生成圖片驗證碼的方法實例
- 詳解JSP 內置對象request常見用法
- 使用IDEA編寫jsp時EL表達式不起作用的問題及解決方法
- jsp實現局部刷新頁面、異步加載頁面的方法
- Jsp中request的3個基礎實踐
- JavaServlet的文件上傳和下載實現方法
- JSP頁面的靜態包含和動態包含使用方法