response文件流輸出文件名中文不顯示的解決
文件流輸出文件名中文不顯示
response返回文件流 用response.setHeader(“Content-disposition”, “attachment; filename=”+fileName);結(jié)果中文名稱以“—”下滑下顯示。
使用如下方法沒有解決
response.setCharacterEncoding(“UTF-8”); response.setContentType(“text/html;charset=UTF-8”); response.setLocale(new java.util.Locale(“zh”,“CN”));
原因是這些操作是針對返回內(nèi)容進(jìn)行編碼設(shè)置,而我這里文件名稱設(shè)置于header;
解決方法
將文件名稱轉(zhuǎn)換為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的話不會顯示下載進(jìn)度 ??
Response.AddHeader("Content-Length", fileSize.ToString());
Response.TransmitFile(strFilePath, 0, fileSize);
Response.Flush();
Response.Close();
info.Delete();以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
把spring boot項目發(fā)布tomcat容器(包含發(fā)布到tomcat6的方法)
這篇文章主要介紹了把spring boot項目發(fā)布tomcat容器(包含發(fā)布到tomcat6的方法),然后在文章給大家提到了如何將Spring Boot項目打包部署到外部Tomcat,需要的朋友參考下吧2017-11-11
Java索引越界異常Exception java.lang.IndexOutOfBoundsException
本文主要介紹了Java索引越界異常Exception java.lang.IndexOutOfBoundsException的解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06
Java concurrency集合之ConcurrentSkipListSet_動力節(jié)點Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了Java concurrency集合之ConcurrentSkipListSet的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06
詳解Spring Boot整合Mybatis實現(xiàn) Druid多數(shù)據(jù)源配置
本篇文章主要介紹了詳解Spring Boot整合Mybatis實現(xiàn) Druid多數(shù)據(jù)源配置,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-03-03
Spring框架的環(huán)境搭建和測試實現(xiàn)
這篇文章主要介紹了Spring框架的環(huán)境搭建和測試實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10

