Java實現線上預覽--openOffice實現
Java實現線上預覽–openOffice實現
簡介
之前有寫了poi實現線上預覽的文章,裡面也說到了使用openOffice也可以做到,這裡就詳細介紹一下。
我的實現邏輯有兩種:
一、利用jodconverter(基於OpenOffice服務)將檔案(.doc、.docx、.xls、.ppt)轉化為html格式。
二、利用jodconverter(基於OpenOffice服務)將檔案(.doc、.docx、.xls、.ppt)轉化為pdf格式。
轉換成html格式大家都能理解,這樣就可以直接在瀏覽器上查看了,也就實現了線上預覽的功能;轉換成pdf格式這點,需要使用者安裝了Adobe Reader XI,這樣你會發現把pdf直接拖到瀏覽器頁面可以直接開啟預覽,這樣也就實現了線上預覽的功能。
將檔案轉化為html格式或者pdf格式
話不多說,直接上程式碼。
package com.pdfPreview.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ConnectException;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
/**
* 利用jodconverter(基於OpenOffice服務)將檔案(*.doc、*.docx、*.xls、*.ppt)轉化為html格式或者pdf格式,
* 使用前請檢查OpenOffice服務是否已經開啟, OpenOffice程序名稱:soffice.exe | soffice.bin
*
* @author yjclsx
*/
public class Doc2HtmlUtil {
private static Doc2HtmlUtil doc2HtmlUtil;
/**
* 獲取Doc2HtmlUtil例項
*/
public static synchronized Doc2HtmlUtil getDoc2HtmlUtilInstance() {
if (doc2HtmlUtil == null) {
doc2HtmlUtil = new Doc2HtmlUtil();
}
return doc2HtmlUtil;
}
/**
* 轉換檔案成html
*
* @param fromFileInputStream:
* @throws IOException
*/
public String file2Html(InputStream fromFileInputStream, String toFilePath,String type) throws IOException {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String timesuffix = sdf.format(date);
String docFileName = null;
String htmFileName = null;
if("doc".equals(type)){
docFileName = "doc_" + timesuffix + ".doc";
htmFileName = "doc_" + timesuffix + ".html";
}else if("docx".equals(type)){
docFileName = "docx_" + timesuffix + ".docx";
htmFileName = "docx_" + timesuffix + ".html";
}else if("xls".equals(type)){
docFileName = "xls_" + timesuffix + ".xls";
htmFileName = "xls_" + timesuffix + ".html";
}else if("ppt".equals(type)){
docFileName = "ppt_" + timesuffix + ".ppt";
htmFileName = "ppt_" + timesuffix + ".html";
}else{
return null;
}
File htmlOutputFile = new File(toFilePath + File.separatorChar + htmFileName);
File docInputFile = new File(toFilePath + File.separatorChar + docFileName);
if (htmlOutputFile.exists())
htmlOutputFile.delete();
htmlOutputFile.createNewFile();
if (docInputFile.exists())
docInputFile.delete();
docInputFile.createNewFile();
/**
* 由fromFileInputStream構建輸入檔案
*/
try {
OutputStream os = new FileOutputStream(docInputFile);
int bytesRead = 0;
byte[] buffer = new byte[1024 * 8];
while ((bytesRead = fromFileInputStream.read(buffer)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
fromFileInputStream.close();
} catch (IOException e) {
}
OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
try {
connection.connect();
} catch (ConnectException e) {
System.err.println("檔案轉換出錯,請檢查OpenOffice服務是否啟動。");
}
// convert
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
converter.convert(docInputFile, htmlOutputFile);
connection.disconnect();
// 轉換完之後刪除word檔案
docInputFile.delete();
return htmFileName;
}
/**
* 轉換檔案成pdf
*
* @param fromFileInputStream:
* @throws IOException
*/
public String file2pdf(InputStream fromFileInputStream, String toFilePath,String type) throws IOException {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String timesuffix = sdf.format(date);
String docFileName = null;
String htmFileName = null;
if("doc".equals(type)){
docFileName = "doc_" + timesuffix + ".doc";
htmFileName = "doc_" + timesuffix + ".pdf";
}else if("docx".equals(type)){
docFileName = "docx_" + timesuffix + ".docx";
htmFileName = "docx_" + timesuffix + ".pdf";
}else if("xls".equals(type)){
docFileName = "xls_" + timesuffix + ".xls";
htmFileName = "xls_" + timesuffix + ".pdf";
}else if("ppt".equals(type)){
docFileName = "ppt_" + timesuffix + ".ppt";
htmFileName = "ppt_" + timesuffix + ".pdf";
}else{
return null;
}
File htmlOutputFile = new File(toFilePath + File.separatorChar + htmFileName);
File docInputFile = new File(toFilePath + File.separatorChar + docFileName);
if (htmlOutputFile.exists())
htmlOutputFile.delete();
htmlOutputFile.createNewFile();
if (docInputFile.exists())
docInputFile.delete();
docInputFile.createNewFile();
/**
* 由fromFileInputStream構建輸入檔案
*/
try {
OutputStream os = new FileOutputStream(docInputFile);
int bytesRead = 0;
byte[] buffer = new byte[1024 * 8];
while ((bytesRead = fromFileInputStream.read(buffer)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
fromFileInputStream.close();
} catch (IOException e) {
}
OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
try {
connection.connect();
} catch (ConnectException e) {
System.err.println("檔案轉換出錯,請檢查OpenOffice服務是否啟動。");
}
// convert
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
converter.convert(docInputFile, htmlOutputFile);
connection.disconnect();
// 轉換完之後刪除word檔案
docInputFile.delete();
return htmFileName;
}
public static void main(String[] args) throws IOException {
Doc2HtmlUtil coc2HtmlUtil = getDoc2HtmlUtilInstance();
File file = null;
FileInputStream fileInputStream = null;
file = new File("D:/poi-test/exportExcel.xls");
fileInputStream = new FileInputStream(file);
// coc2HtmlUtil.file2Html(fileInputStream, "D:/poi-test/openOffice/xls","xls");
coc2HtmlUtil.file2pdf(fileInputStream, "D:/poi-test/openOffice/xls","xls");
file = new File("D:/poi-test/test.doc");
fileInputStream = new FileInputStream(file);
// coc2HtmlUtil.file2Html(fileInputStream, "D:/poi-test/openOffice/doc","doc");
coc2HtmlUtil.file2pdf(fileInputStream, "D:/poi-test/openOffice/doc","doc");
file = new File("D:/poi-test/週報模版.ppt");
fileInputStream = new FileInputStream(file);
// coc2HtmlUtil.file2Html(fileInputStream, "D:/poi-test/openOffice/ppt","ppt");
coc2HtmlUtil.file2pdf(fileInputStream, "D:/poi-test/openOffice/ppt","ppt");
file = new File("D:/poi-test/test.docx");
fileInputStream = new FileInputStream(file);
// coc2HtmlUtil.file2Html(fileInputStream, "D:/poi-test/openOffice/docx","docx");
coc2HtmlUtil.file2pdf(fileInputStream, "D:/poi-test/openOffice/docx","docx");
}
}
轉換成html和轉換成pdf的過程幾乎一樣,只是在建立輸出的File時前者命名為XXX.html,後者命名為XXX.pdf,在執行converter.convert(docInputFile, htmlOutputFile);時,jodconverter會自己根據檔案型別名轉換成對應的檔案。
注意,main方法裡別file2Html和file2pdf都呼叫,會報錯的,要麼轉html,要麼轉pdf,只能選一個。還有就是在執行之前,需要啟動openOffice的服務:在openOffice目錄下的命令視窗中執行soffice -headless -accept=”socket,host=127.0.0.1,port=8100;urp;” -nofirststartwizard即可啟動。
以上需要引入jodconverter的jar包。
再分享一下我老師大神的人工智慧教程吧。零基礎!通俗易懂!風趣幽默!希望你也加入到我們人工智慧的隊伍中來!http://www.captainbed.net