java poi docx 转 html 中文乱码

在本地环境使用 poi 转 docx 为 html 正常。部署到生产环境后中文都变为问号了。

指定字符集后恢复正常。

修改前,直接输出到 html 文件的 out 中。

OutputStream out = new FileOutputStream(htmlFullPath);
XHTMLConverter.getInstance().convert(doc, out, options);

修改后,将 out 转为 gbk 编码的字符串,再将字符串保存到 html 文件。关键是 out.toString("gbk")

// 转换 html
ByteArrayOutputStream out = new ByteArrayOutputStream();
XHTMLConverter.getInstance().convert(doc, out, options);

// 将 html 字符串保存到文件
FileUtil.saveFile(out.toString("gbk"), htmlFullPath);

可以多试几种字符集。我遇到的情况是,utf-8 会输出问号,gbk 和 gb2312 正常。

修改前完整代码:

    /**
     * 将 docx 文件转换为 html 文件
     * @param docFullPath
     * @param htmlFullPath
     */
    public static void docxToHtml(String docFullPath, String htmlFullPath) {

        File docFile = new File(docFullPath);
        if (!docFile.exists()) {
            return;
        }

        XWPFDocument doc = null;
        OutputStream out = null;

        try {

            File htmlFile = new File(htmlFullPath);
            if (!htmlFile.getParentFile().exists()) {
                htmlFile.getParentFile().mkdirs();
            }
            if (!htmlFile.exists()) {
                htmlFile.createNewFile();
            }

            doc = new XWPFDocument(OPCPackage.open(docFullPath));
            out = new FileOutputStream(htmlFullPath);

            // 设置 html 中 images 文件夹
            XHTMLOptions options = XHTMLOptions.create();
            options.setImageManager(new ImageManager(htmlFile.getParentFile(), "images"));
            options.setIgnoreStylesIfUnused(false);
            options.setFragment(false);

            XHTMLConverter.getInstance().convert(doc, out, options);

        } catch (Exception e) {
            e.printStackTrace();

        } finally {
            if (doc != null) {
                try {
                    doc.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

修改后完整代码:

/**
     * 将 docx 文件转换为 html 文件
     * @param docFullPath docx 文件全路径
     * @param htmlFullPath 生成的 html 文件全路径
     */
    public static void docxToHtml(String docFullPath, String htmlFullPath) {

        File docFile = new File(docFullPath);
        if (!docFile.exists()) {
            return;
        }

        XWPFDocument doc = null;

        try {

            File htmlFile = new File(htmlFullPath);
            if (!htmlFile.getParentFile().exists()) {
                htmlFile.getParentFile().mkdirs();
            }
            if (!htmlFile.exists()) {
                htmlFile.createNewFile();
            }

            doc = new XWPFDocument(OPCPackage.open(docFullPath));

            // 设置 html 中 images 文件夹
            XHTMLOptions options = XHTMLOptions.create();
            options.setImageManager(new ImageManager(htmlFile.getParentFile(), "images"));
            options.setIgnoreStylesIfUnused(false);
            options.setFragment(false);

            // 转换 html
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            XHTMLConverter.getInstance().convert(doc, out, options);

            // 将 html 字符串保存到文件
            FileUtil.saveFile(out.toString("gbk"), htmlFullPath);

        } catch (Exception e) {
            e.printStackTrace();

        } finally {
            if (doc != null) {
                try {
                    doc.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

参考文章:

Java使用ByteArrayOutputStream,依赖默认编码,如何解决_bytearrayoutputstream设置编码_一写代码就开心的博客-CSDN博客

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值