java在线⽂档_java实现在线⽂档浏览
⽬前发现两种⽅法:
1、使⽤openoffice将office转换为pdf⽂档,使⽤swfTools将pdf⽂档转换为swf⽂件,通过flexpaper插件展⽰;
2、webOffice插件,直接展⽰。
在这⾥先说下第⼀种:
第⼀ office--->pdf
然后在cmd环境下进⼊安装⽬录的program⽬录,输⼊打开openoffice的命令:
soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard
输⼊完成之后在任务管理器可以看见soffice.bin的进程在任务管理器,这⼀服务就启动成功。当然在代码中转换office2pdf我们还需要⼀
些架包。
下载jodconverter-2.2.2架包,然后复制到lib⽬录下,引⼊架包就可以了。这个架包有如下包:
如果报错 The type com.sun.star.lang.XEventListener cannot be resolved. It is indirectly referenced from required异常:
解决办法:到官⽅⽹站下载jodconverter-2.2.2.zip,然后把lib⽂件夹下得所有jar包都拷进去,就可以了。
packagecom.util;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.FileOutputStr
* @time xx*/
public classConvertPdf {/***@paramfilePath 上传⽂件所在⽂件夹的绝对路径
*@paramfileName ⽂件名称
*@return⽣成pdf路径\⽂件名*/
publicString beginConvert(String filePath, String fileName,String fastName) {final String DOC = ".doc
";final String DOCX =
".docx";final String XLS = ".xls";final String XLSX = ".xlsx";final String PDF = ".pdf";final String PPT=".ppt";final String
PPTX=".pptx";final String TXT=".txt";
String fileExt= "";if (null != fileName && fileName.indexOf(".") > 0) {int index = fileName.lastIndexOf(".");
fileExt=fileName.substring(index).toLowerCase();
}
String inputFile= filePath + File.separator +fileName;
String outputFile= "";int i = 0;//如果是office⽂档,先转为pdf⽂件
if (fileExt.equals(DOC) || fileExt.equals(DOCX) ||fileExt.equals(XLS)||
fileExt.equals(XLSX)||fileExt.equals(PPT)||fileExt.equals(PPTX)){
outputFile= filePath + File.separator + fastName +PDF;
System.out.println("====源⽂件!===="+inputFile);
System.out.println("====转换后⽂件!===="+outputFile);
i=office2PDF(inputFile, outputFile);
}if(fileExt.equals(TXT)||fileExt.equals(".java")){
File input=newFile(inputFile);
ChangeFileCode changeFileCode= newChangeFileCode();
String FileEnCode(inputFile);if(fileCode!=null && !"".equals(fileCode)) { changeFileCode.Path(), fileCode);//如果⽂件编码为ANSI⽤GBK来读,如果是UTF-8⽤UTF-8来读changeFileCode.Path(), "UTF-8");//UTF-8则⽂件编码为UTF-8, 如果为GBK,编码为ANSI changeFileCode.start();
}
inputFile=filePath + File.separator + fastName + ".odt";
outputFile=filePath + File.separator + fastName +PDF;
System.out.println("====源⽂件!===="+inputFile);
System.out.println("====转换后⽂件!===="+outputFile);
i=office2PDF(inputFile, outputFile);
}if(fileExt.equals(PDF)){//如果是pdf不需要转换 复制改名
outputFile = filePath + File.separator + fastName +PDF;
i=copy(new File(inputFile),newFile(outputFile));
}if(i==0){returnoutputFile;
}else{return "0";
}
}/*** 复制⽂件*/
private intcopy(File fromFile, File toFile){try{if (!ists()){
System.out.println("====来源⽂件为空!====");
}if (!ists()){
System.out.println("====创建新⽂件!====");
}
FileInputStream fis= newFileInputStream(fromFile);
System.out.println("fromFile :" +AbsolutePath());
FileOutputStream fos= newFileOutputStream(toFile);
System.out.println("toFile :" +AbsolutePath());int len = 0;byte[] buf = new byte[1024];while((len = ad(buf)) != -1){
fos.write(buf,0,len);
}
fis.close();
fos.close();
}catch(Exception e) {
System.out.println("====⽂件复制失败====");return -1;
}return 0;
}/*** office⽂档转pdf⽂件
*@paramsourceFile office⽂档绝对路径
*@paramdestFile pdf⽂件绝对路径
*@return
*/
public intoffice2PDF(String sourceFile, String destFile) {
String OpenOffice_HOME= Value("base", "oo_home");
String host_Value("base", "oo_host");
String port_Str= Value("base", "oo_port");
OpenOfficeConnection connection=null;
Process pro=null;try{
File inputFile= newFile(sourceFile);if (!ists()) {
System.out.println("====不到源⽂件===="+sourceFile);return -1; //不到源⽂件
}//如果⽬标路径不存在, 则新建该路径
File outputFile = newFile(destFile);if (!ParentFile().exists()) {
}//windows//启动OpenOffice的服务
String command =OpenOffice_HOME+ "\\program\\ -headless -accept=\"socket,host="
+ host_Str + ",port=" + port_Str + ";urp;\"";//linux
/*String command = openOffice_HOME
+ "/program/soffice --headless --accept=\"socket,host="
+ host_Str + ",port=" + port_Str + ";urp;\" --nofirststartwizard &";*/System.out.println("====command===="+command);
Runtime().exec(command);//连接openoffice服务
connection = newSocketOpenOfficeConnection(host_Str, Integer.parseInt(port_Str));
DocumentConverter converter = newOpenOfficeDocumentConverter(connection);try{
}catch(Exception e) {if(connection!=null){
connection.disconnect();
}if(pro!=null){
pro.destroy();
}
System.out.println("====转换异常!====");
}//关闭连接和服务
connection.disconnect();
pro.destroy();return 0;
}catch(FileNotFoundException e) {
System.out.println("====⽂件未到====");return -1;
}catch(ConnectException e) {
System.out.println("====OpenOffice服务监听异常!====");
}catch(IOException e) {
System.out.println("====IO异常!====");
}return 1;
}public static void main(String[] args) throwsIOException {
ConvertPdf c=newConvertPdf();
String filePath="E:\\";
String fileName="ddd.docx";
System.out.println(">>>"+c.beginConvert(filePath, fileName,"1"));
}
}
第⼆ pdf--->swf
packagecom.util;importjava.io.BufferedInputStream;importjava.io.IOException;importjava.io.InputStream;importorg.slf4j.Logger;将pdf⽂件转成swf格式*/
public classConvertSwf {private final Logger log = Logger(ConvertSwf.class);/*** 将pdf⽂件转换成swf⽂件
*@paramsourceFile pdf⽂件绝对路径
*@paramoutFile swf⽂件绝对路径
*@paramtoolFile 转换⼯具绝对路径*/
publicString convertPdf2Swf(String sourceFile, String outFile) {//linux//String TOOL = "pdf2swf";//windows
String TOOL = "D:\\Program Files (x86)\\SWFTools\\pdf2swf";
String command= TOOL +" "+ sourceFile + " -o " +outFile+ " -s flashversion=9 "+ " -p 0-100"+" -s languagedir=/opt/xpdf-chinese-simplified/ ";try{
Process Runtime().exec(command);
log.info("===="+InputStream())+"====");
log.info("===="+ErrorStream())+"====");
log.info("====转换成功swf====");
}catch(Exception e) {
e.printStackTrace();
}return "";
}static String loadStream(InputStream in) throwsIOException{int ptr = 0;
in= newBufferedInputStream(in);
jquery在线库StringBuffer buffer= newStringBuffer();while ((ad())!= -1){
buffer.append((char)ptr);
}String();
}public static voidmain(String[] args) {
ConvertSwf c= newConvertSwf();
String filePath="E:\\";
String fileName="1.pdf";
}
}
第三 展⽰swf
使⽤这个插件需要有三个js⽂件。分别是:jquery.js、flexpaper_flash.js、flexpaper_flash_debug.js。插件的名字是FlexPaperViewer.swf。
html,body{height:100%;
}body{margin:0;padding:0;overflow:auto;
}#flashContent{display:none;
}
在线⽂档预览
varfp=newFlexPaperViewer('FlexPaperViewer','viewerPlaceHolder',
{
config:{
SwfFile:escape('localhost:8080/TestBaiDu/12.swf'),
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论