我想更改响应对象的字符编码,但在执行下面的Freemarker模板执行代码后,响应对象将更改为已提交的阶段。如何更改响应对象的字符编码。提前致谢。
Writer writer = response.getWriter();
Environment env = template.createProcessingEnvironment(args, writer);
env.setOutputEncoding(charEncoding); // this line doesn't seem to have any effect
env.process();
字符编码始终设置为ISO-8859-1
,我希望它为UTF-8
。
我的环境:
答案 0 :(得分:3)
FreeMarker会写入您在上面提供的Writer
,因此它无法影响输出字符集。关键是创建一个Writer
,它(内部)生成一个具有所需编码的字节流。为此,在之前得到Writer
做:
response.setCharacterEncoding(charEncoding);