欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 产业 > freemarker 读取template.xml ,通过response 输出文件,解决中文乱码问题

freemarker 读取template.xml ,通过response 输出文件,解决中文乱码问题

2025/9/26 13:57:19 来源:https://blog.csdn.net/tonysh_zds/article/details/143777557  浏览:    关键词:freemarker 读取template.xml ,通过response 输出文件,解决中文乱码问题

采用

try (Writer writer = new OutputStreamWriter(os, “UTF-8”))

UTF-8 内容转换
public static void setResponseHeader(HttpServletResponse response, String fileName) {try {// fileName = "中文.xls";try {fileName = new String(fileName.getBytes(),"ISO8859-1");} catch (UnsupportedEncodingException e) {e.printStackTrace();}response.setContentType("application/octet-stream;charset=UTF-8");//response.setCharacterEncoding("UTF-8");response.setHeader("Content-Disposition", "attachment;filename="+ fileName);//response.addHeader("Pargam", "no-cache");response.addHeader("Cache-Control", "no-cache");} catch (Exception ex) {ex.printStackTrace();}}
}public void xml2XmlOutFromResources(HttpServletResponse response, Map<String, Object> dataMap, String templetFile, String fileName) throws IOException, TemplateException {//设置响应头ExportExcel.setResponseHeader(response, fileName);Configuration configuration = new Configuration(Configuration.VERSION_2_3_28);configuration.setDefaultEncoding("UTF-8");configuration.setClassLoaderForTemplateLoading(this.getClass().getClassLoader(), "");Template template = null;try {template = configuration.getTemplate("templates/exportTemplate.xml","UTF-8");} catch (Exception e) {e.printStackTrace();}//模板和数据模型合并生成文件OutputStream os = null;try {os = response.getOutputStream();try (Writer writer = new OutputStreamWriter(os, "UTF-8")) {template.process(dataMap, writer);}} catch (Exception ex) {} finally {try {if (os != null) {os.close();}} catch (IOException e) {e.printStackTrace();}}}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词