springmvc中下載中文文件名稱為下劃線的解決方案
springmvc下載中文文件名稱為下劃線
springboot項(xiàng)目中,在下載文件的時(shí)候,通過(guò)封裝ResponseEntity,將文件流寫入body,這種下載文件的方式,造成了下載的文件名為正文顯示為下劃線的形式;
這個(gè)問(wèn)題很好解決
直接將輸入的文件名的編碼格式定義成GBK格式;
如下代碼
public static ResponseEntity<FileSystemResource> export(File file) throws UnsupportedEncodingException {
if (file == null) {
return null;
}
//這個(gè)位置對(duì)文件名進(jìn)行編碼
String fileName = new String (file.getName().getBytes("GBK"),"ISO-8859-1");
HttpHeaders headers = new HttpHeaders();
headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
headers.add("Content-Disposition", "attachment; filename=" +fileName);
headers.add("Pragma", "no-cache");
headers.add("Expires", "0");
headers.add("Last-Modified", new Date().toString());
headers.add("ETag", String.valueOf(System.currentTimeMillis()));
return ResponseEntity
.ok()
.headers(headers)
.contentLength(file.length())
.contentType(MediaType.parseMediaType("application/octet-stream"))
.body(new FileSystemResource(file));
}
java生成文件名時(shí)漢字變?yōu)橄聞澗€?
public static void exportToExcel(String uid, String name, String htmlText,?
HttpServletRequest request, HttpServletResponse response) {
? ? ? htmlText = htmlText.replaceFirst("<table>", "<tableFirst>");
? ? ? htmlText = htmlText.replaceAll("<table>",
? ? ? ? ? ? "<table cellpadding=\"3\" cellspacing=\"0\" ?border=\"1\" rull=\"all\"?
style=\"border-collapse: collapse\">");
? ? ? htmlText = htmlText.replaceFirst("<tableFirst>", "<table>");
? ? ? try (OutputStream out = response.getOutputStream()) {
? ? ? ? ?String fileName = name+ "_" + DateUtils.getNow("yyyyMMddHHmmss");
// ? ? ? fileName = new String(fileName.getBytes(),"utf-8")+ ".xls";
? ? ? ? ?if ("large".equals(htmlText)) {
? ? ? ? ? ? ReportingPo report = reportingService.getByUid(uid);
? ? ? ? ? ? Map<String, Object> formParameters = generationService.getFormParameters(request.getParameterMap(),?
report.getDataRange());
? ? ? ? ? ? ReportTable reportTable = generationService.getReportTable(report, formParameters);
? ? ? ? ? ? htmlText = reportTable.getHtmlText();
? ? ? ? ?}
// ? ? ? response.reset();
? ? ? ? ?response.addHeader("Content-Disposition", "attachment;filename=" +
?new String(fileName.getBytes("utf-8"),"iso-8859-1")+ ".xls");
// ? ? ? response.setHeader("Content-Disposition", String.format("attachment; filename=%s", fileName));
? ? ? ? ?response.setContentType("application/vnd.ms-excel; charset=utf-8");
? ? ? ? ?response.setCharacterEncoding("utf-8");
? ? ? ? ?response.addCookie(new Cookie("fileDownload", "true"));
// ? ? ? out.write(new byte[] { (byte) 0xEF, (byte) 0xBB, (byte) 0xBF }); // 生成帶bom的utf8文件
? ? ? ? ?out.write(htmlText.getBytes("utf-8"));
? ? ? ? ?out.flush();
? ? ? } catch (Exception ex) {
? ? ? ? ?throw new RuntimeException(ex);
? ? ? }
? ?}注意這里兩個(gè)編碼
new String(fileName.getBytes("utf-8"),"iso-8859-1")+ ".xls"以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
如何把spring boot應(yīng)用發(fā)布到Harbor
這篇文章主要介紹了如何把spring boot應(yīng)用發(fā)布到Harbor,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11
Mybatis?如何開(kāi)啟控制臺(tái)打印sql語(yǔ)句
這篇文章主要介紹了Mybatis?如何開(kāi)啟控制臺(tái)打印sql語(yǔ)句問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11
Java獲得當(dāng)前時(shí)間前指定幾個(gè)小時(shí)具體時(shí)間的方法示例
這篇文章主要介紹了Java獲得當(dāng)前時(shí)間前指定幾個(gè)小時(shí)具體時(shí)間的方法,涉及java使用Calendar針對(duì)日期時(shí)間的相關(guān)運(yùn)算與轉(zhuǎn)換操作技巧,需要的朋友可以參考下2017-08-08
java項(xiàng)目中使用 Lombok遇到的問(wèn)題小結(jié)
這篇文章主要介紹了java項(xiàng)目中使用 Lombok遇到的問(wèn)題小結(jié),需要的朋友可以參考下2018-07-07
Spring中的ApplicationRunner接口的使用詳解
這篇文章主要介紹了Spring中的ApplicationRunner接口的使用詳解,ApplicationRunner使用起來(lái)很簡(jiǎn)單,只需要實(shí)現(xiàn)CommandLineRunner或者ApplicationRunner接口,重寫run方法就行,需要的朋友可以參考下2023-11-11
java書店系統(tǒng)畢業(yè)設(shè)計(jì) 總體設(shè)計(jì)(1)
這篇文章主要介紹了java書店系統(tǒng)畢業(yè)設(shè)計(jì),第一步系統(tǒng)總體設(shè)計(jì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10
SpringBoot配置及使用Schedule過(guò)程解析
這篇文章主要介紹了SpringBoot配置及使用Schedule過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04

