最近搞web項目,使用框架struts+spring+jpa實現,做到項目裏面一個在線預覽功能,試過無數的方法,最後得到了一個非常使用的方法,這方法也是我看過多篇博客的出來的,僅限參考。
效果圖如下:
第一步:
通過第三方軟件openoffice將office文檔ppt,pptx,doc,docx,xls,xlsx轉換成pdf文檔;
openoffice下載鏈接:http://www.openoffice.org/zh-cn/download/,
第二步:
JODConverter一個Java的OpenDocument 文件轉換器,導入其相關的jar包
下載地址:http://download.csdn.net/detail/tan313/9041821
第三步:
進行安裝文件,在進行項目開發前,必須啓動openoffice,我這裏不需要之前啓動openoffice,啓動openoffice寫在代碼中,使用代碼進行啓動;不過你唯一要改的就是你的openoffice安裝路徑,這裏在後邊我會說到的。
上面相關jar包導入後,而且openoffice也安裝好後,就可以進行項目開發:
首先給出工具類,很重要:
/**
*
*/
package com.sdbd.utils;
import java.io.File;
import java.util.Date;
import java.util.regex.Pattern;
import org.artofsolving.jodconverter.OfficeDocumentConverter;
import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;
import org.artofsolving.jodconverter.office.OfficeManager;
/**
* 這是一個工具類,主要是爲了使Office2003-2007全部格式的文檔(.doc|.docx|.xls|.xlsx|.ppt|.pptx)
* 轉化爲pdf文件<br>
* Office2010的沒測試<br>
*
* @date 2012-11-5
* @author xhw
*
*/
public class Office2PDF {
/**
* 使Office2003-2007全部格式的文檔(.doc|.docx|.xls|.xlsx|.ppt|.pptx) 轉化爲pdf文件<br>
*
* @param inputFilePath
* 源文件路徑,如:"e:/test.docx"
* @param outputFilePath
* 目標文件路徑,如:"e:/test_docx.pdf"
* @return
*/
public boolean openOfficeToPDF(String inputFilePath, String outputFilePath) {
return office2pdf(inputFilePath, outputFilePath);
}
/**
* 根據操作系統的名稱,獲取OpenOffice.org 3的安裝目錄<br>
* 如我的OpenOffice.org 3安裝在:C:/Program Files (x86)/OpenOffice.org 3<br>
*
* @return OpenOffice.org 3的安裝目錄
*/
public String getOfficeHome() {
String osName = System.getProperty("os.name");
System.out.println("操作系統名稱:"+osName);
if (Pattern.matches("Linux.*", osName)) {
return "/opt/openoffice.org3";
} else if (Pattern.matches("Windows.*", osName)) {
return "C:/Program Files/OpenOffice 4";
} else if (Pattern.matches("Mac.*", osName)) {
return "/Application/OpenOffice.org.app/Contents";
}
return null;
}
/**
* 連接OpenOffice.org 並且啓動OpenOffice.org
*
* @return
*/
public OfficeManager getOfficeManager() {
DefaultOfficeManagerConfiguration config = new DefaultOfficeManagerConfiguration();
// 獲取OpenOffice.org 3的安裝目錄
String officeHome = getOfficeHome();
config.setOfficeHome(officeHome);
// 啓動OpenOffice的服務
OfficeManager officeManager = config.buildOfficeManager();
officeManager.start();
return officeManager;
}
/**
* 轉換文件
*
* @param inputFile
* @param outputFilePath_end
* @param inputFilePath
* @param outputFilePath
* @param converter
*/
public void converterFile(File inputFile, String outputFilePath_end, String inputFilePath, String outputFilePath, OfficeDocumentConverter converter) {
File outputFile = new File(outputFilePath_end);
// 假如目標路徑不存在,則新建該路徑
if (!outputFile.getParentFile().exists()) {
outputFile.getParentFile().mkdirs();
}
converter.convert(inputFile, outputFile);
System.out.println("文件:" + inputFilePath + "\n轉換爲\n目標文件:" + outputFile + "\n成功!");
}
/**
* 使Office2003-2007全部格式的文檔(.doc|.docx|.xls|.xlsx|.ppt|.pptx) 轉化爲pdf文件<br>
*
* @param inputFilePath
* 源文件路徑,如:"e:/test.docx"
* @param outputFilePath
* 目標文件路徑,如:"e:/test_docx.pdf"
* @return
*/
public boolean office2pdf(String inputFilePath, String outputFilePath) {
boolean flag = false;
OfficeManager officeManager = getOfficeManager();
// 連接OpenOffice
OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
long begin_time = new Date().getTime();
if (null != inputFilePath) {
File inputFile = new File(inputFilePath);
// 判斷目標文件路徑是否爲空
if (null == outputFilePath) {
// 轉換後的文件路徑
String outputFilePath_end = getOutputFilePath(inputFilePath);
if (inputFile.exists()) {// 找不到源文件, 則返回
converterFile(inputFile, outputFilePath_end, inputFilePath, outputFilePath, converter);
flag = true;
}
} else {
if (inputFile.exists()) {// 找不到源文件, 則返回
converterFile(inputFile, outputFilePath, inputFilePath, outputFilePath, converter);
flag = true;
}
}
officeManager.stop();
} else {
System.out.println("con't find the resource");
}
long end_time = new Date().getTime();
System.out.println("文件轉換耗時:[" + (end_time - begin_time) + "]ms");
return flag;
}
/**
* 獲取輸出文件
*
* @param inputFilePath
* @return
*/
public String getOutputFilePath(String inputFilePath) {
String outputFilePath = inputFilePath.replaceAll("." + getPostfix(inputFilePath), ".pdf");
return outputFilePath;
}
/**
* 獲取inputFilePath的後綴名,如:"e:/test.pptx"的後綴名爲:"pptx"<br>
*
* @param inputFilePath
* @return
*/
public String getPostfix(String inputFilePath) {
return inputFilePath.substring(inputFilePath.lastIndexOf(".") + 1);
}
}
在該工具類中,你需要改動的就是getOfficeHome()函數中返回值爲你的openoffice的安裝路徑。
工具類好了,在web中當我們點擊預覽按鈕提交到一個action或者servlet處理
在處理類中唯一需要的參數是你預覽原文件的的路徑,記住,是絕對路徑
根據相對路徑獲取絕對路徑方法:
String realpathdir = request.getSession().getServletContext().getRealPath(pathdir);
獲取到絕對路徑就可以調用工具類進行轉換:
office2pdf.openOfficeToPDF(filePath, 你需要存儲的路徑+"/" + 文件名 +".pdf");
filePath爲原文件的絕對路徑,第二個參數爲你需要存儲的路徑和文件名,在這裏我處理的方法寫一個相對路徑,然後獲取其絕對路徑,後邊就跟上你轉換的文件名了。
轉換好後,就開始進行預覽,
部門代碼如下:
file = new File(convertrealpath);//convertrealpath爲你轉換好後的文件的絕對路徑
URL u = new URL("file:///" + convertpath);
BufferedInputStream br = new BufferedInputStream(new FileInputStream(file));
byte[] buf = new byte[1024];
int len = 0;
response.reset(); // 非常重要
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "inline; filename=" + java.net.URLEncoder.encode(file.getName(), "UTF-8"));
OutputStream out = response.getOutputStream();
while ((len = br.read(buf)) > 0)
out.write(buf, 0, len);
br.close();
out.close();
return null;
以上就可以做到在線預覽。經過檢查,office系列文檔ppt,pptx,xls,xlsx,doc,docx都能夠預覽。
項目源碼不能夠展示,給個文檔轉換源碼:(轉換後其實就好做了,把上面那個代碼拷貝,路徑放進去就能夠實現預覽):
http://download.csdn.net/detail/tan313/9041835
參閱博客:http://blog.csdn.net/z69183787/article/details/17468039