flexPaper形式实现文档预览功能
大致步骤如下
1、安装openoffice程序,作为后台服务启动
soffice -headless -accept="socket,host=172.19.15.143,port=8100;urp;" -nofirststartwizard &
2、安装SWFTools
3、使用jodconverter-2.2.2,先把excel、word转化成pdf文件
在用pdf2swf.exe把生成的pdf文件转化成swf文件。然后调用flexpaper实现在线浏览该文件。
public class DocumentConvert {
public void convert() throws Exception {
doc2pdf();
pdf2swf();
}
/**
*
* 方法说明:
* @throws Exception
*
*/
public void doc2pdf() throws Exception {
// pdf格式文件是否存在不进行转换
if (pdfFile.exists()||docFile.equals(pdfFile)) {
return ;
}
OpenOfficeConnection connection = new SocketOpenOfficeConnection();
try {
connection.connect();
DocumentConverter converter = new OpenOfficeDocumentConverter(
connection);
converter.convert(docFile, pdfFile);
// close the connection
connection.disconnect();
logger.debug("****pdf转换成功,PDF输出:" + pdfFile.getPath()
+ "****");
} catch (java.net.ConnectException e) {
e.printStackTrace();
logger.debug("****swf转换器异常,openoffice服务未启动!****");
throw e;
} catch (OpenOfficeException e) {
e.printStackTrace();
logger.debug("****swf转换器异常,读取转换文件失败****");
throw e;
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
/**
*
* 方法说明:
*
* @throws Exception
*
*/
public void pdf2swf() throws Exception {
// swf文件已经存在,不需要转换
if (swfFile.exists()) {
return;
}
// pdf文件不存在无法进行转换
if (!pdfFile.exists()) {
return;
}
Runtime r = Runtime.getRuntime();
Process p=null;
//windows环境
if (isWindowsSystem()) {
p = r.exec(SWFTools+" "
+ pdfFile.getPath() + " -o " + swfFile.getPath() + " -T 9");
} else {
p = r.exec(SWFTools+" " + 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();
}
}
/**
*
* 方法说明:判断是否Windows操作系统
*
* @return
*
*/
private static boolean isWindowsSystem() {
String p = System.getProperty("os.name");
return p.toLowerCase().indexOf("windows") >= 0 ? true : false;
}
private 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();
}
public String getSWFPath() {
if (swfFile.exists()) {
String tempString = swfFile.getPath();
tempString = tempString.replaceAll("\\\\", "/");
return tempString;
} else {
return "";
}
}
}