现在的位置: 首页 > 综合 > 正文

freemarker乱码问题

2013年05月01日 ⁄ 综合 ⁄ 共 1003字 ⁄ 字号 评论关闭

数据不是乱码,模板文件中的中文是乱码,是模板的文件格式不正确造成的;

其他的地方编码设置:

config.setEncoding(Locale.CHINA, "UTF-8");
        config.setDirectoryForTemplateLoading(new File(base+"configuration\\template\\jsp"));
        
        //设置模板路径
        config.setObjectWrapper(new DefaultObjectWrapper());
        Template temp = config.getTemplate("template_entity.ftl");
        temp.setEncoding("UTF-8");
        temp.setLocale(Locale.CHINA);
        
        System.err.println(temp.getEncoding());
        
        //把包中点转换成“/”,形成目录的路径
        String str = "code."+root.get("projectInfo")+"."+root.get("entityPackage");
        String dir = base + str.replace(".", "/") + "/";
        
        //设置目标路径,增加目标类的文件名
        String path = dir + StrUtil.FirstCap(root.get("class").toString())+".java";
        File f = new File(dir);
        if( !f.exists() )
            f.mkdirs();
        Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path),"utf-8"));

//        OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(path),"UTF-8");
        temp.process(root, out);
        out.flush();
        out.close();

抱歉!评论已关闭.