zoukankan      html  css  js  c++  java
  • Java下载中文乱码问题解决方法

    问题描述:Java应用下载文件功能,输出文件名中的中文乱码

    测试环境:tomcat,websphere6.1

    方法一:

    response.setContentType("application/x-download");

    fileName = URLDecoder.decode(fileName, "utf-8");

    fileName= java.net.URLEncoder.encode(fileName,"utf-8");

    response.setHeader("Content-Disposition", "attachment; filename=\""+ new String((fileName + ".zip").getBytes(),"UTF-8") +"\"");

    方法二:

    response.setHeader("Content-Disposition", "attachment;filename=" + toUtf8String(filename));

        

    public static String toUtf8String(String s) {

        StringBuffer sb = new StringBuffer();

        for (int i=0;i<s.length();i++) {

            char c = s.charAt(i);

            if (c >= 0 && c <= 255) {

                sb.append(c);

            } else {

                byte[] b;

                try {

                    b = Character.toString(c).getBytes("utf-8");

                } catch (Exception ex) {

                    log.error(ex);

                    b = new byte[0];

                }

                for (int j = 0; j < b.length; j++) {

                    int k = b[j];

                    if (k < 0) k += 256;

    sb.append("%" + Integer.toHexString(k).toUpperCase());

                }

            }

        }

        return sb.toString();

    }

  • 相关阅读:
    使用iwebshop開發實現QQ第三方登錄
    實現QQ第三方登錄
    實現一個天氣預報折現圖功能
    yii框架製作簡易RBAC權限管理
    yii框架RBAC權限管理
    如何利用swoole搭建一個簡易聊天室
    oracle 常用语句
    博客侧边栏
    Oracle 函数使用记录
    myeclipse 导入项目时no projects are found to import解决办法
  • 原文地址:https://www.cnblogs.com/jingtao/p/1908450.html
Copyright © 2011-2022 走看看