---
title: "FlexPaper+SWFTools ，java实现在线文档浏览"
author: "Dxx23"
date: "2013-09-25T14:39:18Z"
tags: ["Flash", "FlexPaper", "Java", "Office", "swftools"]
categories: ["Web前端"]
slug: "flexpaper-swftools-java"
draft: false
lastmod: "2015-10-09T11:07:39Z"
id: 1947083
url: "https://dxx23.iteye.com/blog/1947083"
---

1、安装openoffice，OpenOffice\_3.4.0\_Win\_x86\_zh-CN.zip，使用新建OpenOfficeService.properties文件，放到src目录下 \[code="java"] OO\_HOME = C:\\\\Program Files (x86)\\\\OpenOffice.org 3 oo\_host = 127.0.0.1 oo\_port = 8100 \[/code] 2、\[url=http://download.csdn.net/detail/dxx23/6318867]下载 jodconverter-2.2.2\[/url]，将lib目录所有jar 复制到项目 lib目录 3、 \[url=http://download.csdn.net/detail/dxx23/6318785]下载swftools-0.9.2.exe \[/url]安装后，复制安装目录中到 pdf2swf.exe 到项目附件目录中。 4、\[url=http://code.google.com/p/flexpaper/downloads/list]下载Flexpaper\[/url]，引用其中的js 5、在环境变量中的Path配置如下：（注最后的“;”不要） C:\\Program Files (x86)\\OpenOffice.org 3\\program;D:\\IdeaProjects\\zsbk\\zsbk\\out\\artifacts\\zsbk\_war\_exploded\\JS\\StreetMap\\flexpaper \[code="java"] &lt;%@ page import="com.xxx.util.FlashUtil" %&gt; &lt;%-- Created by IntelliJ IDEA. Date: 13-7-11 Time: 上午8:34 To change this template use File | Settings | File Templates. --%&gt; &lt;%@ page contentType="text/html;charset=UTF-8" language="java" %&gt; &lt;% String applicationPath = application.getRealPath(""); // String filePath="http://x.x.x.x/JieJing/"; String fileName=request.getParameter("fileName"); String contextPath = request.getContextPath(); String flashFile = null; String filePath = null; if(fileName.indexOf("gisPhotos")!=-1){ fileName = fileName.substring(fileName.lastIndexOf("gisPhotos/"),fileName.length()); filePath =applicationPath + "\\\\JS\\\\StreetMap"; flashFile = new FlashUtil().beginConvert(filePath,fileName);//绝对路径 相对路径 flashFile = "\\\\JS\\\\StreetMap\\\\" +flashFile; flashFile = contextPath+flashFile.replace("\\\\","/"); }else{ filePath =applicationPath + "\\\\JS\\\\StreetMap\\\\flexpaper"; // fileName = new String(fileName.getBytes("ISO-8859-1"),"UTF-8"); flashFile = new FlashUtil().beginConvert(filePath,fileName);//绝对路径 相对路径 } %&gt; 在线文档浏览

[]()

\[/code] \[code="java"] package com.xxx.util; import java.io.BufferedInputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.net.ConnectException; 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; /\** * Created with IntelliJ IDEA. * Date: 13-7-11 * Time: 上午10:46 * To change this template use File | Settings | File Templates. \*/ public class FlashUtil { public static void main(String\[] args){ String outPath = new FlashUtil().beginConvert("","附件.pdf"); System.out.println("生成swf文件:" + outPath); // boolean outPath = new FlashUtil().isExistFlash("123.pdf"); // System.out.println("是否存在swf文件:" + outPath); } private static final String DOC = ".doc"; private static final String DOCX = ".docx"; private static final String XLS = ".xls"; private static final String XLSX = ".xlsx"; private static final String PDF = ".pdf"; private static final String SWF = ".swf"; private static final String TOOL = "pdf2swf.exe"; /\** * 入口方法-通过此方法转换文件至swf格式 * @param filePath 上传文件所在文件夹的绝对路径 * @param fileName 文件名称 * @return 生成swf文件名 \*/ public String beginConvert(String filePath,String fileName) { String outFile = ""; String fileNameOnly = ""; String fileExt = ""; if (null != fileName && fileName.indexOf(".") &gt; 0) { int index = fileName.indexOf("."); fileNameOnly = fileName.substring(0, index); fileExt = fileName.substring(index).toLowerCase(); } String inputFile = filePath + File.separator + fileName; String outputFile = ""; //如果是flash文件，直接显示 if(fileExt.equals(SWF)){ outFile = fileName; }else { //主要是针对中文汉字转拼音 fileNameOnly = new CnToSpell().getPinYin(fileNameOnly); //如果存在对应的flash文件 boolean isExistFlash = isExistFlash(filePath,fileNameOnly); if(isExistFlash){ outFile = fileNameOnly + SWF; }else { //如果是office文档，先转为pdf文件 if (fileExt.equals(DOC) || fileExt.equals(DOCX) || fileExt.equals(XLS) || fileExt.equals(XLSX)) { outputFile = filePath + File.separator + fileNameOnly + PDF; File pdfFile = new File(outputFile); if(!pdfFile.exists()){//判断pdf文件是否已经生成 office2PDF(inputFile, outputFile); } inputFile = outputFile; fileExt = PDF; } if (fileExt.equals(PDF)) { outputFile = filePath + File.separator + fileNameOnly + SWF; outputFile = outputFile.replace("\\\\","/"); File swfFile = new File(outputFile); if(!swfFile.exists()){//判断swf文件是否已经生成 File parentFolder = swfFile.getParentFile(); if(parentFolder!=null&&!parentFolder.exists()){ parentFolder.mkdirs(); } String toolFile = null; if(filePath.indexOf("flexpaper")==-1){ toolFile = filePath + File.separator +"flexpaper"+ File.separator + TOOL; }else{ toolFile = filePath + File.separator + TOOL; } convertPdf2Swf(inputFile, outputFile, toolFile); } outFile = fileNameOnly + SWF; } } } return outFile; } /\** * 将pdf文件转换成swf文件 * @param sourceFile pdf文件绝对路径 * @param outFile swf文件绝对路径 * @param toolFile 转换工具绝对路径 \*/ private void convertPdf2Swf(String sourceFile, String outFile, String toolFile) { String command = toolFile + " \\"" + sourceFile + "\\" -o \\"" + outFile + "\\" -s flashversion=9 "; try { Process process = Runtime.getRuntime().exec(command); System.out.println(loadStream(process.getInputStream())); System.err.println(loadStream(process.getErrorStream())); System.out.println(loadStream(process.getInputStream())); System.out.println("###--Msg: swf 转换成功"); } catch (Exception e) { e.printStackTrace(); } } /\** * 检测文件夹下是否已存在对应的flash文件 * @return \*/ private boolean isExistFlash(String filePath,String fileNameOnly){ String fileName = fileNameOnly.substring(fileNameOnly.lastIndexOf("/")+1); String newFilePath = fileNameOnly.substring(0 ,fileNameOnly.lastIndexOf("/")+1); File file = new File(filePath + File.separator+newFilePath); if(!file.exists()){//判断是否已经生成新文件夹，然后再去判断文件夹是否存在对应的flash文件 return false; } File\[] files = file.listFiles(); for(int j=0;j