解决IE及其他浏览器下载文件标题中文乱码问题

解决IE及其他浏览器下载文件标题中文乱码问题

近期些项目遇到下载文件中文名称乱码问题,在网上看了一些博客,总结出以下内容,废话不说,直接上代码。

	//首先给一个生成文件的路径
	String filePath = "C:\Users\admin\Desktop\pdf";
	//判断文件夹存不存在,不存在则创建
	File wjjfile = new File(filePath);
	if (!wjjfile.exists()) {
    	wjjfile.mkdirs();
	}
    //生成一个随机数作为生成文件名
    String code = String.valueOf(new Random().nextInt(899999) + 100000);
    String fileName = code+".pdf";
    String clientFileName = "202x年度xxx花名册.pdf";
    //创建pdf   使用itext技术手动画PDF,这一步网上有很多例子
    if(BysjymcPDFUtil.createPDF(filePath+fileName, data)){
        //打印日志
    }
    //判断文件存不存在
    File file=new File(filePath,fileName);
    if(!file.isFile()){
        throw new FileNotFoundException("");
    }
    BufferedInputStream bis= new BufferedInputStream(new FileInputStream(file));
    BufferedOutputStream bos = null;
    response.setContentType("text/html;charset=UTF-8");
    request.setCharacterEncoding("UTF-8");
    if (request.getScheme() == "https" || request.getScheme() == "http") {
        response.setHeader("Content-Transfer-Encoding", "binary");
        response.setHeader("Cache-Control",
                           "must-revalidate, post-check=0, pre-check=0");
        response.setHeader("Pragma", "public");
    }
    long fileLength = file.length();
    response.setContentType("application/x-msdownload;");
    String browser = "";
    try {
        browser = request.getHeader("User-Agent");
        if (browser.indexOf("MSIE 6.0") > -1 || browser.indexOf("MSIE 7.0") > -1 ) {
            // IE6, IE7 浏览器
            response.addHeader("content-disposition", "attachment;filename="+ new String(clientFileName.getBytes(), "ISO8859-1"));
        } else if (browser.indexOf("MSIE 8.0") > -1) {
            // IE8
            response.addHeader("content-disposition", "attachment;filename="+ URLEncoder.encode(clientFileName, "UTF-8"));
        } else if (browser.indexOf("MSIE 9.0") > -1) {
            // IE9
            response.addHeader("content-disposition", "attachment;filename="+ URLEncoder.encode(clientFileName, "UTF-8"));
        } 
        else if (browser.indexOf("MSIE 10") > 0) {
            // IE10
            response.addHeader("content-disposition", "attachment;filename="+ URLEncoder.encode(clientFileName, "UTF-8"));
        }   //null != agent && -1 != agent.indexOf("like Gecko") 
        else if (browser.indexOf("Gecko") > 0 && browser.indexOf("rv:11") > 0) {
            // IE11
            response.addHeader("content-disposition", "attachment;filename="+ URLEncoder.encode(clientFileName, "UTF-8"));
        } 
        else if (browser.indexOf("Chrome") > -1) {
            // 谷歌
            response.addHeader("content-disposition","attachment;filename*=UTF-8''" + URLEncoder.encode(clientFileName, "UTF-8"));
        } else if (browser.indexOf("Safari") > -1) {
            // 苹果
            response.addHeader("content-disposition", "attachment;filename="+ new String(clientFileName.getBytes(), "ISO8859-1"));
        } else {
            // 火狐或者其他的浏览器
            response.addHeader("content-disposition","attachment;filename*=UTF-8''" + URLEncoder.encode(clientFileName, "UTF-8"));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    response.setHeader("Content-Length", String.valueOf(fileLength));
    bis = new BufferedInputStream(new FileInputStream(file));
    bos = new BufferedOutputStream(response.getOutputStream());
    byte[] buff = new byte[2048];
    int bytesRead;
    while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
        bos.write(buff, 0, bytesRead);
    }
    if (bis != null) {
        try {
            bis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    if (bos != null) {
        try {
            bos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

声明:希望对类似问题的解决有效,转载请附上原文出处链接和本声明。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

从小白到小黑°

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值