jsf 之文件下载 的关键

  1. package demo;
  2.  
  3.  
  4. import java. io. BufferedInputStream;
  5. import java. io. BufferedOutputStream;
  6. import java. io. File;
  7. import java. io. FileInputStream;
  8. import java. io. IOException;
  9. import java. io. InputStream;
  10. import java. io. UnsupportedEncodingException;
  11. import java. net. URLDecoder;
  12. import java. util. logging. Logger;
  13.  
  14. import javax. faces. application. Application;
  15. import javax. faces. context. FacesContext;
  16. import javax. faces. event. ActionEvent;
  17. import javax. servlet. ServletOutputStream;
  18. import javax. servlet. http. HttpServletResponse;
  19.  
  20. import org. operamasks. faces. annotation. ManagedBean;
  21. import org. operamasks. faces. annotation. ManagedBeanScope;
  22. public class DownFile { 
  23.   public String download ( ) {
  24.     //OaMailAttach oaMailAttach = getOaMailAttach();
  25.  
  26.     String path= "G:\sun\";
  27.    String fileName=" 20074111452140. doc ";
  28.  
  29.     try {
  30.     FacesContext ctx = FacesContext.getCurrentInstance();
  31.     ctx.responseComplete();
  32.     //String contentType = "application/octet-stream;charset=utf -8 ";
  33.     String contentType = "application/x-download ";
  34.     HttpServletResponse response = (HttpServletResponse) ctx.getExternalContext().getResponse();
  35.  
  36.  
  37.  
  38.     response.setContentType(contentType);
  39.  
  40.     StringBuffer contentDisposition = new StringBuffer();
  41.  
  42.     contentDisposition.append("attachment; ");
  43.  
  44.     contentDisposition.append("filename=\ "" );
  45.     contentDisposition. append (fileName );
  46.     contentDisposition. append ( "\"" );
  47.      //log.debug(System.getProperty("file.encoding"));
  48.     response. setHeader ( "Content-Disposition", new String (contentDisposition. toString ( ). getBytes ( System. getProperty ( "file.encoding" ) ), "iso8859_1" ) );
  49.     // log.debug(contentDisposition.toString());
  50.     ServletOutputStream out = response. getOutputStream ( );
  51.  
  52.      //log.debug(new Long(oaMailAttach.getAtFile().length()));
  53.  
  54.      byte [ ] bytes = new byte [0xffff ];
  55.      InputStream is = new FileInputStream ( new File (path + fileName ) );
  56.      int b = 0;
  57.      while ( (b = is. read (bytes, 0, 0xffff ) ) > 0 ) {
  58.     out. write (bytes, 0, b );
  59.      }
  60.     is. close ( );
  61.     out. flush ( );
  62.     ctx. responseComplete ( );
  63.     } catch ( Exception e ) {
  64.      // TODO 自动生成 catch 块
  65.     e. printStackTrace ( );
  66.     }
  67.     return null;
  68.     }
  69.  
  70. }

关键语句:ctx.responseComplete();

不然会报错误如下:

Servlet response already use stream, Writer not possible

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值