Office实现文件在线预览
程序开发
2023-09-09 11:07:32
使用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:
- 下载openoffice4安装 www.openoffice.org;
- openoffice服务启动
安装OpenOffice成功后,您可以进入
soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard
linux下安装openoffice:
- 下载Apache_OpenOffice_4.1.6_Linux_x86-64_install-rpm_zh-CN.tar.gz
- 本人资源包放在 /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
查看端口:
[root@localhost opt]# netstat -lnp |grep 8100
说明启动成功, 大功告成!
2.文件转换代码
(1)导入jar包
com.artofsolving jodconverter 2.2.2 org.openoffice juh 3.2.1 org.openoffice ridl 3.2.1 org.openoffice unoil 3.2.1 commons-cli commons-cli 1.1 true com.thoughtworks.xstream xstream 1.3.1
(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();// convertDocumentConverter 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();// convertDocumentConverter converter = new StreamOpenOfficeDocumentConverter(connection);converter.convert(docInputFile, htmlOutputFile);connection.disconnect();// 转换完之后删除word文件docInputFile.delete();return htmFileName;} }
标签:
上一篇:
用css设置锚点:点击一个元素会跳到相应的位置
下一篇:
相关文章
-
无相关信息