jsr250-api-1.0.jar 与 jakarta.annotation-api-1.3.5.jar;jar包冲突;freemaker——java生成word文档

一、jsr250-api-1.0.jar 与 jakarta.annotation-api-1.3.5.jar

报错提示:

查找资料:

修改加载依赖:

二、jar包冲突

选择其中的某个jar,右键红色的jar,选择排除

三、freemaker——java生成word文档

java生成word文档

1、word模板转为ftl

将文档模板创建好,对应的填写参数用对应字段写入,比如:姓名 替换为 name

将doc另存为xml文件,然后在参数前后加字符,例如:name 替换为 ${name}

修改xml文件后缀为ftl

2、对应的java代码

pom导入依赖

<dependency>
    <groupId>org.freemarker</groupId>
    <artifactId>freemarker</artifactId>
    <version>2.3.20</version>
</dependency>

包含文档模板目录,模板名称,生成文档目录名称,图片地址

import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;

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

/***
 *
 * Project Name:maventest
 * <p>生成word工具类<br>
 *
 */
public class WordUtil {

    private Configuration configuration = null;

    /****
     * 模板文件存放的目录
     */
    private static final String  baseDir = "F:/JAVAmodel/untitled2";
    /***
     * 模板文件名称
     */
    private static final String  templateFile = "freemaker2.ftl";
    /***
     * word生成的输出目录与文件名称
     */
    private static final String  outputDir = "F:/temp/testWord";

    /**
     * 图片地址
     */
    private static final String  phone = "F:/JAVAmodel/untitled2/ce.jpg";

    public WordUtil(){
        configuration = new Configuration();
        configuration.setDefaultEncoding("UTF-8");
    }

    public static void main(String[] args) throws IOException {
        WordUtil test = new WordUtil();
        test.createWord();
    }

    /*****
     *
     * Project Name: maventest
     * <p>转换成word<br>
     */
    public void createWord() throws IOException {
        Map<String,Object> dataMap=new HashMap<String,Object>();
        //构造参数
        getData(dataMap);

        configuration.setClassForTemplateLoading(this.getClass(), "");//模板文件所在路径
        Template t=null;
        try {
            configuration.setDirectoryForTemplateLoading(new File(baseDir));
            t = configuration.getTemplate(templateFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        File outFile = new File(outputDir+Math.random()*10000+".doc"); //导出文件
        Writer out = null;
        try {
            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile)));
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        }

        try {
            t.process(dataMap, out); //将填充数据填入模板文件并输出到目标文件
            System.out.println("生成成功...");
        } catch (TemplateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /****
     *
     * Project Name: maventest
     * <p>初始化数据map <br>
     *
     *        封装数据的map
     */
    private void getData(Map<String, Object> dataMap) throws IOException {
        dataMap.put("userName", "王小");
        dataMap.put("sex", "男");
        dataMap.put("nation", "汉族");
        dataMap.put("birthday", "1985-02-26");
        dataMap.put("nationPlace", "春日部");
        dataMap.put("politicalStatus", "党员");
        dataMap.put("image", getImageString(phone));


//        dataMap.put("graduationSchool", "双叶幼稚园");
//        dataMap.put("lastBackground", "幼稚园");
//        dataMap.put("graduationMajor", "玩泥沙");
//        dataMap.put("workUnit", "NASA");
//        dataMap.put("business", "煮菜的");
//        dataMap.put("postalAddress", "lc");
//        dataMap.put("postalcode", "lc");
//        dataMap.put("mobile", "18898416969");
//        dataMap.put("admissionTicket", "lc");
//        dataMap.put("enterSchoolTime", "lc");
//        dataMap.put("emergencyContact", "lc");
//        dataMap.put("readingInstrouction", "lc");
//        dataMap.put("year", "2019");
//        dataMap.put("month", "02");
//        dataMap.put("day", "20");

    }

    /**
     *
     * @Title: getImageString
     * @Description: 将本地、网络图片转换成BASE64字符串
     * @param @param filename
     * @param @return
     * @param @throws IOException
     * @return String
     * @throws
     */
    public static String getImageString(String imageUrl) throws IOException {

        InputStream in = null;

        InputStream dis = null;
        byte[] data = null;

        try {

//            //方法一、将网络图片导入wolrd
//            URL url = new URL(imageUrl);
//            //打开网络输入流
//            URLConnection conn = url.openConnection();
//
//            //设置超时间为3秒
//            //conn.setConnectTimeout(3*1000);
//            //防止屏蔽程序抓取而返回403错误
//            //conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
//
//            //得到输入流
//            InputStream inputStream = conn.getInputStream();
//            //获取自己数组
//            data = readInputStream(inputStream);


            //方法二、将本地图片导入wolrd,打开本地输入流
            in = new FileInputStream(imageUrl);
            data = new byte[in.available()];
            in.read(data);
            in.close();


        } catch (IOException e) {
            throw e;
        } finally {
            if (dis != null)
                dis.close();
        }

        BASE64Encoder encoder = new BASE64Encoder();

        return data != null ? encoder.encode(data) : "";

    }

    /**
     *
     * @Title: readInputStream
     * @Description: 将网络图片流转换成数组
     * @param @param inputStream
     * @param @return
     * @param @throws IOException
     * @return byte[]
     * @throws
     */
    public static  byte[] readInputStream(InputStream inputStream) throws IOException {
        byte[] buffer = new byte[1024];
        int len = 0;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        while((len = inputStream.read(buffer)) != -1) {
            bos.write(buffer, 0, len);
        }
        bos.close();
        return bos.toByteArray();
    }

    /**
     * @Title: downloadImg
     * @Description: 网络图片下載到本地
     * @param @param imgUrl:网络图片,http开头
     * @param @return 返回下载到本地的图片路径
     * @param @throws Exception
     * @return String
     * @throws
     */
//    public String downloadImg(String imgUrl) throws Exception{
//
//        // 构造URL
//        URL url = new URL(imgUrl);
//        // 打开连接
//        URLConnection con = url.openConnection();
//        //设置请求超时为5s
//        con.setConnectTimeout(5*1000);
//        // 输入流
//        InputStream is = con.getInputStream();
//
//        // 1K的数据缓冲
//        byte[] bs = new byte[1024];
//        // 读取到的数据长度
//        int len;
//
//        //创建下载路径
//        String savePath = "D://download//";
//        String filename =  UUIDUtil.getUUID()+".jpg";
//        String returnUrl = savePath+filename;
//
//        File sf = new File(savePath);
//        if(!sf.exists()){
//            sf.mkdirs();
//        }
//
//        // 输出的文件流
//        OutputStream os = new FileOutputStream(sf.getPath()+"\\"+filename);
//        // 开始读取
//        while ((len = is.read(bs)) != -1) {
//            os.write(bs, 0, len);
//        }
//        // 完毕,关闭所有链接
//        os.flush();
//        os.close();
//        is.close();
//
//        return returnUrl;
//    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值