Office实现文件在线预览

使用OpenOffice实现文件在线预览

https://www.cnblogs.com/huangyanqi/p/9675440.html

一、解决方案

方案1:

可以直接使用第三方服务,不过这个需要收费的,我在这列几个
http://www.yozodcs.com/
https://www.idocv.com/
http://www.officeweb365.com/

方案2:

服务器安装office web apps文件浏览服务器,实现文件预览,安装部署方法可自行百度

方案3:

office转Html、pdf实现在线预览文件

本文主要介绍使用openoffice开源软件实现pdf文件转换,当前的主流浏览器都是支持直接打开pdf格式文件,从而实现文件预览

二、openoffice实现文件格式转换

1.openoffice软件安装

Windows下安装openoffice:

  1. 下载openoffice4安装 www.openoffice.org
  2. openoffice服务启动

安装OpenOffice成功后,您可以进入<OpenOffice安装目录>/program/目录并运行以下命令启动OpenOffice服务:

soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard

linux下安装openoffice:

  1. 下载Apache_OpenOffice_4.1.6_Linux_x86-64_install-rpm_zh-CN.tar.gz
  2. 本人资源包放在 /opt/moudles 中, 解压后放在 /opt/softwares 中

解压 openoffice包

[root@localhost moudles]# tar -zxvf Apache_OpenOffice_4.1.6_Linux_x86-64_install-rpm_zh-CN.tar.gz  -C ../softwares/

解压之后会在 /opt/softwares 中生成 zh-CN文件夹,进入zh-CN中RPMS文件夹

[root@localhost moudles]# cd /opt/softwares/zh-CN/RPMS

运行 yum localinstall *.rpm

[root@localhost RPMS]# yum localinstall *.rpm

成功之后会在当前目录生成 desktop-integration文件夹,运行 yum localinstall 。

[root@localhost desktop-integration]# yum localinstall openoffice4.1.6-redhat-menus-4.1.6-9788.noarch. rpm

(如果上一句执行不成功,改用

​ cd desktop-integration ,执行:rpm -ivh openoffice4.1.6-redhat-menus-4.1.6-9783.noarch.rpm;

安装成功会在 /opt 目录下生成openoffice4文件夹

*启动:*

临时启动: 临时启动之后画面就不会动了, 不要认为是死机。只要不报错就是好现象。

[root@localhost desktop-integration]# /opt/openoffice4/program/soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" –nofirststartwizard

永久启动:

[root@localhost desktop-integration]# /opt/openoffice4/program/soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard &

查看是否启动成功:

[root@localhost opt]# ps -ef|grep openoffice

img

查看端口:

[root@localhost opt]# netstat -lnp |grep 8100

img

说明启动成功, 大功告成!

2.文件转换代码

(1)导入jar包

<dependency>



    <groupId>com.artofsolving</groupId>



    <artifactId>jodconverter</artifactId>



    <version>2.2.2</version>



</dependency>



<dependency>



    <groupId>org.openoffice</groupId>



    <artifactId>juh</artifactId>



    <version>3.2.1</version>



</dependency>



<dependency>



    <groupId>org.openoffice</groupId>



    <artifactId>ridl</artifactId>



    <version>3.2.1</version>



</dependency>



<dependency>



    <groupId>org.openoffice</groupId>



    <artifactId>unoil</artifactId>



    <version>3.2.1</version>



</dependency>



<dependency>



    <groupId>commons-cli</groupId>



    <artifactId>commons-cli</artifactId>



    <version>1.1</version>



    <optional>true</optional>



</dependency>



<dependency>



    <groupId>com.thoughtworks.xstream</groupId>



    <artifactId>xstream</artifactId>



    <version>1.3.1</version>



</dependency>

(2)工具类代码

import java.io.File;



import java.io.FileOutputStream;



import java.io.IOException;



import java.io.InputStream;



import java.io.OutputStream;



 



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.StreamOpenOfficeDocumentConverter;



 



public class Doc2HtmlUtil {



	



	



    /**



     * 转换文件成pdf



     * 



     * @param fromFileInputStream:



     * @throws IOException 



     */



    public static String file2pdf(InputStream fromFileInputStream, String toFilePath,String type) throws Exception{



        String timesuffix = IDUtil.getID();



        String docFileName = null;



        String htmFileName = null;



        if("doc".equals(type)){



            docFileName = timesuffix.concat(".doc");



            htmFileName = timesuffix.concat(".pdf");



        }else if("docx".equals(type)){



            docFileName = timesuffix.concat(".docx");



            htmFileName = timesuffix.concat(".pdf");



        }else if("xls".equals(type)){



            docFileName = timesuffix.concat(".xls");



            htmFileName = timesuffix.concat(".pdf");



        }else if("xlsx".equals(type)){



            docFileName = timesuffix.concat(".xlsx");



            htmFileName = timesuffix.concat(".pdf");



        }else if("ppt".equals(type)){



            docFileName = timesuffix.concat(".ppt");



            htmFileName = timesuffix.concat(".pdf");



        }else if("pptx".equals(type)){



            docFileName = timesuffix.concat(".pptx");



            htmFileName = timesuffix.concat(".pdf");



        }else if("txt".equals(type)){



            docFileName = timesuffix.concat(".txt");



            htmFileName = timesuffix.concat(".pdf");



        }else{



        	return null;



        }



 



        File htmlOutputFile = new File(toFilePath + File.separatorChar + htmFileName);



        File docInputFile = new File(toFilePath + File.separatorChar + docFileName);



        if (htmlOutputFile.exists()){



        	htmlOutputFile.delete();



        }



 



		htmlOutputFile.createNewFile();



 



        docInputFile.createNewFile();



        



        /**



         * 由fromFileInputStream构建输入文件



         */



        int bytesRead = 0;



        byte[] buffer = new byte[1024];



    	OutputStream os = new FileOutputStream(docInputFile);



		while ((bytesRead = fromFileInputStream.read(buffer)) != -1) {



		    os.write(buffer, 0, bytesRead);



		}



		os.close();



        fromFileInputStream.close();



        



        OpenOfficeConnection connection = new SocketOpenOfficeConnection(FSUtil.FS_FILE_CONVERT_HOST,8100);



		connection.connect();



        // convert



        DocumentConverter converter = new StreamOpenOfficeDocumentConverter(connection);



        converter.convert(docInputFile, htmlOutputFile);



        connection.disconnect();



        // 转换完之后删除word文件



        docInputFile.delete();



        return htmFileName;



    }



    /**



     * 文件转换成Html



     * @param fromFileInputStream



     * @param toFilePath



     * @param type



     * @return



     * @throws Exception



     */



    public static String file2Html (InputStream fromFileInputStream, String toFilePath,String type) throws Exception{



		String timesuffix = IDUtil.getID();



        String docFileName = null;



        String htmFileName = null;



        if("doc".equals(type)){



            docFileName = timesuffix.concat(".doc");



            htmFileName = timesuffix.concat(".html");



        }else if("docx".equals(type)){



            docFileName = timesuffix.concat(".docx");



            htmFileName = timesuffix.concat(".html");



        }else if("xls".equals(type)){



            docFileName = timesuffix.concat(".xls");



            htmFileName = timesuffix.concat(".html");



        }else if("xlsx".equals(type)){



            docFileName = timesuffix.concat(".xlsx");



            htmFileName = timesuffix.concat(".html");



        }else if("ppt".equals(type)){



            docFileName = timesuffix.concat(".ppt");



            htmFileName = timesuffix.concat(".html");



        }else if("pptx".equals(type)){



            docFileName = timesuffix.concat(".pptx");



            htmFileName = timesuffix.concat(".html");



        }else if("txt".equals(type)){



            docFileName = timesuffix.concat(".txt");



            htmFileName = timesuffix.concat(".html");



        }else if("pdf".equals(type)){



        	docFileName = timesuffix.concat(".pdf");



			htmFileName = timesuffix.concat(".html");



        }else{



        	return null;



        }



		File htmlOutputFile = new File(toFilePath + File.separatorChar + htmFileName);



        File docInputFile = new File(toFilePath + File.separatorChar + docFileName);



        if (htmlOutputFile.exists()){



        	htmlOutputFile.delete();



        }



		htmlOutputFile.createNewFile();



        docInputFile.createNewFile();



        /**



         * 由fromFileInputStream构建输入文件



         */



        int bytesRead = 0;



        byte[] buffer = new byte[1024 * 8];



    	OutputStream os = new FileOutputStream(docInputFile);



		while ((bytesRead = fromFileInputStream.read(buffer)) != -1) {



		    os.write(buffer, 0, bytesRead);



		}



		os.close();



        fromFileInputStream.close();



        OpenOfficeConnection connection = new SocketOpenOfficeConnection(FSUtil.FS_FILE_CONVERT_HOST,8100);



		connection.connect();



        // convert



        DocumentConverter converter = new StreamOpenOfficeDocumentConverter(connection);



        converter.convert(docInputFile, htmlOutputFile);



        connection.disconnect();



        // 转换完之后删除word文件



        docInputFile.delete();



        return htmFileName;



	}	



}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值