springmvc中下载中文文件名称为下划线的解决方案

网友投稿 610 2022-08-31


springmvc中下载中文文件名称为下划线的解决方案

目录springmvc下载中文文件名称为下划线这个问题很好解决java生成文件名时汉字变为下划线?注意这里两个编码

springmvc下载中文文件名称为下划线

springboot项目中,在下载文件的时候,通过封装ResponseEntity,将文件流写入body,这种下载文件的方式,造成了下载的文件名为正文显示为下划线的形式;

这个问题很好解决

直接将输入的文件名的编码格式定义成GBK格式;

如下代码

public static ResponseEntity export(File file) throws UnsupportedEncodingException {

if (file == null) {

return null;

}

//这个位置对文件名进行编码

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生成文件名时汉字变为下划线?

public static void exportToExcel(String uid, String name, String htmlText,

HttpServletRequest request, HttpServletResponse response) {

htmlText = htmlText.replaceFirst("

htmlText = htmlText.replaceAll("

"

style=\"border-collapse: collapse\">");

htmlText = htmlText.replaceFirst("", "

");

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 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 http://ex) {

throw new RuntimeException(ex);

}

http:// }

注意这里两个编码

new String(fileName.getBytes("utf-8"),"iso-8859-1")+ ".xls"

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 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 http://ex) {

throw new RuntimeException(ex);

}

http:// }

注意这里两个编码

new String(fileName.getBytes("utf-8"),"iso-8859-1")+ ".xls"


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:【Python小试】将核酸序列翻译成氨基酸序列(碱基序列翻译成氨基酸)
下一篇:【Python小试】使用列表解析式简化代码(列表推导式python菜鸟教程)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~