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

打印 上一主题 下一主题

主题 987|帖子 987|积分 2961

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

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

UTF-8 内容转换

  1.     public static void setResponseHeader(HttpServletResponse response, String fileName) {
  2.         try {
  3.             // fileName = "中文.xls";
  4.              try {
  5.                 fileName = new String(fileName.getBytes(),"ISO8859-1");
  6.             } catch (UnsupportedEncodingException e) {
  7.                 e.printStackTrace();
  8.             }
  9.             response.setContentType("application/octet-stream;charset=UTF-8");
  10.             //response.setCharacterEncoding("UTF-8");
  11.             response.setHeader("Content-Disposition", "attachment;filename="+ fileName);//
  12.             response.addHeader("Pargam", "no-cache");
  13.             response.addHeader("Cache-Control", "no-cache");
  14.         } catch (Exception ex) {
  15.             ex.printStackTrace();
  16.         }
  17.     }
  18. }
  19. public void xml2XmlOutFromResources(HttpServletResponse response, Map<String, Object> dataMap, String templetFile, String fileName) throws IOException, TemplateException {
  20.        //设置响应头
  21.        ExportExcel.setResponseHeader(response, fileName);
  22.        Configuration configuration = new Configuration(Configuration.VERSION_2_3_28);
  23.        configuration.setDefaultEncoding("UTF-8");
  24.        configuration.setClassLoaderForTemplateLoading(this.getClass().getClassLoader(), "");
  25.        Template template = null;
  26.        try {
  27.            template = configuration.getTemplate("templates/exportTemplate.xml","UTF-8");
  28.        } catch (Exception e) {
  29.            e.printStackTrace();
  30.        }
  31.        //模板和数据模型合并生成文件
  32.        OutputStream os = null;
  33.        try {
  34.            os = response.getOutputStream();
  35.            try (Writer writer = new OutputStreamWriter(os, "UTF-8")) {
  36.                template.process(dataMap, writer);
  37.            }
  38.        } catch (Exception ex) {
  39.        } finally {
  40.            try {
  41.                if (os != null) {
  42.                    os.close();
  43.                }
  44.            } catch (IOException e) {
  45.                e.printStackTrace();
  46.            }
  47.        }
  48.    }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

耶耶耶耶耶

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表