使用struts2导出excel,
struts.xml文件中这样配置的文件名:<action name="exportexcel" class="BeenChina.PM.Action.CaseNumberEfficiencyQuery">
    <result name="success" type="stream">
        ...
        <param name="contentDisposition">attachment;filename="${fileName}.xls"</param>
        ...
    </result>
        </action>
Action的fileName属性的get方法:public String getFileName() throws UnsupportedEncodingException {
    fileName=new String(fileName.getBytes(),"ISO8859-1");
    return fileName;
}public void setFileName(String fileName) throws UnsupportedEncodingException {
    this.fileName=fileName;
}
现在的问题是:为什么要用ISO8859-1才可以使下载文件名不是乱码,而UTF-8却不行?

解决方案 »

  1.   

    iso-8859-1是JAVA网络传输使用的标准字符集
      

  2.   

    这个是跟你tomcat配置的传输编码格式有关系的。
    在tomcat的安装目录下找到
    server.xml。
    找到下面这段:看看你那里面设置的是什么。
      <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8" />
      <Connector port="8009" protocol="AJP/1.3" redirectPort="8443"  URIEncoding="UTF-8"/>
      

  3.   


    我的默认配置是这样的:
    <Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443"/>
      

  4.   


    我的默认配置是这样的:
    <Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443"/>
    没加的话默认就是ISO,你加上的话就等于修改了默认编码格式。比如加上UTF-8后那么就是UTF-8传输了。
      

  5.   


    我的默认配置是这样的:
    <Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443"/>
    没加的话默认就是ISO,你加上的话就等于修改了默认编码格式。比如加上UTF-8后那么就是UTF-8传输了。我发现用ISO8859-1,firfox和Chrome好用,IE却找不到文件了。
      

  6.   

    最终的解决办法是这样的:public   static  String encode(HttpServletRequest request, String fileName)  throws  UnsupportedEncodingException {  
        String agent = request.getHeader("USER-AGENT" );  
        if( null  != agent && - 1  != agent.indexOf( "MSIE" )) {  
            return  URLEncoder.encode(fileName,  "UTF8" );  
        }
        else if  ( null  != agent && - 1  != agent.indexOf( "Firefox" )) {  
            return   "=?UTF-8?B?" +( new  String(Base64.encodeBase64(fileName.getBytes( "UTF-8" ))))+ "?=" ;  
        } 
        else if  ( null  != agent && - 1  != agent.indexOf( "Chrome" )) { 
            return  new String(fileName.getBytes(), "ISO8859-1");         
        }  
        else{
            return fileName;
        }
    }   
    好用是好用了,不知道这样做合适不?