返回顶部
首页 > 资讯 > 后端开发 > Python >response文件流输出文件名中文不显示的解决
  • 307
分享到

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

2024-04-02 19:04:59 307人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

目录文件流输出文件名中文不显示使用如下方法没有解决解决方法response下载时中文文件名乱码文件流输出文件名中文不显示 response返回文件流 用response.setHea

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

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

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: response文件流输出文件名中文不显示的解决

本文链接: https://www.lsjlt.com/news/164132.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作