2 mx 14 mx_14 于 2015.05.27 21:29 提问

struts2的xml配置下载文件时文件名乱码

图片说明
图片说明
这里写上中文名乱码了,我可以咋解决呢?谢谢大家的指点!

3个回答

guwei4037
guwei4037   Ds   Rxr 2015.05.27 21:41
已采纳
mx_14
mx_14   2015.05.27 22:22

非常谢谢你给的链接,我这个问题都一天多了,我刚才看了那个链接,然后试着弄到我的项目中,完美的解决了问题,非常感谢!

mx_14
mx_14   2015.05.27 22:29

附上我的解决方法:

public class DownFileAction {

    private String fileName;//由页面传递过来

    public String execute() throws UnsupportedEncodingException{
        return "success";
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) throws UnsupportedEncodingException {
        this.fileName = new String(fileName.getBytes("ISO-8859-1"),"UTF-8");
    }

    //获取输入流
    public InputStream getInputStream() throws FileNotFoundException, UnsupportedEncodingException {

        String rootPath = ServletActionContext.getRequest().getSession().getServletContext().getRealPath("upload");

        HttpServletResponse response = ServletActionContext.getResponse();
        response.setHeader("Content-Disposition", "attachment;fileName="+java.net.URLEncoder.encode(fileName,"UTF-8"));
       return ServletActionContext.getServletContext().getResourceAsStream(rootPath+"\\"+fileName);
    }

} 
 <action name="downFile" class="com.mx.struts2.action.DownFileAction">
            <result type="stream">
                <param name="contentType">application/octet-stream</param>
                <param name="inputName">inputStream</param>
                <param name="bufferSize">2048</param>
            </result>
</action>
Csdn user default icon
上传中...
上传图片
插入图片