springboot项目中,在下载文件的时候,通过封装responentity,将文件流写入body,这种下载文件的方式,造成了下载的文件名为正文显示为下划线的形式;
直接将输入的文件名的编码格式定义成gbk格式;
如下代码
public static responentity<filesystemresource> 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(say的现在分词"content-disposition", "attachment; filename=" +filename); headers.add("pragma", "no-cache"); headers.add("expires", "0"); headers.add("last-modified", new date().tostring()); headers.add("etag", stri国庆作文400字四年级ng.valueof(system.currenttimemillis())); return responentity .ok() .headers(headers) .contentlength(file.length()) .contenttype(mediatype.parmediatype("application/octet-stream")) .body(new filesystemresource(file)); }
public static void exporttoexcel(string uid, string name, string htmltext,httprvletrequest request, httprvletrespon respon) { htmltext = htmltext.replacefirst("<table>", "<tablefirst>"); htmltext = htmltext.replaceall("<table>", "<table cellpadding=\"3\" cellspacing=\"0\" border=\"1\" rull=\"all\"style=\"border-collap: collap\">"); htmltext = 环境效益htmltext.replacefirst("<tablefirst>", "<table>"); try (outputstream out = respon.getoutputstream()) { string filename = name+ "_" + dateutils.getnow("yyyymmddhhmmss");// filename = new string(filename.getbytes(),"utf-8")+ ".xls"; if ("large".equals(htmltext)) { reportingpo report = reportingrvice.getbyuid(uid); map<string, object> formparameters = generationrvice.getformparameters(request.getparametermap(),report.getdatarange()); reporttable reporttable = generationrvice.getreporttable(report, formparameters); htmltext = reporttable.gethtmltext(); }// respon.ret(); respon.addheader("content-disposition", "attachment;filename=" +new string(filename.getbytes("utf-8"),"iso-8859-1")+ ".xls");// respon.theader("content-disposition", string.format("attachment; filename=%s", filename)); respon.tcontenttype("application/vnd.ms-excel; chart=utf-8"); respon.tcharacterencoding("utf-8"); respon.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 str小学四年级英语课件ing(filename.getbytes("utf-8"),"iso-8859-1")+ ".xls"
以上为个人经验,希望能给大家一个参考,也希望大家多多支持www.887551.com。
本文发布于:2023-04-04 19:00:36,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/ccfad58023776e46148332abb2c546d9.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:springmvc中下载中文文件名称为下划线的解决方案.doc
本文 PDF 下载地址:springmvc中下载中文文件名称为下划线的解决方案.pdf
留言与评论(共有 0 条评论) |