随笔-11  评论-0  文章-2  trackbacks-0
  1. public void DownloadFileManager(String filename, String url)   
  2.             throws IOException {   
  3.         HttpServletResponse response = Struts2Utils.getResponse();   
  4.         HttpServletRequest request = Struts2Utils.getRequest();   
  5.         InputStream is = null;   
  6.         OutputStream os = null;   
  7.         try {   
  8.              String agent = (String)request.getHeader("USER-AGENT");     
  9.               if(agent != null && agent.indexOf("MSIE") == -1) {     
  10.               String enableFileName = "=?UTF-8?B?" + (new String (Base64.encodeBase64(filename.getBytes("UTF-8")))) + "?=";     
  11.               response.setHeader("Content-Disposition""attachment; filename=" + enableFileName);     
  12.               }else {     
  13.                // IE     
  14.              String enableFileName = new String(filename.getBytes("GBK"), "ISO-8859-1");     
  15.                response.setHeader("Content-Disposition""attachment; filename=" + enableFileName);     
  16.               }    
  17.   
  18.     /*       response.setHeader("Content-Disposition", "attachment;filename="  
  19.                     + new String(filename.getBytes("gbk"), "iso-8859-1")); */  
  20.             
  21.             
  22.             response.setContentType("application/x-msdownload");   
  23.             is = new FileInputStream(url);   
  24.             os = response.getOutputStream();   
  25.             byte[] buffer = new byte[21940];   
  26.             int length = 0;   
  27.             while ((length = is.read(buffer)) > 0) {   
  28.                 os.write(buffer, 0, length);   
  29.             }   
  30.   
  31.         } catch (Exception e) {   
  32.             e.printStackTrace();   
  33.         } finally {   
  34.             if (os != null) {   
  35.                 os.close();   
  36.             }   
  37.             if (is != null) {   
  38.                 is.close();   
  39.             }   
  40.         }   
  41.   
  42.     }
posted on 2012-07-07 00:40 阅读(76) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: