import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStream;import java.io.InputStreamReader;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;public class DocConverter { PRivate String SWFTools_Windows = "C:/SWFTools/pdf2swf.exe "; private int environment;// 環境1:windows,2:linux(涉及pdf2swf路徑問題) public int getEnvironment() { return environment; } public void setEnvironment(int environment) { this.environment = environment; } private String fileName; private File pdfFile; private File swfFile; private File docFile; private File odtFile; public DocConverter(String fileString) { ini(fileString); } public DocConverter(String fileString, int environment) { this.environment = environment; ini(fileString); } /* * 初始化 @param fileString */ private void ini(String fileString) { try { fileName = fileString.substring(0, fileString.lastIndexOf("/")); docFile = new File(fileString); String s = fileString.substring(fileString.lastIndexOf("/") + 1, fileString.lastIndexOf(".")); fileName = fileName + "/" + s; // 用于處理TXT文檔轉化為PDF格式亂碼,獲取上傳文件的名稱(不需要后面的格式) String txtName = fileString.substring(fileString.lastIndexOf(".")); // 判斷上傳的文件是否是TXT文件 if (txtName.equalsIgnoreCase(".txt")) { // 定義相應的ODT格式文件名稱 odtFile = new File(fileName + ".odt"); // 將上傳的文檔重新copy一份,并且修改為ODT格式,然后有ODT格式轉化為PDF格式 this.copyFile(docFile, odtFile); pdfFile = new File(fileName + ".pdf"); // 用于處理PDF文檔 } else if (txtName.equals(".pdf") || txtName.equals(".PDF")) { pdfFile = new File(fileName + ".pdf"); // this.copyFile(docFile, pdfFile); } else { pdfFile = new File(fileName + ".pdf"); } swfFile = new File(fileName + ".swf"); } catch (Exception e) { e.printStackTrace(); } } /** * @Title: copyFile @Description: docFile2 @param: @param odtFile2 @return: * void @author: hl @time: 2014-4-17 下午9:41:52 @throws */ private void copyFile(File sourceFile, File targetFile) throws Exception { // 新建文件輸入流并對它進行緩沖 FileInputStream input = new FileInputStream(sourceFile); BufferedInputStream inBuff = new BufferedInputStream(input); // 新建文件輸出流并對它進行緩沖 FileOutputStream output = new FileOutputStream(targetFile); BufferedOutputStream outBuff = new BufferedOutputStream(output); // 緩沖數組 byte[] b = new byte[1024 * 5]; int len; while ((len = inBuff.read(b)) != -1) { outBuff.write(b, 0, len); } // 刷新此緩沖的輸出流 outBuff.flush(); // 關閉流 inBuff.close(); outBuff.close(); output.close(); input.close(); } /* * 轉為PDF @param file */ private void doc2pdf() throws Exception { if (docFile.exists()) { if (!pdfFile.exists()) { OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100); try { connection.connect(); DocumentConverter converter = new OpenOfficeDocumentConverter(connection); converter.convert(docFile, pdfFile); // close the connection connection.disconnect(); } catch (java.net.ConnectException e) { // ToDo Auto-generated catch block e.printStackTrace(); throw e; } catch (com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException e) { e.printStackTrace(); throw e; } catch (Exception e) { e.printStackTrace(); throw e; } } } } /* * 轉換成swf */ private void pdf2swf() throws Exception { if (!swfFile.exists()) { if (pdfFile.exists()) { Runtime r = Runtime.getRuntime(); if (environment == 1) {// windows環境處理 Process p = null; try { String[] cmd = new String[7]; cmd[0] = SWFTools_Windows; cmd[1] = "-i"; cmd[2] = pdfFile.getPath().trim(); cmd[3] = "-o"; cmd[4] = swfFile.getPath().trim(); cmd[5] = "-s"; cmd[6] = "languagedir=C://xpdf"; p = Runtime.getRuntime().exec(cmd); InputStream is2 = p.getErrorStream(); BufferedReader br2 = new BufferedReader(new InputStreamReader(is2)); while (br2.readLine() != null) ; p.waitFor(); p.exitValue(); // // 如果不讀取流則targetFile.exists() 文件不存在,但是程序沒有問題 if (pdfFile.exists()) { pdfFile.delete(); } } catch (Exception e) { e.printStackTrace(); throw e; } finally { if (p != null) { p.destroy(); } p = null; } } else if (environment == 2) {// linux環境處理 try { r.exec("pdf2swf " + pdfFile.getPath() + " -o " + swfFile.getPath() + " -T 9"); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(); } } } } } /* * 轉換主方法 */ public boolean conver() { if (swfFile.exists()) { return true; } try { doc2pdf(); pdf2swf(); } catch (Exception e) { return false; } if (swfFile.exists()) { return true; } else { return false; } } /* * 返回文件路徑 @param s */ public String getswfPath() { if (swfFile.exists()) { return swfFile.getPath(); } else { return ""; } } /* * 設置輸出路徑 */ public void setOutputPath(String outputPath) { if (!outputPath.equals("")) { String realName = fileName.substring(fileName.lastIndexOf("/"), fileName.lastIndexOf(".")); if (outputPath.charAt(outputPath.length()) == '/') { swfFile = new File(outputPath + realName + ".swf"); } else { swfFile = new File(outputPath + realName + ".swf"); } } }}中文可能會出現亂碼:
解決方案下載:
http://download.csdn.net/detail/anshichuxuezhe/9748124
放到C盤根目錄下
新聞熱點
疑難解答