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页面的静态包含和动态包含使用方法