1.首先下载jar包 aspose-words-19.5.jar
2.cmd命令
mvn install:install-file -DgroupId=com.aspose.words -DartifactId=aspose-words -Dversion=19.5 -Dpackaging=jar -Dfile=下载的jar包所在地址
3.添加依赖
<dependency> <groupId>com.aspose.words</groupId> <artifactId>aspose-words</artifactId> <version>19.5</version> </dependency>
4.添加 license.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- -->
<License>
<Data>
<Products>
<Product>Aspose.Total for Java</Product>
<Product>Aspose.Words for Java</Product>
</Products>
<EditionType>Enterprise</EditionType>
<SubscriptionExpiry>20991231</SubscriptionExpiry>
<LicenseExpiry>20991231</LicenseExpiry>
<SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>
</Data>
<Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature>
</License>
5.创建工具类
public class WordConvertUtil {
public static boolean getLicense() {
boolean result = false;
try {
//这个地方xml文件的物理路径要写正确
Resource resource = new ClassPathResource("license.xml");
InputStream is = resource.getInputStream();
License aposeLic = new License();
aposeLic.setLicense(is);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 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;
}
}
6.直接调用工具类方法即可