Java实现doc文件和docx文件互转的几种方式

方式1:利用aspose进行转换

Jar包地址:

链接:https://pan.baidu.com/s/1pwJ-gwghInoU-_M60ZF_YQ
提取码:3eek

// 将doc输入流转换为docx输入流
private static InputStream convertDocIs2DocxIs(InputStream docInputStream) throws IOException {byte[] docBytes = FileCopyUtils.copyToByteArray(docInputStream);byte[] docxBytes = convertDocStream2docxStream(docBytes);return new ByteArrayInputStream(docxBytes);
}
// 将doc字节数组转换为docx字节数组
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)) {com.aspose.words.Document doc = new com.aspose.words.Document(sbs);doc.save(os, SaveFormat.DOCX);docxBytes = os.toByteArray();} catch (Exception e) {System.out.println("出错啦");} }return docxBytes;
}

备注:该方式同样可以实现将docx转换为doc文件,只需指定SaveFormat的格式。

方式2:spire.doc.free

首先导入依赖

<dependency><groupId>e-iceblue</groupId><artifactId>spire.doc.free</artifactId><version>3.9.0</version>
</dependency>

代码:

Document document = new Document();
document.loadFromFile("源文件路径");
document.saveToFile("目标文件的路径", FileFormat.Docx);

方式3:同样可以使用JodConverter转换

jod可以实现多种文档之间的相互转换,具体案例百度上有很多。

方式4:利用documents4j实现

首先是依赖

 <dependency><groupId>com.documents4j</groupId><artifactId>documents4j-local</artifactId><version>1.0.3</version></dependency><dependency><groupId>com.documents4j</groupId><artifactId>documents4j-transformer-msoffice-word</artifactId><version>1.0.3</version></dependency>

示例代码:

public static void convert(String fileType) {File inputWord = new File("D:\\test_workspace\\testdemo\\src\\main\\resources\\targetFile.docx");File outputFile = new File("D:\\zzz.doc");try (InputStream docxInputStream = new FileInputStream(inputWord);OutputStream outputStream = new FileOutputStream(outputFile)) {IConverter converter = LocalConverter.builder().build();boolean flag = false;if("docx".equals(fileType)){// docx转docflag = converter.convert(docxInputStream).as(DocumentType.DOCX).to(outputStream).as(DocumentType.DOC).execute();} else if ("doc".equals(fileType)){ //flag = converter.convert(docxInputStream).as(DocumentType.DOC).to(outputStream).as(DocumentType.PDF).execute();}if (flag) { converter.shutDown();}System.out.println("转换成功");} catch (Exception e) {e.printStackTrace();}
}

备注:有人说linux下documents4j转换会出现异常,具体没有去验证。

Java实现DOCX文件和DOC文件互转相关推荐

  1. pdf文件转doc文件

    依赖 <!-- pdf依赖--><dependency><groupId>org.apache.pdfbox</groupId><artifact ...

  2. .chm文件与.doc文件互相转换(一).chm转.doc

    简介 本文将描述如何将一个.chm文档转化为一个.doc文档. 准备工具:下载一个CHM2Word 2012软件即可. 本文CHM2Word 2012的未破解,试用30天,网上应该有破解版. 安装 转 ...

  3. java 读取 doc_java如何读取doc文件

    使用poi: package org.apache.poi.hwpf; 19 20 import org.apache.poi.hwpf.model.FileInformationBlock; 21 ...

  4. 【Java】操作doc文件

    读取txt文件 在Java里要对文件进行操作,需要用到IO流. public static void main(String args[]){File f = new File("test. ...

  5. Freemark 模板生成doc文件,xml doc文件转docx 文件,docx文件转pdf文件

    freemark 模板生成doc文件,此doc文件为xml格式,无法直接转pdf 需要doc转docx文件,才能转pdf 方法1:docx4j 转 pdf 方法2: e-iceblue docx 转 ...

  6. open一个绝对路径地址 python_实例15:用Python批量转换doc文件为docx文件

    python-docx模块虽然强大,但却不能处理后缀为".doc"的word文件.如果强制读取doc文件,将会报如下错误. import docx #导入docx库doc = do ...

  7. doc转docx文件会乱吗_利用python将doc文件转换为docx

    需求:最近在研究word文档的抽取,发现python中docx库只能提取以docx结尾的文件,因此需要将doc文件转换为docx. 基础知识了解 1.什么是doc? 汉语:文档(外语全称:Docume ...

  8. java处理docx文件中的图片

    public static void main(String[] args) throws Exception {InputStream fileInputStream = null;FileOutp ...

  9. docx文件是什么?如何打开后缀名.docx格式的word2007文件?

    < DOCTYPE html PUBLIC -WCDTD XHTML TransitionalEN httpwwwworgTRxhtmlDTDxhtml-transitionaldtd> ...

  10. DOC文件丢失怎么恢复?用这7种方法找回

    在日常生活和工作中,我们经常会因为各种原因丢失重要的DOC文件,这给我们带来了不小的困扰.但是,不必担心,在本文中,我们将带大家了解一下DOC文件丢失怎么恢复. 关于DOC文件 DOC文件是指一种微软 ...

最新文章

  1. 通过Windows的远程桌面连接树莓派
  2. 右边补0 润乾报表_关于润乾报表的补充说明 -
  3. “人工智能的数理基础”主题论坛,五位学者从数学角度解决AI问题
  4. #中delay函数_ECBM系列教程3:闪烁灯——delay函数的用法。
  5. ORA-12541:TNS没有监听器
  6. CERL 2.0 预告:Erlang Style Concurrency + 状态机
  7. 九、PyQt5 QLineEdit输入的子网字符串校验QRegExp
  8. 2017甘肃省计算机二级考试,甘肃省2017年计算机二级考试网上报名须知及流程
  9. left join效率为什么低_为什么自考的你学习效率特别低?对号入座
  10. MATLAB 绘图函数总结
  11. SU草图大师必备实用插件,拿走不谢!
  12. js获取当前屏幕宽度方法
  13. 解决mysql登录出现10061的问题
  14. php怎样做艺术字体,用ps打造科幻艺术字体
  15. 基于Python的拉勾网的模拟登录获取cookie
  16. P2P模式文件传输网络应用的开发
  17. 什么是TF-IDF?
  18. 安卓/苹果/支付宝sdk拉起支付宝h5/app支付
  19. 曾经沧海难为水,除却巫山不是云。
  20. 防止U盘中毒的一个小技巧

热门文章

  1. 多少秒算长镜头_什么是长镜头画面(电影里的长镜头有哪些分类)
  2. 百度面试题:求绝对值最小的数
  3. 【b站雅思笔记】Simon‘s IELTS Course - 阅读部分
  4. 『Python开发实战菜鸟教程』实战篇:一文带你了解人脸识别应用原理及手把手教学实现自己的人脸识别项目
  5. 固定偏置放大电路为何不能保证静态工作点的稳定性?
  6. wrapper.and的用法
  7. 5.无线射频基础知识介绍_无线射频基础介绍
  8. css3效果隔两秒旋转然后停两秒再继续旋转,无限循环
  9. kali-top10-Nmap
  10. matlab实现序列卷积和运算,用matlab计算序列卷积和并绘图