java实现doc文件流转docx文件流

news/2025/3/6 3:01:49/

java实现doc文件流转docx文件流

    • 1. 引入依赖
    • 2. InputStream转字节数组,并关闭流
    • 3. 使用ByteArrayOutputStream接收docx文件流
    • 4.字节数组转InputStream
    • 5. 完整代码

1. 引入依赖

<!-- https://mvnrepository.com/artifact/com.aspose/aspose-words -->
<dependency><groupId>com.aspose</groupId><artifactId>aspose-words</artifactId><version>18.2.0</version>
</dependency>

jar下载链接:https://pan.baidu.com/s/1203Xn9JgJ1tG6cWPkythAA?pwd=rea4
提取码:rea4

2. InputStream转字节数组,并关闭流

byte[] docBytes = FileCopyUtils.copyToByteArray(docInputStream);

3. 使用ByteArrayOutputStream接收docx文件流

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);

4.字节数组转InputStream

new ByteArrayInputStream(docxBytes)

5. 完整代码

    /*** doc转为docx* @param docInputStream* @return* @throws IOException*/public static InputStream convertDocIs2DocxIs(InputStream docInputStream) throws IOException {byte[] docBytes = FileCopyUtils.copyToByteArray(docInputStream);byte[] docxBytes = convertDocStream2docxStream(docBytes);return new ByteArrayInputStream(docxBytes);}// 将doc字节数组转换为docx字节数组public 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;}

http://www.ppmy.cn/news/972745.html

相关文章

【Redis】所以延迟双删有啥用

文章目录 1、何为延时双删2、常用缓存策略2.1、介绍2.2、先删缓存后更库2.3、先更库后删缓存2.4、使用场景 3、延时双删实现4、为什么要使用延时双删5、方案选择6、延时双删真的完美吗7、如何确定延时的时间 1、何为延时双删 延迟双删&#xff08;Delay Double Delete&#xf…

【el-tree查询并高亮】vue使用el-tree组件,搜索展开并选中对应节点,高亮搜索的关键字,过滤后高亮关键字,两种方法

第一种&#xff08;直接展开并高亮关键字&#xff09; 效果图这样的&#xff0c;会把所有的有这些关键字的节点都展开 代码&#xff1a; 这里的逻辑就是通过递归循环把所有和关键字匹配的节点筛选出来 然后通过setCheckedKeys方法把他展开选中 然后通过filterReal把关键字高亮…

[数学建模] [2019年A 模拟练习][层次分析法、熵值法、多目标优化、主成分分析法] 4. 深圳居民健康水平评估与测控模型研究

1、前言 2019年“深圳杯”数学建模挑战赛A题 原题&#xff0c;这个是当时学校内部校赛所作&#xff0c;为了拿到参加国赛名额&#xff0c;也权当是做一个简单的练手。 本次练习属于综合评判类&#xff0c;常用的方法无非 层次分析法、熵值法、多目标优化、主成分分析法 等&am…

Sentinel 规则持久化到 Nacos

一、Sentinel规则管理模式&#x1f349; Sentinel的控制台规则管理有三种模式&#xff1a; 原始模式&#x1f95d; 原始模式&#xff1a;控制台配置的规则直接推送到Sentinel客户端&#xff0c;也就是我们的应用。然后保存在内存中&#xff0c;服务重启则丢失 pull模式&#…

Vue为什么使用return返回data对象的原因

简介&#xff1a;在Vue中&#xff0c;我们经常看到组件的data选项是一个函数&#xff0c;并在函数中使用return语句返回一个对象。这种用法在Vue的官方文档和示例中被广泛采用。本文将深入探讨为什么Vue要求data必须是一个返回对象的函数&#xff0c;以及这种设计背后的原理。 …

15个 AI-powered应用,加速学术写作和阅读

文章目录 写作方面1.Jenni AI&#xff08;推荐&#xff09;2.Paperpal 阅读方面3.Schoarlcy4.chatpdf5.Casper6.SciSpace&#xff08;推荐&#xff09; 文献管理/写论文笔记用7.lateral8. ClioVis9.Glasp10. Audiopen 学术目的的搜索引擎11. Consensus&#xff08;推荐&#xf…

Spring期刊模板中,引用Appendix中的表格时,页面显示正确但链接跳转错误

错误描述&#xff1a; 正文中有Table1&#xff0c;Table2&#xff0c; Table3. Appendix中有Table A1&#xff0c; Table A2&#xff0c; Table A3 appendix中引用 Table A1&#xff0c;生成pdf后。appendix中显示Table A1&#xff0c;正确&#xff0c;但是点击Table A1以后&…

科研办公相关的人工智能(AI)工具整理

参考来源&#xff1a; 同济学姐分享chatGPT及常见AI工具科普&教程&#xff5c;副业搞钱途径&#xff5c;效率神器整合chatGPT的新必应&#xff08;NewBing chat&#xff09;简直就是科研神器&#xff01;ChatGPT搭配闪击PPT三分钟生成PPT&#xff0c;你只需要输入一个标题…