struts2 下載 解決IE,火狐下載亂碼

one:

Struts.xml

<result name="success" type="stream"><!-- ;charset=UTF-8  ;charset=ISO-8859-1-->
    <param name="contentType">application/octet-stream</param>   
    <param name="contentDisposition">attachment;filename=${fileName}</param>
    <param name="inputName">fileinputstream</param>
    <param name="bufferSize">4096</param>
   </result>

two:

 private FileInputStream fileinputstream;

 private String fileName;

/**
  * 下载文件
  */
 public InputStream getFileinputstream(){
//  ServletActionContext.getResponse().setContentType("application/octet-stream;charset=UTF-8");
  try {


   String s=(String) Constants.SIDECODE_LIST[Constants.SIDECODE_IN_STATE][2];
   String url=this.getFileUrl().replace("http://www.jiwu.com/", s);  //這裡你可以自己配置路徑


   fileinputstream = new FileInputStream(new File(url));
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return fileinputstream;
 }

 public void setFileinputstream(FileInputStream fileinputstream) {
  this.fileinputstream = fileinputstream;
 }

/**
  * 防止IE,火狐乱码
  * @return 
  */

 public String getFileName() {
  if(fileName!=null){
   try {
    HttpServletRequest request = ServletActionContext.getRequest();   
          String Agent = request.getHeader("User-Agent");   
          if (null != Agent) {   
              Agent = Agent.toLowerCase();   
              if (Agent.indexOf("firefox") != -1) {   
               fileName = new String(fileName.getBytes(),"ISO-8859-1");   
//               fileName = StringUtils.replace(fileName, " ", "%20");
              } else if (Agent.indexOf("msie") != -1) {   
               fileName = java.net.URLEncoder.encode(fileName,"UTF-8");  
               fileName = StringUtils.replace(fileName, "+", "%20"); 
              } else {   
               fileName = java.net.URLEncoder.encode(fileName,"UTF-8");   
               fileName = StringUtils.replace(fileName, "+", "%20"); 
              }   
          } 

   } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }  
   return fileName;
  }
  return "";
 }
 public void setFileName(String fileName) {
  this.fileName = fileName;
 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值