惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 【当耐特】
N
Netflix TechBlog - Medium
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
雷峰网
雷峰网
MongoDB | Blog
MongoDB | Blog
有赞技术团队
有赞技术团队
Engineering at Meta
Engineering at Meta
M
MIT News - Artificial intelligence
Google DeepMind News
Google DeepMind News
罗磊的独立博客
Hugging Face - Blog
Hugging Face - Blog
WordPress大学
WordPress大学
T
Tailwind CSS Blog
小众软件
小众软件
J
Java Code Geeks
人人都是产品经理
人人都是产品经理
博客园_首页
MyScale Blog
MyScale Blog
博客园 - 聂微东
V
Visual Studio Blog
The Cloudflare Blog
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
U
Unit 42

博客园 - 镜涛

Spark环境搭建遇到的问题 This installation has not been configured properly for Software Updates The Activator X for bundle Y is invalid, caused by ClassNotFoundException: X SAP全球技术大会参会随笔 工作流系统概述 需要掌握的flex知识点 ORA-01460: 转换请求无法实现或不合理 去除对象中的类型集合 【转载】Tomcat内存溢出的原因及调试 做产品开发的感想 基于SAML的单点登录.NET代理端实现方案 Cursor identified in Fetch statement is not open 获取矩形中心点与矩形外某点连线和矩形交点的算法 Could not find the main class. Program will exit. - 镜涛 Unrecognized attribute 'targetFramework'.错误解决 [译]Razor内幕之模板 - 镜涛 - 博客园 [译]Razor内幕之表达式 - 镜涛 - 博客园 [译]Razor内幕之解析 [译]Razor内幕之介绍
Java下载中文乱码问题解决方法 - 镜涛 - 博客园
镜涛 · 2010-12-16 · via 博客园 - 镜涛

问题描述: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();

}