導航:首頁 > 源碼編譯 > jodconverter源碼

jodconverter源碼

發布時間:2025-01-30 08:16:44

java使用jodconverter包實現office轉pdf的時候,報錯文件格式錯誤是為什麼啊

1 有幾種原因可以參考一下:
1)從資料庫讀取出來的內容 與 寫死的文件名是否 物理一致,即是否是其他編碼。
2)注意從資料庫讀取出來的字元串是否在前後有多餘的空格,即驗證其長度
3)查找物理位置上的文件是否真的存在。
4) 是否能夠連接到soffice上,soffice是否啟動了多次。
5) 我曾經遇到的問題是:在應用程序中寫死的和從資料庫讀取的一樣,但在系統磁碟上所保存的就不是這個文件名,而是某種編碼的字元。
正確代碼如下;
* 將Office文檔轉換為PDF. 運行該函數需要用到OpenOffice, OpenOffice下載地址為

* http://www.openoffice.org/
*
* <pre>
* 方法示例:
* String sourcePath = "F:\\office\\source.doc";
* String destFile = "F:\\pdf\\dest.pdf";
* Converter.office2PDF(sourcePath, destFile);
* </pre>
*
* @param sourceFile
* 源文件, 絕對路徑. 可以是Office2003-2007全部格式的文檔, Office2010的沒測試. 包括.doc,
* .docx, .xls, .xlsx, .ppt, .pptx等. 示例: F:\\office\\source.doc
* @param destFile
* 目標文件. 絕對路徑. 示例: F:\\pdf\\dest.pdf
* @return 操作成功與否的提示信息. 如果返回 -1, 表示找不到源文件, 或url.properties配置錯誤; 如果返回 0,
* 則表示操作成功; 返回1, 則表示轉換失敗
*/
public static int office2PDF(String sourceFile, String destFile) {
try {
File inputFile = new File(sourceFile);
if (!inputFile.exists()) {
return -1;// 找不到源文件, 則返回-1
}

// 如果目標路徑不存在, 則新建該路徑
File outputFile = new File(destFile);
if (!outputFile.getParentFile().exists()) {
outputFile.getParentFile().mkdirs();
}

String OpenOffice_HOME = "D:\\Program Files\\OpenOffice.org 3";//這里是OpenOffice的安裝目錄, 在我的項目中,為了便於拓展介面,沒有直接寫成這個樣子,但是這樣是絕對沒問題的
// 如果從文件中讀取的URL地址最後一個字元不是 '\',則添加'\'
if (OpenOffice_HOME.charAt(OpenOffice_HOME.length() - 1) != '\\') {
OpenOffice_HOME += "\\";
}
// 啟動OpenOffice的服務
String command = OpenOffice_HOME
+ "program\\soffice.exe -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\"";
Process pro = Runtime.getRuntime().exec(command);
// connect to an OpenOffice.org instance running on port 8100
OpenOfficeConnection connection = new SocketOpenOfficeConnection(
"127.0.0.1", 8100);
connection.connect();

// convert
DocumentConverter converter = new OpenOfficeDocumentConverter(
connection);
converter.convert(inputFile, outputFile);

// close the connection
connection.disconnect();
// 關閉OpenOffice服務的進程
pro.destroy();

return 0;
} catch (FileNotFoundException e) {
e.printStackTrace();
return -1;
} catch (ConnectException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

return 1;
}

閱讀全文

與jodconverter源碼相關的資料

熱點內容
手機硬體編程 瀏覽:832
如何去除你看文件夾時間 瀏覽:438
兩個加數的和的編程 瀏覽:793
51單片機lcd顯示 瀏覽:583
hacmp命令 瀏覽:619
安卓游戲機都有什麼 瀏覽:73
androidapp被殺死 瀏覽:393
msp430單片機測量溫度 瀏覽:428
用友的雲加密是一直都不用換嗎 瀏覽:360
美的空調壓縮機壞了怎麼辦 瀏覽:803
usb單片機驅動 瀏覽:129
模板模式在源碼中的體 瀏覽:938
linuxsd卡分區 瀏覽:632
網頁查郵箱伺服器地址 瀏覽:226
程序員重構水管圖 瀏覽:43
電子加密貨幣有漏洞發行後能改嗎 瀏覽:797
java一段時間執行 瀏覽:513
pdftojpg綠色版 瀏覽:680
linux收音機 瀏覽:699
程序員眼中的井川里予 瀏覽:517