转自:
http://xuliduo.javaeye.com/blog/671889
相关介绍:
http://www.chinaret.com/user/topic_view.aspx?b=1&id=71971467-c260-4b73-8281-64ec243380a9
现在项目有个需求,要求把ms office和txt在线播放,偶当时就想到了
布丁网
然后就问……google……
结果google告诉偶,简单的办法在windows上是可以实现的,非常简单...不过要用到jacob,具体大家去问Google。
由于要调用dll,在linux上偶不清楚能不能用(公司搞服务器的人说不可以),结果就被pass了……
再然后……只有这么办了……
先用
openOffice把ppt、word、excel、txt转换成pdf,然后用
swftools转换成swf,然后在线播放。
具体说明如下(windows,在linux下偶正准备测试)
1、安装相关软件,这个偶就不说了……
2、下载
jodconverter,偶用的2.2.2...然后怎么把lib放在环境变量或者项目环境偶就不说了……
3、以cmd方式启动openoffice server
Java代码
cd opeonofiice的安装路径/program
cd opeonofiice的安装路径/program
Java代码
soffice -headless -accept=
"socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard
soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard
4、看看8100被监听没
Java代码
netstat -an
netstat -an
5、写代码了……偶没有把代码集成到项目中,所以是单独的例子,不过这样还好点,不然和项目偶和太厉害了
JOD4DocToPDF
Java代码
package com.born.sys.util.pdf;
import java.io.File;
import java.net.ConnectException;
import java.util.Date;
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;
public class JOD4DocToPDF
extends java.lang.Thread {
private File inputFile;
private File outputFile;
public JOD4DocToPDF(File inputFile, File outputFile) {
this.inputFile = inputFile;
this.outputFile = outputFile; }
public void docToPdf() { Date start =
new Date(); OpenOfficeConnection connection =
new SocketOpenOfficeConnection(
8100);
try { connection.connect(); DocumentConverter converter =
new OpenOfficeDocumentConverter( connection); converter.convert(inputFile, outputFile); }
catch (ConnectException cex) { cex.printStackTrace(); }
finally {
if (connection !=
null) { connection.disconnect(); connection =
null; } }
long l = (start.getTime() -
new Date().getTime());
long day = l / (
24 *
60 *
60 *
1000);
long hour = (l / (
60 *
60 *
1000) - day *
24);
long min = ((l / (
60 *
1000)) - day *
24 *
60 - hour *
60);
long s = (l /
1000 - day *
24 *
60 *
60 - hour *
60 *
60 - min *
60); System.out.println(
"生成" + outputFile.getName() +
"耗费:" + min +
"分" + s +
"秒"); }
public void run() {
this.docToPdf(); }
public File getInputFile() {
return inputFile; }
public void setInputFile(File inputFile) {
this.inputFile = inputFile; }
public File getOutputFile() {
return outputFile; }
public void setOutputFile(File outputFile) {
this.outputFile = outputFile; }
public static void main(String[] args) { JOD4DocToPDF tools =
new JOD4DocToPDF(
new File(
"d:/中文的ppt哦.ppt"),
new File(
"d:/被转换的pdf.pdf")); tools.start(); } }
/**
*
*/
package com.born.sys.util.pdf;
import java.io.File;
import java.net.ConnectException;
import java.util.Date;
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;
/**
* <ul>
* <li>文件名称: com.born.sys.util.pdf.JOD4DocToPDF.java</li>
* <li>文件描述:</li>
* <li>版权所有: 版权所有(C)2001-2006</li>
* <li>公 司: born</li>
* <li>内容摘要:</li>
* <li>其他说明:</li>
* <li>完成日期:2010-5-21</li>
* <li>修改记录0:无</li>
* </ul>
*
* @version 1.0
* @author 许力多
*/
public class JOD4DocToPDF extends java.lang.Thread {
private File inputFile;// 需要转换的文件
private File outputFile;// 输出的文件
public JOD4DocToPDF(File inputFile, File outputFile) {
this.inputFile = inputFile;
this.outputFile = outputFile;
}
public void docToPdf() {
Date start = new Date();
// connect to an OpenOffice.org instance running on port 8100
OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
try {
connection.connect();
// convert
DocumentConverter converter = new OpenOfficeDocumentConverter(
connection);
converter.convert(inputFile, outputFile);
} catch (ConnectException cex) {
cex.printStackTrace();
} finally {
// close the connection
if (connection != null) {
connection.disconnect();
connection = null;
}
}
long l = (start.getTime() - new Date().getTime());
long day = l / (24 * 60 * 60 * 1000);
long hour = (l / (60 * 60 * 1000) - day * 24);
long min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);
long s = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
System.out.println("生成" + outputFile.getName() + "耗费:" + min + "分" + s
+ "秒");
}
/**
* 由于服务是线程不安全的,所以……需要启动线程
*/
public void run() {
this.docToPdf();
}
public File getInputFile() {
return inputFile;
}
public void setInputFile(File inputFile) {
this.inputFile = inputFile;
}
public File getOutputFile() {
return outputFile;
}
public void setOutputFile(File outputFile) {
this.outputFile = outputFile;
}
/**
* @param args
*/
public static void main(String[] args) {
JOD4DocToPDF tools = new JOD4DocToPDF(new File("d:/中文的ppt哦.ppt"),
new File("d:/被转换的pdf.pdf"));
tools.start();
}
}
ps:其实还有很多属性的,不过偶图简单,直接用的官方例子,人懒无敌……阿门
通过Runtime来调用cmd,然后生成相关的播放文件,生成好的flash效果见
点击看效果
PdfToSwf
Java代码
package com.born.sys.util.pdf;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
public class PdfToSwf {
public int convertPDF2SWF(String sourcePath, String destPath, String fileName)
throws IOException { File dest =
new File(destPath);
if (!dest.exists()) { dest.mkdirs(); } File source =
new File(sourcePath);
if (!source.exists()) {
return 0; } String[] envp =
new String[
1]; envp[
0] =
"PATH=D://tools//SWFTools//"; String command =
"pdf2swf -z -s flashversion=9 /"" + sourcePath +
"/" -o /"" + destPath + fileName +
"/""; Process pro = Runtime.getRuntime().exec(command, envp); BufferedReader bufferedReader =
new BufferedReader(
new InputStreamReader(pro.getInputStream()));
while (bufferedReader.readLine() !=
null) { String text = bufferedReader.readLine(); System.out.println(text); }
try { pro.waitFor(); }
catch (InterruptedException e) { e.printStackTrace(); } command =
"swfcombine -z -X 720 -Y 540 /"D:/tools/SWFTools/swfs/rfxview.swf/" viewport=/"" + destPath + fileName +
"/" -o /"" + destPath + fileName +
"/""; pro = Runtime.getRuntime().exec(command, envp); System.out.println(command); bufferedReader =
new BufferedReader(
new InputStreamReader(pro .getInputStream()));
while (bufferedReader.readLine() !=
null) { String text = bufferedReader.readLine(); System.out.println(text); }
try { pro.waitFor(); }
catch (InterruptedException e) { e.printStackTrace(); }
return pro.exitValue(); }
public static void main(String[] args) { String sourcePath =
"d:/PersonalManagementSystem.pdf"; String destPath =
"d:/"; String fileName =
"PersonalManagementSystem.swf";
try { System.out.println(
new PdfToSwf().convertPDF2SWF(sourcePath, destPath, fileName)); }
catch (IOException e) { e.printStackTrace(); } } }
package com.born.sys.util.pdf;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* <ul>
* <li>文件名称: com.born.sys.util.pdf.PdfToSwf.java</li>
* <li>文件描述: pdf生成swf</li>
* <li>版权所有: 版权所有(C)2001-2006</li>
* <li>公 司: born</li>
* <li>内容摘要:</li>
* <li>其他说明:</li>
* <li>完成日期:2010-5-21</li>
* <li>修改记录0:无</li>
* </ul>
*
* @version 1.0
* @author 许力多
*/
public class PdfToSwf {
public int convertPDF2SWF(String sourcePath, String destPath,
String fileName) throws IOException {
// 目标路径不存在则建立目标路径
File dest = new File(destPath);
if (!dest.exists()) {
dest.mkdirs();
}
// 源文件不存在则返回
File source = new File(sourcePath);
if (!source.exists()) {
return 0;
}
// 调用pdf2swf命令进行转换
// D:/tools/SWFTools>pdf2swf.exe -z -B rfxview.swf -s flashversion=9
// d:/人员管理系
// 统PersonalManagementSystem简介.pdf -o d:/test.swf
// 要把D://tools//SWFTools//放在path里面……不然使用不了播放器
// 先生成flash
String[] envp = new String[1];
envp[0] = "PATH=D://tools//SWFTools//";
String command = "pdf2swf -z -s flashversion=9 /"" + sourcePath
+ "/" -o /"" + destPath + fileName + "/"";
Process pro = Runtime.getRuntime().exec(command, envp);
// System.out.println(command);
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(pro.getInputStream()));
while (bufferedReader.readLine() != null) {
String text = bufferedReader.readLine();
System.out.println(text);
}
try {
pro.waitFor();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 然后在套播放器
/*
* swfcombine -z -X 720 -Y 540 "D:/tools/SWFTools/swfs/rfxview.swf"
* viewport="d:/人
* 员管理系统PersonalManagementSystem简介.swf" -o "d:/人员管理系统PersonalManagemen
* tSystem简介.swf"
*/
command = "swfcombine -z -X 720 -Y 540 /"D:/tools/SWFTools/swfs/rfxview.swf/" viewport=/""
+ destPath + fileName + "/" -o /"" + destPath + fileName + "/"";
pro = Runtime.getRuntime().exec(command, envp);
System.out.println(command);
bufferedReader = new BufferedReader(new InputStreamReader(pro
.getInputStream()));
while (bufferedReader.readLine() != null) {
String text = bufferedReader.readLine();
System.out.println(text);
}
try {
pro.waitFor();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return pro.exitValue();
}
public static void main(String[] args) {
String sourcePath = "d:/PersonalManagementSystem.pdf";
String destPath = "d:/";
String fileName = "PersonalManagementSystem.swf";
try {
System.out.println(new PdfToSwf().convertPDF2SWF(sourcePath,
destPath, fileName));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
好了,基本就解决了,丢网页上就可以了,这个偶就不说了……
……过两天把她们集成到项目里面偶就安心了
PS,貌似,好像,听说,在转换某系中文的时候有乱码,但是偶没碰见……如果中奖了……,去看看
光头的专栏