Usher_baby

导航

用aspose-word解决doc转docx工具类

首先pom中添加依赖(官网的收费!)

<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-words</artifactId>
<version>20.4.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/aspose-words.jar</systemPath>
</dependency>

以下为工具类:
/**
* 文档工具类
*/
public class DocumentUtils{
/**
* doc文件的输入流转换成docx的输入流
* @param inputStream
* @return
*/
@SneakyThrows
public static InputStream docToDocx(InputStream inputStream){
if(FileMagic.valueOf(inputStream) == FileMagic.OLE2) {
Document document = new Document(inputStream);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
document.save(outputStream, 20);
closeStream(outputStream);
return new ByteArrayInputStream(outputStream.toByteArray());
}else{
return inputStream;
}
}

private static void closeStream(OutputStream outputStream) throws IOException {
if(outputStream != null){
outputStream.close();
}
}

private static void closeStream(InputStream inputStream) throws IOException {
if(inputStream != null){
inputStream.close();
}
}
}

posted on 2022-03-30 10:20  Usher_baby  阅读(608)  评论(0编辑  收藏  举报