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

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

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

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

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

猜你喜欢

  • SpringBoot读取自定义配置文件方式(properties,yaml)

    2021-06-30 23:46:07
  • android实现图片闪烁动画效果的两种实现方式(实用性高)

    2022-06-29 14:18:32
  • Android webview打开本地图片上传实现代码

    2023-06-02 14:11:50
  • java在linux本地执行shell命令的实现方法

    2023-11-14 02:31:33
  • SpringBoot实现文件上传与下载功能的示例代码

    2021-11-10 09:15:32
  • SpringBoot整合OpenCV的实现示例

    2022-09-07 03:53:19
  • 10分钟学会VS NuGet包私有化部署

    2022-04-23 02:28:31
  • Android中利用SurfaceView制作抽奖转盘的全流程攻略

    2022-02-04 00:43:10
  • 如何使用MybatisPlus快速进行增删改查详解

    2023-11-03 06:58:13
  • springboot ErrorPageFilter的实际应用详解

    2023-11-24 01:02:59
  • 关于Assert.assertEquals报错的问题及解决

    2023-11-03 04:56:10
  • java实现Base64加密解密算法

    2023-11-25 08:07:27
  • Java线程池运行状态监控实现解析

    2021-10-06 07:15:14
  • 30条Java代码编写经验分享

    2022-01-09 06:41:42
  • 利用Spring Boot操作MongoDB的方法教程

    2023-11-29 11:14:27
  • JVM类运行机制实现原理解析

    2023-11-01 18:26:20
  • IDEA2022版本创建maven web项目的两种方式详解

    2022-04-24 19:34:54
  • Java将Exception信息转为String字符串的方法

    2022-12-01 08:39:35
  • C#调用摄像头实现拍照功能的示例代码

    2023-02-14 16:21:41
  • SpringMVC+EasyUI实现页面左侧导航菜单功能

    2022-09-30 17:11:01
  • asp之家 软件编程 m.aspxhome.com