admin管理员组

文章数量:1130349

  • 各个浏览器的agent

    IE: Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko

    CHROME: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36

    EDGE: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.18363

    FIREFOX: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:76.0) Gecko/20100101 Firefox/76.0

    UBUNTU: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:76.0) Gecko/20100101 Firefox/76.0

    长城: Mozilla/5.0 (X11; Linux aarch64; rv:52.0) Gecko/20100101 Firefox/52.0

    龙芯中标麒麟: Mozilla/5.0 (X11; Fedora; Linux mips64; rv:52.0) Gecko/20100101 Firefox/52.0

  • 根据RFC标准建议的处理方式:contentDisposition:;filename=xxx;filename*=utf-8’'xxx
    如果只是普通的下载附件,该方式已经可以完美解决各个浏览器的附件名乱码。但在流式插件下使用打开原成文档会出现打开失败的问题,这是由于各个插件厂商对该标准的支持各不相同!!

  • 附上代码

public static String buildContentDisposition(String userAgent, String fileName, String type, boolean forceDownLoad) {
        
        userAgent = MyUtils.getValue(userAgent, "");

        try {
            String userAgentLower = userAgent.toLowerCase();
            if (userAgentLower.indexOf("firefox") >= 0) {
                byte[] bytes = userAgent.toLowerCase().indexOf("linux") >= 0 ? fileName.getBytes("UTF-8") : fileName.getBytes("GB2312");
                fileName = new String(bytes, "ISO-8859-1");
            } else if (userAgentLower.indexOf("chrome") == -1 && userAgentLower.indexOf("safari") >= 0) {//chrom下发起的请求头也有safari,在中创中间件下会出现被替换的问题。
                fileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
            } else {
                fileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20").replaceAll("%28", "\\(").replaceAll("%29", "\\)")
                        .replaceAll("%3B", ";").replaceAll("%40", "@").replaceAll("%23", "\\#").replaceAll("%26", "\\&").replaceAll("%2C", "\\,");
            }

        } catch (UnsupportedEncodingException e) {
            System.out.println("contentDisposition:" + fileName + "编码异常");
        }
        
        // 2020-05-22(tinyf) ntko对filename*=utf-8''的支持存在缺陷,会导致打开文件失败。
        if (userAgent.indexOf("WOW64") > -1) {
            fileName = String.format(";filename=%1$s", fileName);
        } else {
            fileName = String.format(";filename=%1$s;filename*=utf-8''%<s", fileName);
        }
        
        if (!StringUtils.hasText(type)) {
            type = forceDownLoad ? "attachment" : "inline";
        }

        return type + fileName;
    }

  • 各个浏览器的agent

    IE: Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko

    CHROME: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36

    EDGE: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.18363

    FIREFOX: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:76.0) Gecko/20100101 Firefox/76.0

    UBUNTU: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:76.0) Gecko/20100101 Firefox/76.0

    长城: Mozilla/5.0 (X11; Linux aarch64; rv:52.0) Gecko/20100101 Firefox/52.0

    龙芯中标麒麟: Mozilla/5.0 (X11; Fedora; Linux mips64; rv:52.0) Gecko/20100101 Firefox/52.0

  • 根据RFC标准建议的处理方式:contentDisposition:;filename=xxx;filename*=utf-8’'xxx
    如果只是普通的下载附件,该方式已经可以完美解决各个浏览器的附件名乱码。但在流式插件下使用打开原成文档会出现打开失败的问题,这是由于各个插件厂商对该标准的支持各不相同!!

  • 附上代码

public static String buildContentDisposition(String userAgent, String fileName, String type, boolean forceDownLoad) {
        
        userAgent = MyUtils.getValue(userAgent, "");

        try {
            String userAgentLower = userAgent.toLowerCase();
            if (userAgentLower.indexOf("firefox") >= 0) {
                byte[] bytes = userAgent.toLowerCase().indexOf("linux") >= 0 ? fileName.getBytes("UTF-8") : fileName.getBytes("GB2312");
                fileName = new String(bytes, "ISO-8859-1");
            } else if (userAgentLower.indexOf("chrome") == -1 && userAgentLower.indexOf("safari") >= 0) {//chrom下发起的请求头也有safari,在中创中间件下会出现被替换的问题。
                fileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
            } else {
                fileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20").replaceAll("%28", "\\(").replaceAll("%29", "\\)")
                        .replaceAll("%3B", ";").replaceAll("%40", "@").replaceAll("%23", "\\#").replaceAll("%26", "\\&").replaceAll("%2C", "\\,");
            }

        } catch (UnsupportedEncodingException e) {
            System.out.println("contentDisposition:" + fileName + "编码异常");
        }
        
        // 2020-05-22(tinyf) ntko对filename*=utf-8''的支持存在缺陷,会导致打开文件失败。
        if (userAgent.indexOf("WOW64") > -1) {
            fileName = String.format(";filename=%1$s", fileName);
        } else {
            fileName = String.format(";filename=%1$s;filename*=utf-8''%<s", fileName);
        }
        
        if (!StringUtils.hasText(type)) {
            type = forceDownLoad ? "attachment" : "inline";
        }

        return type + fileName;
    }

本文标签: 乱码文件名文件名称浏览器解决方案