Struts2和Servlet下载文件的区别

本文提供了一段使用Servlet和Struts2实现文件下载的简单代码示例,包括设置响应头、获取文件输入流并将其输出至客户端。

1.Servlet下载简单代码


  1. protected void service(HttpServletRequest req, HttpServletResponse resp)  
  2.             throws ServletException, IOException {  
  3.         resp.setHeader("Cache-Control""no-cache");  
  4.         resp.setContentType("application/vnd.ms-excel");  
  5.         resp.setHeader("Content-Disposition""attachment; filename=file.xls");  
  6.         OutputStream os = resp.getOutputStream();  
  7.         FileInputStream in = new FileInputStream(new File("xxxxx"));  
  8.         int n = 0;// 每次读取的字节长度  
  9.         byte[] bb = new byte[1024];// 存储每次读取的内容  
  10.         while ((n = in.read(bb)) != -1) {  
  11.             os.write(bb, 0, n);// 将读取的内容,写入到输出流当中  
  12.         }  
  13.         os.close();// 关闭输入输出流  
  14.         in.close();  
  15.     }  

2.Stuts2下载简单代码

  1. public class TestAction {  
  2.     private InputStream excelStream;  
  3.     private String filename;  
  4.       
  5.     /** 
  6.      * 物理存在的excel文件 
  7.      * @return 
  8.      * @throws UnsupportedEncodingException 
  9.      */  
  10.     public String down() throws UnsupportedEncodingException{  
  11.         File file = new File(  
  12.             "xxxx");//文件路径  
  13.         try {  
  14.             excelStream=new FileInputStream(file);  
  15.             filename=new String("真实excel文件".getBytes("UTF-8"),"ISO8859-1");  
  16.         } catch (FileNotFoundException e) {  
  17.             e.printStackTrace();  
  18.         }  
  19.         return "success";  
  20.     }  
  21.     /** 
  22.      * @return the excelStream 
  23.      */  
  24.     public InputStream getExcelStream() {  
  25.         return excelStream;  
  26.     }  
  27.    
  28.     /** 
  29.      * @param excelStream the excelStream to set 
  30.      */  
  31.     public void setExcelStream(InputStream excelStream) {  
  32.         this.excelStream = excelStream;  
  33.     }  
  34.    
  35.     /** 
  36.      * @return the filename 
  37.      */  
  38.     public String getFilename() {  
  39.         return filename;  
  40.     }  
  41.    
  42.     /** 
  43.      * @param filename the filename to set 
  44.      */  
  45.     public void setFilename(String filename) {  
  46.         this.filename = filename;  
  47.     }  
  48.       
  49.  }  
struts.xml配置文件:
  1. <action name="down" class="com.techbirds.action.TestAction" method="down">  
  2.     <result name="success" type="stream">  
  3.         <param name="contentType">application/vnd.ms-excel   </param>  
  4.         <param name="inputName">excelStream</param>  
  5.         <param name="contentDisposition">filename="${filename}.xls"   </param>  
  6.         <param name="bufferSize">1024</param>  
  7.  <span style="white-space:pre"> </span></result>  
  8.  </action>  

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值