文件流输出文件名中文不显示
response返回文件流 用response.setHeader(“Content-disposition”, “attachment; filename=”+fileName);结果中文名称以“—”下滑下显示。
使用如下方法没有解决
response.setCharacterEncoding(“UTF-8”);
response.setContentType(“text/html;charset=UTF-8”);
response.setLocale(new java.util.Locale(“zh”,“CN”));
原因是这些操作是针对返回内容进行编码设置,而我这里文件名称设置于header;
解决方法
将文件名称转换为ASCII码~
如下:
// 一万行代码没有显示
OutputStream output = null;
try {
response.reset();
response.setContentType("application/msexcel;charset=UTF-8");
// response.setCharacterEncoding("UTF-8");
fileName = URLEncoder.encode(fileName,"UTF-8");
output = response.getOutputStream();
response.setHeader("Content-disposition", "attachment; filename="+fileName);
wb.write(output);
if(output != null) output.close();
} catch (IOException e) {
// TODO
e.printStackTrace();
}
response下载时中文文件名乱码
FileInfo info = new FileInfo(strFilePath);
long fileSize = info.Length;
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/x-zip-compressed";
Response.AddHeader("Accept-Language", "zh-cn");
Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpContext.Current.Server.UrlEncode(info.Name));
//不指明Content-Length用Flush的话不会显示下载进度
Response.AddHeader("Content-Length", fileSize.ToString());
Response.TransmitFile(strFilePath, 0, fileSize);
Response.Flush();
Response.Close();
info.Delete();
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。