软件编程
位置:首页>> 软件编程>> java编程>> response文件流输出文件名中文不显示的解决

response文件流输出文件名中文不显示的解决

作者:他们叫我老蒋  发布时间:2023-02-06 19:41:02 

标签:response,文件流,文件名,中文不显示

文件流输出文件名中文不显示

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();

来源:https://blog.csdn.net/Jan_jiang/article/details/82906411

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com