jsp中文件下载的实现

方式一:采用RequestDispatcher进行

[java] view plaincopy 在CODE上查看代码片派生到我的代码片
  1. package cn.jbit.download.servlet;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import javax.servlet.RequestDispatcher;  
  6. import javax.servlet.ServletException;  
  7. import javax.servlet.http.HttpServlet;  
  8. import javax.servlet.http.HttpServletRequest;  
  9. import javax.servlet.http.HttpServletResponse;  
  10.   
  11. public class DownloadServlet extends HttpServlet {  
  12.   
  13.     private static final long serialVersionUID = 6765085208899952414L;  
  14.   
  15.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  16.             throws ServletException, IOException {  
  17.         doPost(request, response);  
  18.     }  
  19.   
  20.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  21.             throws ServletException, IOException {  
  22.         String filedownload = "/upload/helloworld.jar";//即将下载的文件的相对路径  
  23.         String filedisplay = "helloworld.jar";//下载文件时显示的文件保存名称  
  24.         response.setContentType("application/x-download");//设置为下载application/x-download  
  25.         //response.setContentType("application/x-msdownload");//设置为下载application/x-msdownload  
  26.         //response.setContentType("application/octet-stream");//设置为下载application/octet-stream  
  27.         response.addHeader("Content-Disposition""attachment;filename="  
  28.                 + filedisplay);  
  29.           
  30.         try {  
  31.             RequestDispatcher rd = request.getRequestDispatcher(filedownload);  
  32.             if(rd != null)  
  33.             {  
  34.                 rd.forward(request,response);  
  35.             }  
  36.             response.flushBuffer();  
  37.         } catch (Exception e) {  
  38.             e.printStackTrace();  
  39.         }  
  40.     }  
  41. }  
方式二:采用文件流输出的方式下载

[java] view plaincopy 在CODE上查看代码片派生到我的代码片
  1. package cn.jbit.download.servlet;  
  2.   
  3. import java.io.BufferedInputStream;  
  4. import java.io.BufferedOutputStream;  
  5. import java.io.File;  
  6. import java.io.FileInputStream;  
  7. import java.io.IOException;  
  8. import java.io.InputStream;  
  9. import java.io.OutputStream;  
  10.   
  11. import javax.servlet.ServletException;  
  12. import javax.servlet.http.HttpServlet;  
  13. import javax.servlet.http.HttpServletRequest;  
  14. import javax.servlet.http.HttpServletResponse;  
  15.   
  16. public class DownloadOfIOServlet extends HttpServlet {  
  17.   
  18.     private static final long serialVersionUID = 6765085208899952414L;  
  19.   
  20.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  21.             throws ServletException, IOException {  
  22.         doPost(request, response);  
  23.     }  
  24.       
  25.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  26.             throws ServletException, IOException {  
  27.         String basePath = request.getSession().getServletContext().getRealPath("/upload");  
  28.         //System.out.println(basePath);  
  29.         String filedisplay = "helloworld.jar";  
  30.         String filedownload = basePath + File.separator + "helloworld.jar";  
  31.         System.out.println("----------------------"+filedownload);  
  32.         response.setContentType("applicaiton/x-download");  
  33.         response.addHeader("Content-Disposition""attachment;filename="+filedisplay);  
  34.           
  35.         InputStream is = null;  
  36.         OutputStream os = null;  
  37.         BufferedInputStream bis = null;  
  38.         BufferedOutputStream bos = null;  
  39.           
  40.         is = new FileInputStream(new File(filedownload));  
  41.         bis = new BufferedInputStream(is);  
  42.         os = response.getOutputStream();  
  43.         bos = new BufferedOutputStream(os);  
  44.           
  45.         byte[] b = new byte[1024];  
  46.         int len = 0;  
  47.         while((len = bis.read(b)) != -1){  
  48.             bos.write(b,0,len);  
  49.         }  
  50.           
  51.         bis.close();  
  52.         is.close();  
  53.         bos.close();  
  54.         os.close();  
  55.     }  
  56. }  
方式三:网页上做超级链接(不推荐使用,样服务器上的目录资源会直接暴露给最终用户)

[html] view plaincopy 在CODE上查看代码片派生到我的代码片
  1. <a href="/DownloadFile/upload/helloworld.jar">helloworld.jara>  
jsp页面代码:

[html] view plaincopy 在CODE上查看代码片派生到我的代码片
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6.   
  7. >  
  8. <html>  
  9.   <head>  
  10.     <base href="<%=basePath%>">  
  11.     <title>My JSP 'download.jsp' starting pagetitle>  
  12.     <meta http-equiv="pragma" content="no-cache">  
  13.     <meta http-equiv="cache-control" content="no-cache">  
  14.     <meta http-equiv="expires" content="0">      
  15.   head>  
  16.     
  17.   <body>  
  18.     下载1<a href="/DownloadFile/upload/helloworld.jar">helloworld.jara><br/>  
  19.     下载2<a href="/DownloadFile/servlet/downloadServlet">helloworld.jara><br/>  
  20.     下载3<a href="/DownloadFile/servlet/downloadOfIOServlet">helloworld.jara><br/>  
  21.     下载4<a href="/DownloadFile/download/filedownload.action">helloworld.jara><br/>  
  22.   body>  
  23. html>  


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部