日期:2014-05-19  浏览次数:20793 次

Java 生成word怎么将word设置为只读属性
我是将word另存为xml文件,用FreeMarker标签处理,然后生成的word文件,怎么将他设置为只读呢。
最后的执行语句是这样的:
Java code
Map<String, Object> dataMap = new HashMap<String, Object>();
        getData(dataMap);
        // 设置模本装置方法和路径,FreeMarker支持多种模板装载方法。可以重servlet,classpath,数据库装载,
        // 模板是放在com.havenliu.document.template包下面
        configuration.setClassForTemplateLoading(this.getClass(),
                "/com/havenliu/document/template");
        Template t = null;
        try {
            // test.ftl为要装载的模板
            t = configuration.getTemplate("test.xml");
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 输出文档路径及名称
        File outFile = new File("D:/files/outfile.doc");
        Writer out = null;
        try {
            out = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(outFile),"utf-8"));
        } catch (Exception e1) {
            e1.printStackTrace();
        }

        try {
            t.process(dataMap, out);
        } catch (TemplateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

用file的

------解决方案--------------------
Java code
new File(outFile).setWritable(false);

------解决方案--------------------
以防万一说下,你的代码里。
out(Writer)对象close后了再用1#的代码。

还有你已经new了一个File对象了outFile 
直接在out.close();后面

outFile.setWritable(false);