Velocity的中文问题
来源:互联网 发布:免费喊单软件 编辑:程序博客网 时间:2024/06/06 10:02
http://blog.csdn.net/mydeman/article/details/6633242
今天做了一个小工具,通过Velocity生成Latext的tex文件,可是当使用Miktex生成PDF时,里面的中文都变成了乱码。而之前在Eclipse直接运行时,并没有发现问题。毫无疑问是文件编码引起的问题。
用Notepad++打开生成的tex文件,发现文件的编码是ANSI,也就是系统本地的编码。下面是生成tex的代码:
- public class VelocityHelper {
- private static VelocityContext vc;
- static {
- vc = new VelocityContext();
- }
- public static void generateFile(String tempatePath, String destPath, Map<String, Object> attributes){
- Template template = Velocity.getTemplate(tempatePath);
- for(String key : attributes.keySet()){
- vc.put(key, attributes.get(key));
- }
- BufferedWriter bw = null;
- try {
- bw = new BufferedWriter(new FileWriter(destPath));
- template.merge(vc, bw);
- bw.flush();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if(bw != null){
- try {
- bw.close();
- } catch (IOException e) {
- }
- }
- }
- }
- }
Google了一下找到了两种解决乱码问题的方法:
1. 在获取模板文件时指定编码,即:
- Template template = Velocity.getTemplate(tempatePath, "UTF-8");
2. 在生成文件时指定编码,即:
- //template.merge(vc, bw);
- Velocity.mergeTemplate(destPath, "UTF-8", vc, bw);
可是这两种方法并不起作用。正在偶然之间看到了上面初始BufferedWriter的代码,这才是生成文件的关键代码,将其修改为:
- bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(destPath), "UTF-8"));
终于看到了久违的中文。
0 0
- Velocity的中文问题
- Velocity的中文问题
- Velocity的中文指南
- spring下使用velocity的中文乱码问题
- velocity找不到模板资源,中文问题
- velocity demo 解决中文显示问题
- 用velocity生成代码,中文乱码问题
- Velocity中文
- Velocity中文
- Velocity中文
- Velocity中文
- Velocity中文
- Velocity中文
- Velocity中文
- Velocity中文
- Velocity中文
- Velocity中文
- Velocity中文
- C语言,有5名学生保存在结构体数组中,编程按学生的成绩升序排序,按学生的姓名降序排序,按年龄从低到高排序
- 正则表达式的尝试使用
- DataInputStream和DataOutputStream用法细节探讨
- poj 2002 Squares
- 39_震动服务&动态的监听edittext文本变化
- Velocity的中文问题
- Android 监听返回键,弹出一个退出对话框
- Android开发之Junit测试
- hdu 1009 FatMouse' Trade
- jQuery_review之textarea的放大和缩小的jQuery实现
- R语言之机器学习
- {{引擎算法机制
- leetcode做题总结,题目Merge Two Sorted Lists 2012/03/30
- 订书机凤凰飞机头护卫舰可怜认购金额离开可怜风格迥然