項目中遇到的word文檔在線預覽需求,查閱很多資料決定利用openoffice轉換word文檔爲pdf/html進行預覽實現。
1.下載openoffice4安裝 www.openoffice.org;
2.導入相關jar包
<dependency>
<groupId>org.artofsolving.jodconverter</groupId>
<artifactId>jodconverter-core</artifactId>
<version>3.0-beta-3</version>
</dependency>
<dependency>
<groupId>com.artofsolving</groupId>
<artifactId>jodconverter</artifactId>
<version>2.2.1</version>
<exclusions>
<exclusion>
<artifactId>commons-io</artifactId>
<groupId>commons-io</groupId>
</exclusion>
</exclusions>
</dependency>
3.openoffice服務啓動
安裝OpenOffice成功後,您可以進入<OpenOffice安裝目錄>/program/目錄並運行以下命令啓動OpenOffice服務:
4.openoffice轉換文檔代碼,這是一個工具類,將要預覽的文檔進行轉換
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
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.StreamOpenOfficeDocumentConverter;
public class Doc2HtmlUtil {
/**
* 轉換文件成pdf
*
* @param fromFileInputStream:
* @throws IOException
*/
public static String file2pdf(InputStream fromFileInputStream, String toFilePath,String type) throws Exception{
String timesuffix = IDUtil.getID();
String docFileName = null;
String htmFileName = null;
if("doc".equals(type)){
docFileName = timesuffix.concat(".doc");
htmFileName = timesuffix.concat(".pdf");
}else if("xls".equals(type)){
docFileName = timesuffix.concat(".xls");
htmFileName = timesuffix.concat(".pdf");
}else if("ppt".equals(type)){
docFileName = timesuffix.concat(".ppt");
htmFileName = timesuffix.concat(".pdf");
}else if("txt".equals(type)){
docFileName = timesuffix.concat(".txt");
htmFileName = timesuffix.concat(".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();
docInputFile.createNewFile();
/**
* 由fromFileInputStream構建輸入文件
*/
int bytesRead = 0;
byte[] buffer = new byte[1024 * 8];
OutputStream os = new FileOutputStream(docInputFile);
while ((bytesRead = fromFileInputStream.read(buffer)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
fromFileInputStream.close();
OpenOfficeConnection connection = new SocketOpenOfficeConnection(FSUtil.FS_FILE_CONVERT_HOST,8100);
connection.connect();
// convert
DocumentConverter converter = new StreamOpenOfficeDocumentConverter(connection);
converter.convert(docInputFile, htmlOutputFile);
connection.disconnect();
// 轉換完之後刪除word文件
docInputFile.delete();
return htmFileName;
}
/**
* 文件轉換成Html
* @param fromFileInputStream
* @param toFilePath
* @param type
* @return
* @throws Exception
*/
public static String file2Html (InputStream fromFileInputStream, String toFilePath,String type) throws Exception{
String timesuffix = IDUtil.getID();
String docFileName = null;
String htmFileName = null;
if("doc".equals(type)){
docFileName = timesuffix.concat(".doc");
htmFileName = timesuffix.concat(".html");
}else if("xls".equals(type)){
docFileName = timesuffix.concat(".xls");
htmFileName = timesuffix.concat(".html");
}else if("ppt".equals(type)){
docFileName = timesuffix.concat(".ppt");
htmFileName = timesuffix.concat(".html");
}else if("txt".equals(type)){
docFileName = timesuffix.concat(".txt");
htmFileName = timesuffix.concat(".html");
}else if("pdf".equals(type)){
docFileName = timesuffix.concat(".pdf");
htmFileName = timesuffix.concat(".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();
docInputFile.createNewFile();
/**
* 由fromFileInputStream構建輸入文件
*/
int bytesRead = 0;
byte[] buffer = new byte[1024 * 8];
OutputStream os = new FileOutputStream(docInputFile);
while ((bytesRead = fromFileInputStream.read(buffer)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
fromFileInputStream.close();
OpenOfficeConnection connection = new SocketOpenOfficeConnection(FSUtil.FS_FILE_CONVERT_HOST,8100);
connection.connect();
// convert
DocumentConverter converter = new StreamOpenOfficeDocumentConverter(connection);
converter.convert(docInputFile, htmlOutputFile);
connection.disconnect();
// 轉換完之後刪除word文件
docInputFile.delete();
return htmFileName;
}
}
5.action類調用轉換工具類進行轉換
//轉換爲pdf
@SuppressWarnings("deprecation")
private String convertToPDF(HttpServletRequest request, String fileName, InputStream fromFileInputStream, String currentId) {
if(FSUtil.isOfficeFile(fileName.toLowerCase())){
String convertFileSavePath=request.getRealPath("/")+FSUtil.FS_HADOOP_FILE_CONVERT_PATH+File.separatorChar+currentId;
File file=new File(convertFileSavePath);
if(!file.exists()){
file.mkdirs();
}
String type=fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
try {
if(type.endsWith("x")){
return Doc2HtmlUtil.file2pdf(fromFileInputStream, convertFileSavePath, type.substring(0, type.length()-1));
}else{
return Doc2HtmlUtil.file2pdf(fromFileInputStream, convertFileSavePath, type);
}
} catch (Exception e) {
log.error(e.getMessage());
return null;
}
}
return null;
}
//轉換爲html
@SuppressWarnings("deprecation")
public String convertToHtml(HttpServletRequest request,String fileName, InputStream fromFileInputStream, String currentId){
if(!FSUtil.isImage(fileName.toLowerCase())){
String convertFileSavePath=request.getRealPath("/")+FSUtil.FS_HADOOP_FILE_CONVERT_PATH+File.separatorChar+currentId;
File file=new File(convertFileSavePath);
if(!file.exists()){
file.mkdirs();
}
String type=fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
try {
if(type.endsWith("x")){
return Doc2HtmlUtil.file2Html(fromFileInputStream, convertFileSavePath, type.substring(0, type.length()-1));
}else{
return Doc2HtmlUtil.file2Html(fromFileInputStream, convertFileSavePath, type);
}
} catch (Exception e) {
log.error(e.getMessage());
return null;
}
}
return null;
}
convertFileSavePath爲轉換後的PDF格式文檔絕對地址返回給前端利用瀏覽器打開是先預覽