weixin_40478037 2018-05-03 03:04 采纳率: 100%
浏览 760
已采纳

悬赏20:给java.class文件添加一个可以随时修改参数的前端

package service;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import servlet.FreeMarkServlet;
import sun.misc.BASE64Encoder;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

/**

  • 使用freemark生成word
  • @author stormwy */

public class Freemark {

public static void main(String[] args){
    Freemark freemark = new Freemark("/template");

// freemark.setTemplateName("wordTemplate.ftl");
freemark.setTemplateName("word1.ftl");
freemark.setFileName("word6"+".docx");
freemark.setFilePath("WebContent/resources/");
freemark.createWord();
}

public void createWord(){

    Template t = null;
    try {
        t = configuration.getTemplate(templateName);
    } catch (IOException e) {
        e.printStackTrace();
    }

    File outFile = new File(filePath+fileName);
    Writer out = null;
    try {
        out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "UTF-8"));
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
//  FreeMarkServlet pro = new FreeMarkServlet();  

    Map<String, Object> map = new HashMap<String, Object>();

    map.put("date1", bianxieren);
    map.put("date2", shenpiren);
    map.put("date3", bianxie);
    map.put("date4", shenpi);
/*  map.put("image", getImageStr());

    map.put("SEX", "男");
    map.put("BIRTH", "1987-08");
    map.put("ZZMM", "党员");

      */

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

}
/**
 * freemark初始化
 * @param templatePath 模板文件位置
 */
public Freemark(String templatePath) {
    configuration = new Configuration();
    configuration.setDefaultEncoding("utf-8");
    configuration.setClassForTemplateLoading(this.getClass(),templatePath);     
}   
//插入图片
/*private String getImageStr() {
    String imgFile = "D:/hanmanyi/pic/111.jpg";
    InputStream in = null;
    byte[] data = null;
    try {
        in = new FileInputStream(imgFile);
        data = new byte[in.available()];
        in.read(data);
        in.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    BASE64Encoder encoder = new BASE64Encoder();
    return encoder.encode(data);
}*/

/**
 * freemark模板配置
 */
private Configuration configuration;
/**
 * freemark模板的名字
 */
private String templateName;
/**
 * 生成文件名
 */
private String fileName;
/**
 * 生成文件路径
 */
private String filePath;

public String getFileName() {
    return fileName;
}

public void setFileName(String fileName) {
    this.fileName = fileName;
}

public String getFilePath() {
    return filePath;
}

public void setFilePath(String filePath) {
    this.filePath = filePath;
}

public String getTemplateName() {
    return templateName;
}

public void setTemplateName(String templateName) {
    this.templateName = templateName;
}

}
图片说明这块是要修改的参数

那个freeMark是一个自己可以运行的的文件,他的功能是可以修改一部分参数,生成一个word文档,但是修改参数的时候必须在文件里修改,所以我就想能不能通过页面输入值给它修改

我自己弄个了简单的前台,我想用前台把值传给这个freeMark,请教了别人,说需要通过servlet,我吧值能传给servlet,但是死活不会从servlet吧值传给freeMark.java了

  • 写回答

3条回答 默认 最新

  • qq_35741499 2018-05-15 03:14
    关注







    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常