struts2文件下载 火狐浏览器的文件名乱码问题

这是一个文件下载的action,红色部分为火狐浏览器需要特地做的事情。

@Controller
@Scope(value = "prototype")
public class FormManage_downloadAction {

    private String fileName;//下载文件时候的名称
    private String filePath;//文件在磁盘的路径

    public String execute() throws UnsupportedEncodingException {
        System.out.println("下载action");
        System.out.println("fileName="+fileName);
        System.out.println("filePath="+filePath);

        HttpServletRequest request = ServletActionContext.getRequest();
        String Agent = request.getHeader("User-Agent");
        if (null != Agent) {
            Agent = Agent.toLowerCase();
            if (Agent.contains("firefox")) {
                System.out.println("火狐浏览器");
                fileName = new String(fileName.getBytes(), "iso8859-1");
            } else {
                System.out.println("非火狐浏览器");
                fileName = URLEncoder.encode(fileName, "utf8");
            }
        }
        return "success";
    }

    public InputStream getInputStream() throws FileNotFoundException {
        return new FileInputStream(filePath);
    }

    public String getFileName() {
        return fileName;
    }

    public String getFilePath() {
        return filePath;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }
}

 

posted @ 2013-08-05 22:41  无忧之路  阅读(573)  评论(0编辑  收藏  举报
无忧之路