文件返回时设置response.header的文件名乱码

response.setContentType("application/x-download");
response.setCharacterEncoding("utf-8");
if (fm != null && fm != null) {
	String userAgent = request.getHeader("User-Agent").toLowerCase();
	String strHeader = "";
	/**
     * 1. IE浏览器,采用URLEncoder编码 2. Opera浏览器,采用filename*方式 3.
     * Safari浏览器,采用ISO编码的中文输出 4. Chrome浏览器,采用Base64编码或ISO编码的中文输出
     * 5. FireFox浏览器,采用Base64或filename*或ISO编码的中文输出
     * 
     */
	// IE使用URLEncoder
	String name=fm.getFileName() + "." + fm.getExtName();
	if(userAgent.contains("firefox")){
		strHeader = "attachment;filename="+new String((name).getBytes("utf-8"), "ISO8859-1");
	}else if (userAgent.contains("windows")) {
		strHeader = "attachment;filename="+ URLEncoder.encode(name, "UTF-8");
		// 其它使用转iso
	} else {
		strHeader = "attachment;filename="+ new String((name).getBytes("utf-8"), "ISO8859-1");
	}
	response.addHeader("Content-Disposition", strHeader);
} else {
	String fix=FileUtil.getExtension(applySign.getContractStoreId());
	response.addHeader("Content-Disposition", "attachment;filename=" + new Date().getTime()+fix);
}
IOUtils.write(data, response.getOutputStream());
IOUtils.closeQuietly(response.getOutputStream());

1.  IE浏览器,采用URLEncoder编码 
2.  Opera浏览器,采用filename*方式 
3.  Safari浏览器,采用ISO编码的中文输出 
4.  Chrome浏览器,采用Base64编码或ISO编码的中文输出 
5.  FireFox浏览器,采用Base64或filename*或ISO编码的中文输出 

转载于:https://my.oschina.net/yifanxiang/blog/756477

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值