今天用struts2写文件的下载,出现了文件名为中文时有乱码的问题。参考了很多网上和书上的方法但是不奏效。以下是我的解决方法,虽然很简单直白,但是解决问题了,供大家参考。


public String getDownFileName() throws UnsupportedEncodingException {
        return java.net.URLDecoder.decode(downFileName, "utf-8");
    }
    public void setDownFileName(String downFileName) throws UnsupportedEncodingException {
//      this.downFileName = new String(downFileName.getBytes("utf-8"),"iso-8859-1");
        this.downFileName = java.net.URLEncoder.encode(downFileName, "utf-8");
    }


其实就是先进行utf-8编码,然后再utf-8解码。我理解是因为服务器端和客户端的编码方式不一样,所有要进行统一的编码