最近一直在弄在線教學(xué)平臺(tái),然后就需要實(shí)現(xiàn)在線預(yù)覽文件功能.之前資源庫(kù)一直用的
libreoffice
實(shí)現(xiàn)Office
轉(zhuǎn)libreoffice
是單線程的买雾,作為一個(gè)在線教學(xué)平臺(tái)顯然是不靠譜的.所以今天分享另一個(gè)純Java
實(shí)現(xiàn)Office
轉(zhuǎn)Aspose
- 首先我們先引入響應(yīng)的
pom
配置(由于默認(rèn)maven倉(cāng)庫(kù)下不下來(lái).文末提供下載地址)
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-words</artifactId>
<version>20.7</version>
<classifier>jdk17</classifier>
</dependency>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-slides</artifactId>
<version>20.7</version>
<classifier>jdk16</classifier>
</dependency>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-cells</artifactId>
<version>20.7</version>
</dependency>
采用工廠模式進(jìn)行office系列轉(zhuǎn)換
- 新建接口而
IFileConvert
public interface IFileConvert {
/**
* pdf文件后綴
*/
String FILE_PDF_SUFFIX = ".pdf";
default String getResultPath(String filePath, String fileSuffix){
// 什么將要返回的pdf路徑
int i = filePath.lastIndexOf(".");
String path = filePath.substring(0, i);
return path + fileSuffix;
}
String fileToPdf(String filePath);
}
- 實(shí)現(xiàn)
WORD
文件轉(zhuǎn)換,新建WordsFileConvert
public class WordsFileConvert implements IFileConvert {
private boolean getLicense() {
boolean result = false;
InputStream is = null;
try {
ClassLoader loader = Thread.currentThread().getContextClassLoader();
is = loader.getResourceAsStream("license.xml");
License license = new License();
license.setLicense(is);
result = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
@Override
public String fileToPdf(String filePath) {
//去除水印
if (!getLicense()) {
return null;
}
String pdfPath = null;
FileOutputStream os = null;
try {
pdfPath = getResultPath(filePath, FILE_PDF_SUFFIX);
os = new FileOutputStream(pdfPath);
Document doc = new Document(filePath);
doc.save(os, SaveFormat.PDF);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return pdfPath;
}
}
- 實(shí)現(xiàn)
PPT
文件轉(zhuǎn)換,新建SlidesFileConvert
public class SlidesFileConvert implements IFileConvert {
private boolean getLicense() {
boolean result = false;
InputStream is = null;
try {
ClassLoader loader = Thread.currentThread().getContextClassLoader();
is = loader.getResourceAsStream("license.xml");
License license = new License();
license.setLicense(is);
result = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
@Override
public String fileToPdf(String filePath) {
if (!getLicense()) {
return null;
}
String pdfPath = null;
FileOutputStream os = null;
try {
pdfPath = getResultPath(filePath, FILE_PDF_SUFFIX);
os = new FileOutputStream(pdfPath);
Presentation pres = new Presentation(filePath);
pres.save(os, SaveFormat.Pdf);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return pdfPath;
}
}
- 實(shí)現(xiàn)
EXCEL
文件轉(zhuǎn)換,新建CellsFileConvert
public class CellsFileConvert implements IFileConvert {
private boolean getLicense() {
boolean result = false;
InputStream is = null;
try {
ClassLoader loader = Thread.currentThread().getContextClassLoader();
is = loader.getResourceAsStream("license.xml");
License license = new License();
license.setLicense(is);
result = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
@Override
public String fileToPdf(String filePath) {
if (!getLicense()) {
return null;
}
String pdfPath = null;
FileOutputStream os = null;
try {
pdfPath = getResultPath(filePath, FILE_PDF_SUFFIX);
os = new FileOutputStream(pdfPath);
Workbook workbook = new Workbook(filePath);
workbook.save(os, SaveFormat.PDF);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return pdfPath;
}
}
- 在
resources
目錄下新建license.xml
文件
<?xml version="1.0" encoding="UTF-8" ?>
<License>
<Data>
<Products>
<Product>Aspose.Total 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>
- 新建工廠枚舉
FileConvertEnum
public enum FileConvertEnum {
PPT("PPT", new SlidesFileConvert()),
WORD("WORD", new WordsFileConvert()),
EXCEL("EXCEL", new CellsFileConvert()),
;
private String fileFormat;
private IFileConvert fileConvert;
FileConvertEnum(String fileFormat, IFileConvert fileConvert) {
this.fileFormat = fileFormat;
this.fileConvert = fileConvert;
}
public String getFileFormat() {
return fileFormat;
}
public IFileConvert getFileConvert() {
return fileConvert;
}
public static IFileConvert getFileConvert(String fileFormat) {
for (FileConvertEnum fileConvertEnum : values()) {
if (fileConvertEnum.fileFormat.equals(fileFormat)){
return fileConvertEnum.fileConvert;
}
}
return null;
}
}
- 使用方式
public static void main(String[] args) {
IFileConvert fileConvert = FileConvertEnum.getFileConvert("WORD");
String pdfUri = fileConvert.fileToPdf("/home/abelethan/Desktop/test.doc");
}