java-freemarker生成word文件的方法总结

1.pom依赖

<dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.9</version>
        </dependency>
        <dependency>
            <groupId>org.docx4j</groupId>
            <artifactId>docx4j-JAXB-Internal</artifactId>
            <version>8.2.2</version>
        </dependency>

2.图片、附件等文件base64编码

	/**
     * Gets base 64 string.
     *
     * @param imageFile 图片文件的全路径名称
     * @return Base64编码的图片内容字符串 base 64 string
     * @Description 将图片内容转换成Base64编码的字符串
     */
    public static String getBase64String(String imageFile) {
   
        String imageBase64 = Constants.NOTHING_STRING;
        if (StringUtils.isEmpty(imageFile)) {
   
            return imageBase64;
        }

        return getBase64StringByFile(new File(imageFile));
    }

    /**
     * @Description 获取文件的base64编码
     *
     * @Param file 图片文件
     * @return Base64编码的图片内容字符串
     **/
    private static String getBase64StringByFile(File file) {
   
        String imageBase64 = Constants.NOTHING_STRING;
        byte[] data;
        try (InputStream inputStream = new FileInputStream(file.getCanonicalFile())) {
   
            data = new byte[in

你可能感兴趣的:(java开发,freemarker,xml,java,doc)