jsp文件下載功能實(shí)現(xiàn)代碼
本文實(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í)有所幫助,也希望大家多多支持碩編程。
相關(guān)文章
- jsp+servlet實(shí)現(xiàn)文件上傳與下載功能
- EJB3.0部署消息驅(qū)動(dòng)Bean拋javax.naming.NameNotFoundException異常
- 在JSP中使用formatNumber控制要顯示的小數(shù)位數(shù)方法
- 秒殺系統(tǒng)Web層設(shè)計(jì)的實(shí)現(xiàn)方法
- 將properties文件的配置設(shè)置為整個(gè)Web應(yīng)用的全局變量實(shí)現(xiàn)方法
- JSP使用過濾器防止Xss漏洞
- 在JSP頁(yè)面中動(dòng)態(tài)生成圖片驗(yàn)證碼的方法實(shí)例
- 詳解JSP 內(nèi)置對(duì)象request常見用法
- 使用IDEA編寫jsp時(shí)EL表達(dá)式不起作用的問題及解決方法
- jsp實(shí)現(xiàn)局部刷新頁(yè)面、異步加載頁(yè)面的方法
- Jsp中request的3個(gè)基礎(chǔ)實(shí)踐
- JavaServlet的文件上傳和下載實(shí)現(xiàn)方法
- JSP頁(yè)面的靜態(tài)包含和動(dòng)態(tài)包含使用方法