package com.hl.zoneSystem_v01.utils; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; 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; /** * @ClassName: DocConverter * @Description: 文档转换 txt\office转pdf再转swf * @project: zoneSystem_v01 * @package: com.hl.zoneSystem_v01.utils * @author: hl * @version: V1.0 * @since: JDK 1.6.0_21 * @date: 2014-4-18 下午12:33:05 */ public class DocConverter { //SWFTools的安装路径 linux与windows各不同 private String SWFTools_Windows = "F:/sortware/testingsoftware/SWFTools/pdf2swf.exe "; private String SWFTools_Linux = "pdf2swf "; // 环境1:windows,2:linux(涉及pdf2swf路径问题) 调用工具类处理,根据回车符判断系统平台 private static final int environment = CommUtils.getOsType(); private String fileString; private String outputPath = "";// 输入路径,如果不设置就输出在默认位置 private String fileName; private File pdfFile; private File swfFile; private File docFile; private File odtFile; private String txtName; public String swfFileName;//最后生成的swf文件 短文件名,不包括路径。 public DocConverter(String fileString) { ini(fileString); } /* * 重新设置 file @param fileString */ public void setFile(String fileString) { ini(fileString); } /* * 初始化 @param fileString */ private void ini(String fileString) { try { //生成绝对路径全名,包括路径+文件名 this.fileString = fileString; fileName = fileString.substring(0, fileString.lastIndexOf(File.separator)); docFile = new File(fileString); String s = fileString.substring(fileString.lastIndexOf(File.separator) + 1,fileString.lastIndexOf(".")); fileName = fileName + File.separator + s; // 用于处理TXT文档转化为PDF格式乱码,获取上传文件的名称(不需要后面的格式) 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文档 //方法二: //将txt重新复制一份,转成utf-8在转成pdf就不会乱码了 odtFile = new File((fileName+1) + ".txt"); this.copyFile2(docFile, odtFile); pdfFile = new File(fileName + ".pdf"); // 用于处理PDF文档 } else if (txtName.equals(".pdf") || txtName.equals(".PDF")) { pdfFile = new File(fileName+1 + ".pdf"); this.copyFile(docFile, pdfFile); } else { pdfFile = new File(fileName + ".pdf"); } swfFile = new File(fileName + ".swf"); //获取swf文件,短文件名,不包含路径 swfFileName = s + ".swf"; } catch (Exception e) { e.printStackTrace(); } } /** * @Title: copyFile * @Description: TODO * @param: @param 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(); } /* * 读取txt的方法 设置编码 txt转pdf有乱码情况(txt本身编码不是utf-8,时,如果本身为utf-8则不会乱码), * 本例中采用先复制另一份txt文件出来,然后将其转化为utf-8格式,然后在转换为pdf就不会乱码了。 */ @SuppressWarnings("unused") private void copyFile2(File sourceFile,File targetFile)throws Exception{ //获取txt原始编码 String charSet = new EncodingDetect().getJavaEncode(sourceFile.getPath()); //新建文件输入流并对它进行缓冲 FileInputStream input = new FileInputStream(sourceFile); InputStreamReader input2 = new InputStreamReader(input,charSet); BufferedReader bfr = new BufferedReader(input2); // 新建文件输出流并对它进行缓冲 FileOutputStream output = new FileOutputStream(targetFile); OutputStreamWriter out = new OutputStreamWriter(output,CommUtils.charSet); BufferedWriter bfw = new BufferedWriter(out); // 缓冲 一行一行的读 String line; //获取系统平台换行符 String lineSeparator = System.getProperty("line.separator"); while((line = bfr.readLine()) != null){ bfw.write(line + lineSeparator); } //刷新 bfw.flush(); //关流 input.close(); input2.close(); bfr.close(); output.close(); out.close(); bfw.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); //判断上传文件为pdf的情况,则无需在转换 if (txtName.equals(".pdf") || txtName.equals(".PDF")) { }else if(txtName.equals(".txt") || txtName.equals(".TXT")){ converter.convert(odtFile, pdfFile); // 关闭文件链接 connection.disconnect(); //删除odt文件 if(odtFile.exists()){ odtFile.delete(); } }else{//office文件处理 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 { // 这里根据SWFTools安装路径需要进行相应更改 Process p = r.exec(SWFTools_Windows + pdfFile.getPath() + " -o " + swfFile.getPath() + " -T 9"); System.out.print(loadStream(p.getInputStream())); System.err.print(loadStream(p.getErrorStream())); System.out.print(loadStream(p.getInputStream())); System.err.println("****swf转换成功,文件输出:" + swfFile.getPath() + "****"); if (pdfFile.exists()) { pdfFile.delete(); } } catch (Exception e) { e.printStackTrace(); throw e; } } else if (environment == 2){// linux环境处理 try { Process p = r.exec(SWFTools_Linux + 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 new RuntimeException(); } } } else { System.out.println("****pdf不存在,无法转换****"); } } else { System.out.println("****swf已存在不需要转换****"); } if (pdfFile.exists()) { pdfFile.delete(); } } static String loadStream(InputStream in) throws IOException { int ptr = 0; //把InputStream字节流 替换为BufferedReader字符流 2013-07-17修改 BufferedReader reader = new BufferedReader(new InputStreamReader(in)); StringBuilder buffer = new StringBuilder(); while ((ptr = reader.read()) != -1) { buffer.append((char) ptr); } return buffer.toString(); } /* * 转换主方法 */ 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(); if (pdfFile.exists()) { pdfFile.delete(); } } 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() { if (swfFile.exists()) { String tempString = swfFile.getPath(); tempString = tempString.replaceAll("\\\\", "/"); return tempString; } else { return ""; } } /* * 设置输出路径 */ public void setOutputPath(String outputPath) { this.outputPath = 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"); } } } } |
|