package com.haiyisoft.selearning.util; 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.IOException; 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; /** * * * * openoffice端口启动命令: * soffice -headless -accept="socket,port=8100;urp; * C:\Program Files (x86)\OpenOffice.org 3\program * @author muyunfei * * <p>Modification History:</p> * <p>Date Author Description</p> * <p>------------------------------------------------------------------</p> * <p>Mar 28, 2015 牟云飞 新建</p> */ public class DocConverter { private String SWFTools_Windows = "D:/SWFTools/pdf2swf.exe "; private String SWFTools_Linux = "D:/SWFTools/pdf2swf.exe "; 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 File odtFile; 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("/")); 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")) { 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: TODO * @param: @param docFile2 * @param: @param odtFile2 * @return: void * @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(); } //利用oppenoffice转换成pdf 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(); //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.println(loadStream(p.getInputStream())); System.out.println(loadStream(p.getErrorStream())); System.out.println(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("pdf2swf " + pdfFile.getPath() + " -o " + swfFile.getPath() + " -T 9"); loadStream(p.getInputStream()); 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已存在不需要转换****"); } } 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(); } 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"); } } } //测试 public static void main(String s[]) { DocConverter d = new DocConverter("E:/TDDOWNLOAD/test.doc"); d.conver(); } }
-----action
if(all_text.contains(last_name)){ //如果是文本课件 //ftpName实际是存放在ftp上的文件名 String fileItemName = (int)(Math.random()*100)+System.currentTimeMillis()+name2; //先传到本地,在本地转换成pdf,然后再转换成swf //获得本地路径 HttpServletRequest reqeuest = ServletActionContext.getRequest(); String rootPath=reqeuest.getRealPath("../uploadFileTemp/")+"\\"; //判断文件夹是否存在 File fileDri=new File(rootPath); if(!fileDri.exists()){ fileDri.mkdirs(); } //获取需要转换的文件名,将路径名中的'\'替换为'/' String converfilename = rootPath.replaceAll("\\\\", "/")+fileItemName; //System.out.println(converfilename); //1、先存到本地 File local_file=new File(converfilename); local_file.createNewFile();//创建一个新的文件 InputStream is = new FileInputStream(file); FileOutputStream out = new FileOutputStream(local_file); int size=-1; byte[] data=new byte[1024 * 1024];//设置缓冲字节 size=is.read(data);//输入到缓冲数据 while(-1!=size){ out.write(data, 0, size); size = is.read(data); } is.close(); out.flush(); out.close(); //调用转换类DocConverter,并将需要转换的文件传递给该类的构造方法 DocConverter d = new DocConverter(converfilename); //调用conver方法开始转换,先执行doc2pdf()将office文件转换为pdf;再执行pdf2swf()将pdf转换为swf; d.conver(); //调用getswfPath()方法,打印转换后的swf文件路径 System.out.println(d.getswfPath()); //生成swf相对路径,以便传递给flexpaper播放器 //String swfpath = "uploadFileTemp"+d.getswfPath().substring(d.getswfPath().lastIndexOf("/")); //2、将swf文件上传到ftp String ftpName =fileItemName.substring(0,fileItemName.lastIndexOf("."))+".swf"; File swfFile= new File(rootPath.replaceAll("\\\\", "/")+ ftpName); TelnetOutputStream telnetOut = ftpClient.put(ftpName); DataOutputStream dataOut = new DataOutputStream(telnetOut); FileInputStream ftpIs = new FileInputStream(swfFile); byte buffer[] = new byte[1024 * 1024]; int count = 0; while ((count = ftpIs.read(buffer)) != -1) { dataOut.write(buffer, 0, count); } ftpIs.close(); telnetOut.flush(); telnetOut.close(); dataOut.flush(); dataOut.close(); ftpClient.closeServer(); //3、保存名字 ftpFileNameList.add(ftpName); //4.删除缓存文件 swfFile.delete(); local_file.delete(); System.out.println(ftpName); }