由于Vue预览插件docx-preview不支持doc,因此需要将doc转成docx,这里使用的是aspose.word jar包(非开源,需要破解或付费,否则有水印和限制)
1.下载aspose.word包,我已经上传站里了,也可以找别人上传的
2.在项目根目录下新建license.xml文件,内容如下:
<!--Aspose.Words授权配置-->
<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>
3.调用方法,我是先把doc文件转成字节数组(可以使用FileCopyUtils.copyToByteArray),再进行转换
import com.aspose.words.SaveFormat;
private static byte[] docStream2docxStream(byte[] arrays) throws Exception {
//aspose.words授权
InputStream is = new FileInputStream("license.xml");
License license = new License();
license.setLicense(is);
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) {
}
}
return docxBytes;
}
当然aspose.word还有其它格式转换操作,这里就不一一展示了…