java预览文件思路,java文件预览

我实现文件预览的方式是:

txt/doc/ppt/excel –>pdf –>swf

将txt/doc/ppt/excel 先转成pdf文件,再转成swf文件,最后展示在前台

需要的工具:

openoffice 、SWFTools、FlexPaper 、JODConverter

1.安装openoffice ,并通过dos窗口打开

openoffice 下载网址:http://www.openoffice.org/zh-cn/download/

下载安装后,通过dos找到安装路径,如:C:\Program Files (x86)\OpenOffice 4\program

执行dos命名:cd C:\Program Files (x86)\OpenOffice 4\program

继续输入 soffice -headless -accept=”socket,host=127.0.0.1,port=8100;urp;” -nofirststartwizard 即可打开

0818b9ca8b590ca3270a3433284dd417.png

由于文件预览只是项目的一个小功能,所以我们将FlexPaper的js文件和FlexPaperView.swf文件(该文件为文件预览的模板文件,必须要有)放在如下路径下:

0818b9ca8b590ca3270a3433284dd417.png

4.导入必须的JODConverter jar包

5.文件预览 fileManagerPreview.jsp

文档在线预览系统

6.文件转换工具类:FileOperatorUtil.java

// 文件类型转换 Txt/Word/Excel/PPT=>PDF

public static void docToPdf(File docFile, File pdfFile) throws Exception{

System.out.println(Config .getConfig("openOffice_connection_port"));

OpenOfficeConnection connection = new SocketOpenOfficeConnection(Integer.parseInt(Config .getConfig("openOffice_connection_port")));

try {

connection.connect();

DocumentConverter converter = new OpenOfficeDocumentConverter(connection);

converter.convert(docFile, pdfFile);

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;

}

}

@SuppressWarnings("all")

public static void pdf2swf(File pdfFile , File swfFile ) throws Exception {

Runtime r = Runtime.getRuntime();

if (!swfFile.exists()) {

if (pdfFile.exists()) {

if (environment == 1) {// windows环境处理

try {

Process p = r.exec(Config.getConfig("pdf_to_swf")+" "+ 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();

}

七。关键点提示:

到这一步 基本上就可以从界面上展现文件预览,可是部分朋友的界面展现只是一个空白的div。

一般的原因有以下几点:

swf路径错误;

FlexPaper控件中路径错误;

①swf路径错误的排除可以通过写死swfFile路径进行测试。

如:SWFFile : ‘http://localhost:8080/ipnet/doc/abcd.swf’

swffile文件必须是放在服务器上,硬盘上是预览不到的,因为flexpaper是需要与服务器通信来获得swf文件流。

②FlexPaper控件中路径错误

按照上文的目录结构,flexpaper的js文件放在WebRoot/plugins/flexpaper下,并且FlexPaperView.swf文件和flexpaper.js文件在同一目录下,

所以我们需要修改flexpaper.js的源码。

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png

修改的js的目的是为确定flexpaper.js的路径和 让flexpaper.js能找到FlexPaperViewer.swf文件。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值