当前位置: 首页 > news >正文

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();}}}
http://www.lryc.cn/news/484892.html

相关文章:

  • arkUI:水果选择与管理:基于 ArkUI 的长按编辑功能实现
  • docker使用,docker图形化界面+docker详细命令
  • idea项目运行时 java: 错误: 不支持发行版本 21
  • hive alter table add columns 是否使用 cascade 的方案
  • 手机怎么玩steam游戏?随时随地远程串流玩steam游戏教程
  • 【使用antv g6实现拓扑图】
  • 【数学 函数空间】拉普拉斯变换解微分方程步骤
  • vue3: toRef, reactive, toRefs, toRaw
  • Unity读取Json
  • 基于STM32的智能语音识别饮水机系统设计
  • c++的几种构造函数
  • FRP 实现内网穿透
  • 数据结构笔记(其八)--一般树的存储及其遍历
  • 在spring boot工程中使用Filter时,@WebFilter 注解不生效的问题分析和解决方案
  • 浅谈“通感一体”
  • 【Linux】监控系统Zabbix的安装与配置
  • Springboot定时任务
  • node.js知识点总结
  • Kotlin中泛型的协变
  • 第三百二十五节 Java线程教程 - Java Fork/Join框架
  • 网络游戏安全现状及相关应对方案
  • uniapp h5地址前端重定向跳转
  • uniapp隐藏自带的tabBar
  • 使用--log-file保存pytest的运行日志
  • WebAPI性能监控-MiniProfiler与Swagger集成
  • 视频会议接入GB28181视频指挥调度,语音对讲方案
  • 深度学习和图像处理
  • 〔 MySQL 〕数据类型
  • 云安全之云计算基础
  • PostgreSQL pg-xact(clog)目录文件缺失处理