标签:blog http io ar os sp java for 文件
package com.text.newwmb.utils; import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; 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 static final int environment = 1;// 环境1:windows,2:linux(涉及pdf2swf路径问题) private String fileString; private String outputPath = "";// 输入路径,如果不设置就输出在默认位置 private String fileName; private File pdfFile; private File swfFile; private File docFile; private String swfpath=""; public static final String SwfToolsPath = new ReadProperties("config/fileserver.properties").getValue("SwfToolsPath")+" "; public static final String OpenOfficePort = new ReadProperties("config/fileserver.properties").getValue("OpenOfficePort"); public DocConverter(String fileString) { ini(fileString); } /* * 重新设置 file @param fileString */ public void setFile(String fileString) { ini(fileString); } /* * 初始化 @param fileString */ private void ini(String fileString) { this.fileString = fileString; fileName = new String(fileString.substring(0, fileString.lastIndexOf("."))); docFile = new File(fileString); pdfFile = new File(fileName + ".pdf"); swfFile = new File(fileName + ".swf"); } /* * 转为PDF @param file */ private void doc2pdf() throws Exception { if (docFile.exists()) { if (!pdfFile.exists()) { OpenOfficeConnection connection = new SocketOpenOfficeConnection(Integer.parseInt(OpenOfficePort)); try { connection.connect(); DocumentConverter converter = new OpenOfficeDocumentConverter(connection); converter.convert(docFile, pdfFile); // close the connection connection.disconnect(); System.out.println("****pdf转换成功,PDF输出:" + pdfFile.getPath() + "****"); } catch (java.net.ConnectException e) { // ToDo Auto-generated catch block e.printStackTrace(); System.out.println("****swf转换异常,openoffice服务未启动!****"); throw e; } catch (com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException e) { e.printStackTrace(); System.out.println("****swf转换器异常,读取转换文件失败****"); throw e; } catch (Exception e) { e.printStackTrace(); throw e; } } else { System.out.println("****已经转换为pdf,不需要再进行转化****"); } } else { System.out.println("****swf转换器异常,需要转换的文档不存在,无法转换****"); } } /* * 转换成swf */ @SuppressWarnings("unused") private void pdf2swf() throws Exception { Runtime r = Runtime.getRuntime(); if (!swfFile.exists()) { if (pdfFile.exists()) { if (environment == 1)// windows环境处理 { try { List<String> command = new ArrayList<String>(); System.out.println("---1---"); command.add(SwfToolsPath);//从配置文件里读取 command.add("-z"); command.add("-s"); command.add("flashversion=9"); command.add("-s"); command.add("poly2bitmap");//加入poly2bitmap的目的是为了防止出现大文件或图形过多的文件转换时的出错,没有生成swf文件的异常 command.add(pdfFile.getPath()); command.add("-o"); command.add(swfFile.getPath()); // 这里根据SWFTools安装路径需要进行相应更改 //Process p = r.exec(SwfToolsPath + pdfFile.getPath() + " -o " + swfFile.getPath() + " -T 9 -s bitmap"); System.out.println("---2---"); ProcessBuilder pb = new ProcessBuilder(); pb.command(command); Process process = pb.start(); //dealWith(process); //process.waitFor();//等待子进程的结束,子进程就是系统调用文件转换这个新进程 InputStreamWathThread inputWathThread = new InputStreamWathThread(process); inputWathThread.start(); ErrorInputStreamWathThread errorInputWathThread = new ErrorInputStreamWathThread(process); errorInputWathThread.start(); try { process.waitFor();//等待子进程的结束,子进程就是系统调用文件转换这个新进程 inputWathThread.setOver(true);//转换完,停止流的处理 errorInputWathThread.setOver(true); } catch (InterruptedException e) { e.printStackTrace(); } //loadStream(p.getInputStream()); /*System.out.print(loadStream(p.getInputStream())); System.err.print(loadStream(p.getErrorStream())); System.out.print(loadStream(p.getInputStream()));*/ System.err.println("****swf转换成功,文件输出:" + swfFile.getPath() + "****"); swfpath = swfFile.getPath(); if (pdfFile.exists()) { pdfFile.delete(); } } catch (Exception e) { e.printStackTrace(); throw e; } } else if (environment == 2)// linux环境处理 { try { Process p = r.exec("pdf2swf " + pdfFile.getPath() + " -o " + swfFile.getPath() + " -T 9"); //System.out.print(loadStream(p.getInputStream())); //System.err.print(loadStream(p.getErrorStream())); System.err.println("****swf转换成功,文件输出:" + swfFile.getPath() + "****"); if (pdfFile.exists()) { pdfFile.delete(); } } catch (Exception e) { e.printStackTrace(); throw e; } } } else { System.out.println("****pdf不存在,无法转换****"); } } else { System.out.println("****swf已存在不需要转换****"); } } static String loadStream(InputStream in) throws IOException { int ptr = 0; BufferedReader reader = new BufferedReader(new InputStreamReader(in)); StringBuilder buffer = new StringBuilder(); while ((ptr = reader.read()) != -1) { buffer.append((char) ptr); } return buffer.toString(); } /* * 转换主方法 */ @SuppressWarnings("unused") public boolean conver() { if (swfFile.exists()) { System.out.println("****swf转换器开始工作,该文件已经转换为swf****"); return true; } if (environment == 1) { System.out.println("****swf转换器开始工作,当前设置运行环境windows****"); } else { System.out.println("****swf转换器开始工作,当前设置运行环境linux****"); } try { doc2pdf(); pdf2swf(); } catch (Exception e) { // TODO: Auto-generated catch block e.printStackTrace(); return false; } if (swfFile.exists()) { return true; } else { return false; } } /* * 返回文件路径 @param s */ public String getswfPath() { String tempString = swfpath; tempString = tempString.replaceAll("\\\\", "/"); return tempString; } /* * 设置输出路径 */ public void setOutputPath(String outputPath) { this.outputPath = outputPath; if (!outputPath.equals("")) { String realName = new String(fileName.substring(fileName.lastIndexOf("/")+1, fileName.length())); swfFile = new File(outputPath +"/"+ realName + ".swf"); // if (outputPath.charAt(outputPath.length()) == ‘/‘) { // swfFile = new File(outputPath + realName + ".swf"); // } else { // swfFile = new File(outputPath + realName + ".swf"); // } } } private void dealWith(final Process pro){ // 下面是处理堵塞的情况 try { new Thread(){ public void run(){ BufferedReader br1 = new BufferedReader(new InputStreamReader(pro.getInputStream())); String text; try { while ( (text = br1.readLine()) != null) { System.out.println(text); } } catch (IOException e) { e.printStackTrace(); } } }.start(); } catch (Exception e) { e.printStackTrace(); } try { new Thread(){ public void run(){ BufferedReader br2 = new BufferedReader(new InputStreamReader(pro.getErrorStream()));//这定不要忘记处理出理时产生的信息,不然会堵塞不前的 String text; try { while( (text = br2.readLine()) != null){ System.err.println(text); } } catch (IOException e) { e.printStackTrace(); } } }.start(); } catch (Exception e) { e.printStackTrace(); } } public static void main(String s[]) { DocConverter d = new DocConverter("D:/file/3/test.pdf"); d.conver(); String swfpath = d.getswfPath(); System.out.println("swfpath :" + swfpath); // swf存放地址项目/upload/swf/xxx.swf } }
package com.text.newwmb.utils; import java.io.BufferedReader; import java.io.InputStreamReader; //这个类主要用来处理一个系统调用而新创建一个线程或进程执行期间所产生的出错流的处理 public class ErrorInputStreamWathThread extends Thread { private Process process = null; private boolean over = false; public ErrorInputStreamWathThread(Process p) { process = p; over = false; } public void run() { try { if (process == null) { System.out.println("No__process"); return; } //对出错流的处理 BufferedReader br = new BufferedReader(new InputStreamReader(process.getErrorStream())); while (true) { if (process == null || over) { System.out.println("done"); break; } String temp; while ((temp = br.readLine()) != null) { // logger.info("出错流信息:" + temp); ; } } } catch (Exception e) { e.printStackTrace(); System.out.println(e.getMessage()); } } public void setOver(boolean over) { this.over = over; } }
package com.text.newwmb.utils; import java.io.BufferedReader; import java.io.InputStreamReader; public class InputStreamWathThread extends Thread { private Process process = null; private boolean over = false; public InputStreamWathThread(Process p) { process = p; over = false; } public void run() { try { if (process == null) { System.out.println("process为null,无法处理文件转换"); return; } //对输入流,可能是一个回车之类的输入 BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream())); while (true) { if (process == null || over) { System.out.println("done"); break; } String temp; while ((temp = br.readLine()) != null) { // logger.info("输入流信息:" + temp);//如这些信息:NOTICE processing PDF page 10 (595x842:0:0) (move:0:0)等等的打印时提示信息 ; } } } catch (Exception e) { e.printStackTrace(); System.out.println(e.getMessage()); } } public void setOver(boolean over) { this.over = over; } }
参考地址:http://nopainnogain.iteye.com/blog/825176
标签:blog http io ar os sp java for 文件
原文地址:http://my.oschina.net/u/1266623/blog/350801