springmvc中下載中文文件名稱為下劃線的解決方案
更新時間:2022年01月26日 16:49:20 作者:我想要的都有啊
這篇文章主要介紹了springmvc中下載中文文件名稱為下劃線的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
springmvc下載中文文件名稱為下劃線
springboot項(xiàng)目中,在下載文件的時候,通過封裝ResponseEntity,將文件流寫入body,這種下載文件的方式,造成了下載的文件名為正文顯示為下劃線的形式;
這個問題很好解決
直接將輸入的文件名的編碼格式定義成GBK格式;
如下代碼
public static ResponseEntity<FileSystemResource> export(File file) throws UnsupportedEncodingException {
if (file == null) {
return null;
}
//這個位置對文件名進(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生成文件名時漢字變?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);
? ? ? }
? ?}注意這里兩個編碼
new String(fileName.getBytes("utf-8"),"iso-8859-1")+ ".xls"以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
如何把spring boot應(yīng)用發(fā)布到Harbor
這篇文章主要介紹了如何把spring boot應(yīng)用發(fā)布到Harbor,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-11-11
Java獲得當(dāng)前時間前指定幾個小時具體時間的方法示例
這篇文章主要介紹了Java獲得當(dāng)前時間前指定幾個小時具體時間的方法,涉及java使用Calendar針對日期時間的相關(guān)運(yùn)算與轉(zhuǎn)換操作技巧,需要的朋友可以參考下2017-08-08
java項(xiàng)目中使用 Lombok遇到的問題小結(jié)
這篇文章主要介紹了java項(xiàng)目中使用 Lombok遇到的問題小結(jié),需要的朋友可以參考下2018-07-07
Spring中的ApplicationRunner接口的使用詳解
這篇文章主要介紹了Spring中的ApplicationRunner接口的使用詳解,ApplicationRunner使用起來很簡單,只需要實(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ì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-10-10

