java 调用 swf 文件上传_java SpringMvc 实现文件在线预览(openoffice+swftools+flexpaper)

项目需求:服务器接受的文件当下只能下载之后才能浏览内容,现需要后台能在线浏览到文件内容,避免繁琐无用文件下载操作.

通过几天网上资料搜索,目前免费的在线预览开发技术使用最多还是(openoffice+swftools+flexpaper),经过2天的学习,终于在本地测试成功,后在服务器搭建环境也成功上线.

1.概述

主要原理:

1.通过第三方工具openoffice,将word、excel、ppt、txt等文件转换为pdf文件

2.通过swfTools将pdf文件转换成swf格式的文件

3.通过FlexPaper文档组件在页面上进行展示

2.安装包下载

1.openoffice是Apache下的一个开放免费的文字处理软件

下载地址:Apache oppenoffice 官网下载 版本-3.4.1

2.SWFTools是一组用来处理Flash的swf文件的工具包,我们使用它将pdf文件转成swf文件!

下载地址:SWFTools官网下载 swftools-2013-04-09-1007.exe

3.FlexPaper是一个开源轻量级的在浏览器上显示各种文档的组件

下载地址:FlexPaper官网下载 版本1.4.0(可选择新版)

4.JODConverter一个Java的OpenDocument 文件转换器,在此我们只用到它的jar包

下载地址:JODCConverter下载

openoffice与SWFTools下载完直接安装就好了,openoffice需要开启相应服务,进入安装目录,进入program文件,在此处打开命令行,或者CMD选择cd到这个文件,然后输入命令soffice -headless -accept=”socket,host=127.0.0.1,port=8100;urp;” -nofirststartwizard

4、引入项目

e623a61e3ce95df03b0e59d3129ab320.png

主要用到的就是其中flexpaper_flash.js和FlexPaperViewer.swf。html页面是Demo格式,可以根据格式自定义自己的显示页面,两个swf文件是测试文件,你直接访问FlexPaperViewer.html指向的是Paper.swf.如果显示成功表示你环境搭建好了.

另外我的是maven项目,jar包是统一管理的.其他项目可以直接把下载的JODConverter文件里面的lib包导入项目中即可.

所有的相关jar包依赖如下:

com.artofsolving

jodconverter

2.2.2

com.artofsolving

jodconverter-cli

2.2.2

commons-cli

commons-cli

1.2

commons-io

commons-io

2.3

org.openoffice

juh

3.0.1

org.openoffice

jurt

3.0.1

org.openoffice

ridl

3.0.1

org.slf4j

slf4j-jdk14

1.5.6

org.openoffice

unoil

3.0.1

com.thoughtworks.xstream

xstream

1.3.1

下一步我们把相应的工具类引入项目:创建DocConverter.class作为我们将文件转换成PDF,然后把PDF转换成SWF格式文件,此工具类是直接将两个步骤一起执行的.代码:` import java.io.BufferedInputStream;

import java.io.File;

import java.io.IOException;

import java.io.InputStream;

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;

/**

* doc docx格式转换

*/

public class DocConverter {

private static final int environment = 1;// 环境 1:windows 2:linux

private String fileString;// (只涉及pdf2swf路径问题)

private String outputPath = “”;// 输入路径 ,如果不设置就输出在默认的位置

private String fileName;

private File pdfFile;

private File swfFile;

private File docFile;

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 = fileString.substring(0, fileString.lastIndexOf("."));

docFile = new File(fileString);

pdfFile = new File(fileName + ".pdf");

swfFile = new File(fileName + ".swf");

//我的路径是从这里指定的,因为此处截取的是全路径+文件名字,去掉的是文件后缀,如果想指定转换到指定指定位置可以如下操作:

// fileName=fileString.substring(fileString.lastIndexOf("/"),fileString.lastIndexOf("."));

// String path=”全路径”;

// docFile=new File(fileString);

// pdfFile=new File(path+fileName+”.pdf”);

// swfFile=new File(path+fileName+”.swf”);

}

/**

* 转为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();

System.out.println("****pdf转换成功,PDF输出:" + pdfFile.getPath()+ "****");

} catch (java.net.ConnectException e) {

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 {

//此处要指定你机器安装的pdf2swf.exe全路径

Process p = r.exec("D:/Program Files/SWFTools/pdf2swf.exe "+ 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 (IOException 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;

in = new BufferedInputStream(in);

StringBuffer buffer = new StringBuffer();

while ((ptr = in.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) {

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");

}

}

}

}

OK!接下来是显示JSP页面previewFile.jsp

pageEncoding="UTF-8"%>

body { margin:0; padding:0; overflow:auto; }

#flashContent { display:none; }

var swfVersionStr = "10.0.0";

var xiSwfUrlStr = "playerProductInstall.swf";

var flashvars = {

//escape中指定的是你swf文件的路径

SwfFile : escape("${pageContext.request.contextPath}/preview${url}"),

//SwfFile : escape("${url}"),

Scale : 0.6,

ZoomTransition : "easeOut",

ZoomTime : 0.5,

ZoomInterval : 0.1,

FitPageOnLoad : false,

FitWidthOnLoad : true,

PrintEnabled : true,

FullScreenAsMaxWindow : false,

ProgressiveLoading : true,

PrintToolsVisible : false,

ViewModeToolsVisible : true,

ZoomToolsVisible : true,

FullScreenVisible : true,

NavToolsVisible : true,

CursorToolsVisible : true,

SearchToolsVisible : true,

localeChain: "en_US"

};

var params = {

}

params.quality = "high";

params.bgcolor = "#ffffff";

params.allowscriptaccess = "sameDomain";

params.allowfullscreen = "true";

var attributes = {};

attributes.id = "FlexPaperViewer";

attributes.name = "FlexPaperViewer";

//此处一定要指定你项目下的FlexPaperViewer.swf路径

swfobject.embedSWF( "${pageContext.request.contextPath}/FlexPaper_1.4.0_flash/FlexPaperViewer.swf", "flashContent",

"1000", "600",//指定高和宽

swfVersionStr, xiSwfUrlStr,

flashvars, params, attributes);

swfobject.createCSS("#flashContent", "display:block;text-align:left;");

To view this page ensure that Adobe Flash Player version

10.0.0 or greater is installed.

到这里,基本配置就完成了,紧接着就是调用了`@RequestMapping(“/center/preview.do”)

public String toView(Model model,String fileName,String Url,HttpServletRequest request)

//fileName = “F:/2.docx”;

String ContextPath = Url+fileName;//文件路径+文件名

DocConverter converter = new DocConverter(ContextPath);

converter.conver();

String getswfPath = converter.getswfPath();

System.out.println(getswfPath);

model.addAttribute(“url”, getswfPath);

return “previewFile”;

}

以上在windows测试没有问题.

过程中我遇到过两种问题情况.

第一种就是路径问题,各种路径需要梳理清楚,研究一下整个过程,差不多能解决路径输入与输出问题.整个工作流程无非就是,指定一个文件的路径作为参数给转换器,转换器调用pdfconver()方法把文件转换成pdf格式,然后调用swfconver()方法将swf格式,里面夹杂一些判断,转换成swf格式之后,将swf文件路径在显示页面指定.

第二种是生成的swf文件名称夹带中文,建议用英文或者数字,至于文件名为中文的swf文件,我最后也没测试成功.干脆我生成swf的时候全生成英文和数字名称.

最后本地windows实现后,在linux安装openoffice与swftools,官网有相应的安装包,和教程,根据教程一步步安装就可以了,记住把pdf2swf命令设置为全局,另外把openoffice服务打开.把项目中的工具环境默认改为2,注意一下路径.

遇到问题一定要耐心!!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值