導航:首頁 > 源碼編譯 > 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源碼相關的資料

熱點內容
資料庫查詢系統源碼 瀏覽:610
php5314 瀏覽:349
完美國際安裝到哪個文件夾 瀏覽:663
什麼app可以掃一掃做題 瀏覽:534
程序員編碼論壇 瀏覽:916
淘點是什麼app 瀏覽:651
中國高等植物pdf 瀏覽:447
51單片機時間 瀏覽:175
後台如何獲取伺服器ip 瀏覽:258
單片機流水燈程序c語言 瀏覽:227
程序員第二職業掙錢 瀏覽:231
運行里怎麼輸入伺服器路徑 瀏覽:833
pythonstepwise 瀏覽:499
劉一男詞彙速記指南pdf 瀏覽:54
php認證級別 瀏覽:360
方舟編譯啥時候推送 瀏覽:1001
php手機驗證碼生成 瀏覽:667
哲學思維pdf 瀏覽:7
凌達壓縮機有限公司招聘 瀏覽:526
weblogic命令部署 瀏覽:30