使用aspose实现doc转docx

1. 引入aspose依赖

2. 创建工具类

import com.aspose.words.Document;
import com.aspose.words.SaveFormat;
import org.apache.commons.io.output.ByteArrayOutputStream;
import org.springframework.util.FileCopyUtils;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

public class WordConvertUtil {
    /**
     * doc输入流转docx输入流
     * @param docInputStream
     * @return
     * @throws IOException
     */
    public static InputStream docConvertDocx(InputStream docInputStream) throws IOException {
        byte[] docBytes = FileCopyUtils.copyToByteArray(docInputStream);
        byte[] docxBytes = convertDocStream2docxStream(docBytes);
        return new ByteArrayInputStream(docxBytes);
    }

    private static byte[] convertDocStream2docxStream(byte[] arrays) {
        byte[] docxBytes = new byte[1];
        if (arrays != null && arrays.length > 0) {
            try (
                    ByteArrayOutputStream os = new ByteArrayOutputStream();
                    InputStream sbs = new ByteArrayInputStream(arrays);
            ) {
                Document doc = new Document(sbs);
                doc.save(os, SaveFormat.DOCX);
                docxBytes = os.toByteArray();
            } catch (Exception e) {
                System.out.println("出错啦");
            }
            return docxBytes;
        }
        return docxBytes;
    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
1. 使用Apache POI库进行换: ```java import org.apache.poi.hwpf.HWPFDocument; import org.apache.poi.hwpf.converter.WordToConverter; import org.apache.poi.hwpf.usermodel.Range; import java.io.*; public class DocToDocxConverter { public static void main(String[] args) throws Exception { // 读取doc文件 InputStream inputStream = new FileInputStream("test.doc"); HWPFDocument document = new HWPFDocument(inputStream); // 换为docx文件 WordToConverter converter = new WordToConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument()); converter.processDocument(document); // 写入docx文件 OutputStream outputStream = new FileOutputStream("test.docx"); converter.getDocument().write(outputStream); // 关闭流 outputStream.close(); inputStream.close(); } } ``` 2. 使用Aspose.Words库进行换: ```java import com.aspose.words.Document; import com.aspose.words.SaveFormat; import java.io.*; public class DocToDocxConverter { public static void main(String[] args) throws Exception { // 读取doc文件 InputStream inputStream = new FileInputStream("test.doc"); Document document = new Document(inputStream); // 换为docx文件 document.save("test.docx", SaveFormat.DOCX); // 关闭流 inputStream.close(); } } ``` 3. 使用JodConverter库进行换: ```java import com.documents4j.api.DocumentType; import com.documents4j.api.IConverter; import com.documents4j.job.LocalConverter; import java.io.*; public class DocToDocxConverter { public static void main(String[] args) throws Exception { // 创建IConverter对象 IConverter converter = LocalConverter.builder().build(); // 读取doc文件 InputStream inputStream = new FileInputStream("test.doc"); // 写入docx文件 OutputStream outputStream = new FileOutputStream("test.docx"); // 换为docx文件 converter.convert(inputStream).as(DocumentType.MS_WORD).to(outputStream).as(DocumentType.DOCX).execute(); // 关闭流 outputStream.close(); inputStream.close(); // 关闭IConverter对象 converter.shutDown(); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值