一、首先下載安裝openOffice軟件,下載地址:http://www.openoffice.org/download/index.html
當然,下載速度特別慢,下面是百度網盤地址。
鏈接:https://pan.baidu.com/s/1FfQa14WYXuJU2CTQf34gew
提取碼:4q08
下載完一步一步下一步安裝好,就行了。沒什麼特別的。
二、引入jar包
<dependency>
<groupId>org.jodconverter</groupId>
<artifactId>jodconverter-core</artifactId>
<version>4.0.0-RELEASE</version>
</dependency>
三、上代碼,註釋都很詳細,應該能看懂,主要原理呢就是講office文件轉換爲PDF,頁面可以直接打開PDF,相當於預覽
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import org.apache.commons.io.FileUtils;
import org.jodconverter.OfficeDocumentConverter;
import org.jodconverter.office.DefaultOfficeManagerBuilder;
import org.jodconverter.office.OfficeManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
@RestController
@RequestMapping("/filePreview")
public class FilePreview {
private static final Logger logger = LoggerFactory.getLogger(FilePreview.class);
//這個是爲了將uri轉爲File對象的一個臨時路徑
private String tempPath = System.getProperty("java.io.tmpdir");
/**
* 預覽文件
*
* @param response 響應對象
* @throws Exception
*/
@RequestMapping("/readFile")
public void readFile(HttpServletResponse response) throws Exception {
try {
File file = null;
//文件路徑
String uri = "http://xxx.xxx.xxx.xxx/group1/M00/00/02/wKgAuV5NSPyAK7-ZAAA8pSZux3M89.docx";
String fileName = uri.substring(uri.lastIndexOf("/") + 1);
//通過FileUtils將服務器文件下載的本地臨時文件夾
URL url = new URL(uri);
FileUtils.copyURLToFile(url, new File(tempPath + File.separator + fileName));
//創建文件對象
file = new File(tempPath + File.separator + fileName);
InputStream inputStream = null;
OutputStream outputStream = null;
//截取文件後綴,判斷是pdf還是word還是excel,若是pdf直接讀否則轉pdf再讀
String fileSuffix = fileName.substring(fileName.lastIndexOf("."));
if (!fileSuffix.equals("pdf")) {
file = openOfficeToPDF(fileName, file);
}
//創建文件輸入流
if (file != null) {
inputStream = new FileInputStream(file);
}
response.setContentType("application/pdf");
outputStream = response.getOutputStream();
byte[] b = new byte[1024];
int length = 0;
while ((length = inputStream.read(b)) != -1) {
outputStream.write(b);
}
inputStream.close();
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
logger.error(e.getMessage(), e);
}
}
/**
* 將office文檔轉換爲pdf文檔
*
* @return
*/
public static File openOfficeToPDF(String fileName, File file) {
try {
//截取源文件文件名
String sourceFileName = fileName.substring(0, fileName.lastIndexOf("."));
//啓動openOffice
DefaultOfficeManagerBuilder builder = new DefaultOfficeManagerBuilder();
//文件轉換爲pdf後會在電腦臨時存儲,重新預覽時,清空之前之前的文件夾
File tempfile = new File("C:/pdfFile/");
//這個方法是我用的工具類,大家可以自己寫下,jar包我就沒有放了
FileUtil.del(tempfile);
//然後重新將創建file文件對象,用來存儲轉換完成的PDF文件
String after_convert_file_path = "C:/pdfFile/" + sourceFileName + ".pdf";
File outputFile = new File(after_convert_file_path);
if (!outputFile.getParentFile().exists()) {
//如果上級目錄不存在則創建一個
outputFile.getParentFile().mkdirs();
}
//指定openOffice4安裝路徑,並開啓
String openOffice4path = "C:/Program Files (x86)/OpenOffice 4";
builder.setOfficeHome(openOffice4path);
OfficeManager officeManager = builder.build();
officeManager.start();
OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
//文件轉換PDF
converter.convert(file, outputFile);
officeManager.stop();
return outputFile;
} catch (Exception e) {
e.printStackTrace();
logger.error(e.getMessage(), e);
}
return null;
}
}
注意:因爲我用的FastDfs文件服務器,所以我的文件路徑是http格式的uri,如果是本地文件就更簡單了,看下面
File file = null;
//文件路徑
String uri = "http://xxx.xxx.xxx.xxx/group1/M00/00/02/wKgAuV5NSPyAK7-ZAAA8pSZux3M89.docx";
String fileName = uri.substring(uri.lastIndexOf("/") + 1);
//通過FileUtils將服務器文件下載的本地臨時文件夾
URL url = new URL(uri);
FileUtils.copyURLToFile(url, new File(tempPath + File.separator + fileName));
//創建文件對象
file = new File(tempPath + File.separator + fileName);
上面這段直接改成
File file = new File("文件路徑") 就好了