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

one:java

Struts.xmlapp

<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>this

two:url

 private FileInputStream fileinputstream;.net

 private String fileName;firefox

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


   String s=(String) Constants.SIDECODE_LIST[Constants.SIDECODE_IN_STATE][2];
   String url=this.getFileUrl().replace("http://www.jiwu.com/", s);  //這裡你能夠本身配置路徑xml


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

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

/**
  * 防止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;  }

相關文章
相關標籤/搜索