Struts2文件下载中文乱码解决

  1. 设置Tomcat编码格式为UTF-8:修改tomcat-->conf-->server.xml文件,设置URIEncoding为UTF-8,不设置时默认值为ISO8859-1

    <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"/>

  2. 项目及Java文件,JSP文件格式当然也要是UTF-8

  3. java代码

    /**
         * 设置下载文件名
         * @param downloadFileName
         */
        public void setDownloadFileName(String downloadFileName) {
            try {
                HttpServletRequest request=ServletActionContext.getRequest();
                if (request.getHeader("User-Agent").toLowerCase()
                        .indexOf("firefox") > 0) {
                    this.downloadFileName = "=?UTF-8?B?"
                            + (new String(org.apache.commons.codec.binary.Base64.encodeBase64(downloadFileName
                                    .getBytes("UTF-8")))) + "?=";
                } else {
                    this.downloadFileName = java.net.URLEncoder.encode(
                            downloadFileName, "UTF-8");
                }
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }

你可能感兴趣的:(文件下载,struts2,中文乱码)