现在的位置: 首页 > 综合 > 正文

三种jsp下载文件的方法

2017年12月19日 ⁄ 综合 ⁄ 共 2240字 ⁄ 字号 评论关闭

 

  1. response.setContentType("application/x-download");//设置为下载application/x-download  
  2. String filedownload = "/要下载的文件名";//即将下载的文件的相对路径  
  3. String filedisplay = "最终要显示给用户的保存文件名";//下载文件时显示的文件保存名称  
  4. String filenamedisplay = URLEncoder.encode(filedisplay,"UTF-8");  
  5. response.addHeader("Content-Disposition","attachment;filename=" + filedisplay);  
  6.    
  7. try  
  8. {  
  9. RequestDispatcher dis = application.getRequestDispatcher(filedownload);  
  10. if(dis!= null)  
  11. {  
  12. dis.forward(request,response);  
  13. }  
  14. response.flushBuffer();  
  15. }  
  16. catch(Exception e)  
  17. {  
  18. e.printStackTrace();  
  19. }  
  20. finally  
  21. {  
  22.    

 

 

  1. <%@page language="java" contentType="application/x-msdownload" pageEncoding="gb2312"%> 
  2. <%  
  3.   //关于文件下载时采用文件流输出的方式处理:  
  4.   //加上response.reset(),并且所有的%>后面不要换行,包括最后一个;  
  5.  
  6.   response.reset();//可以加也可以不加  
  7.   response.setContentType("application/x-download");  
  8.  
  9. //application.getRealPath("/main/mvplayer/CapSetup.msi");获取的物理路径  
  10.  
  11. String filedownload = "想办法找到要提供下载的文件的物理路径+文件名";  
  12.  String filedisplay = "给用户提供的下载文件名";  
  13.   String filedisplay = URLEncoder.encode(filedisplay,"UTF-8");  
  14.   response.addHeader("Content-Disposition","attachment;filename=" + filedisplay);  
  15.  
  16.   java.io.OutputStream outp = null;  
  17.   java.io.FileInputStream in = null;  
  18.   try  
  19.   {  
  20.   outp = response.getOutputStream();  
  21.   in = new FileInputStream(filenamedownload);  
  22.  
  23.   byte[] b = new byte[1024];  
  24.   int i = 0;  
  25.  
  26.   while((i = in.read(b)) > 0)  
  27.   {  
  28.   outp.write(b, 0, i);  
  29.   }  
  30. //    
  31. outp.flush();  
  32. //要加以下两句话,否则会报错  
  33. //java.lang.IllegalStateException: getOutputStream() has already been called for //this response    
  34. out.clear();  
  35. out = pageContext.pushBody();  
  36. }  
  37.   catch(Exception e)  
  38.   {  
  39.   System.out.println("Error!");  
  40.   e.printStackTrace();  
  41.   }  
  42.   finally  
  43.   {  
  44.   if(in != null)  
  45.   {  
  46.   in.close();  
  47.   in = null;  
  48.   }  
  49. //这里不能关闭    
  50. //if(outp != null)  
  51.   //{  
  52.   //outp.close();  
  53.   //outp = null;  
  54.   //}  
  55.   }  
  56. %> 

 

 

 

  1. <%@ page language="java" import="java.util.*" pageEncoding="GBK"%> 
  2. <a href="do_download.jsp?url=xxxxxx">点击下载 千千动听</a> 
  3.  
  4.  
  5.  
  6. <%@ page language="java" import="java.util.*" pageEncoding="GBK"%> 
  7. <%@page import="com.jspsmart.upload.SmartUpload"%> 
  8.  
  9.  
  10. <%  
  11.       
  12.     SmartUpload su=new SmartUpload();  
  13.     su.initialize(pageContext);  
  14.     su.setContentDisposition(null);//禁止浏览器打开文件 只能下载  
  15.     su.downloadFile("upload/1.txt");  
  16.     //out.clear();  
  17.     //out=pageContext.pushBody();  
  18.  %> 

 

抱歉!评论已关闭.