Java实现在线预览–openOffice实现[通俗易懂]

Java实现在线预览–openOffice实现[通俗易懂]Java实现在线预览–openOffice实现

大家好,又见面了,我是你们的朋友全栈君。

#Java实现在线预览–openOffice实现
##简介
之前有写了poi实现在线预览的文章,里面也说到了使用openOffice也可以做到,这里就详细介绍一下。
我的实现逻辑有两种:
一、利用jodconverter(基于OpenOffice服务)将文件(.doc、.docx、.xls、.ppt)转化为html格式。
二、利用jodconverter(基于OpenOffice服务)将文件(.doc、.docx、.xls、.ppt)转化为pdf格式。
转换成html格式大家都能理解,这样就可以直接在浏览器上查看了,也就实现了在线预览的功能;转换成pdf格式这点,需要用户安装了Adobe Reader XI,这样你会发现把pdf直接拖到浏览器页面可以直接打开预览,这样也就实现了在线预览的功能。
##将文件转化为html格式或者pdf格式
话不多说,直接上代码。

package com.pdfPreview.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ConnectException;
import java.text.SimpleDateFormat;
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;
/**
 * 利用jodconverter(基于OpenOffice服务)将文件(*.doc、*.docx、*.xls、*.ppt)转化为html格式或者pdf格式,
 * 使用前请检查OpenOffice服务是否已经开启, OpenOffice进程名称:soffice.exe | soffice.bin
 * 
 * @author yjclsx
 */
public class Doc2HtmlUtil {

	private static Doc2HtmlUtil doc2HtmlUtil;

	/**
	 * 获取Doc2HtmlUtil实例
	 */
	public static synchronized Doc2HtmlUtil getDoc2HtmlUtilInstance() {
		if (doc2HtmlUtil == null) {
			doc2HtmlUtil = new Doc2HtmlUtil();
		}
		return doc2HtmlUtil;
	}

	/**
	 * 转换文件成html
	 * 
	 * @param fromFileInputStream:
	 * @throws IOException 
	 */
	public String file2Html(InputStream fromFileInputStream, String toFilePath,String type) throws IOException {
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		String timesuffix = sdf.format(date);
		String docFileName = null;
		String htmFileName = null;
		if("doc".equals(type)){
			docFileName = "doc_" + timesuffix + ".doc";
			htmFileName = "doc_" + timesuffix + ".html";
		}else if("docx".equals(type)){
			docFileName = "docx_" + timesuffix + ".docx";
			htmFileName = "docx_" + timesuffix + ".html";
		}else if("xls".equals(type)){
			docFileName = "xls_" + timesuffix + ".xls";
			htmFileName = "xls_" + timesuffix + ".html";
		}else if("ppt".equals(type)){
			docFileName = "ppt_" + timesuffix + ".ppt";
			htmFileName = "ppt_" + timesuffix + ".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();
		if (docInputFile.exists())
			docInputFile.delete();
		docInputFile.createNewFile();
		/**
		 * 由fromFileInputStream构建输入文件
		 */
		try {
			OutputStream os = new FileOutputStream(docInputFile);
			int bytesRead = 0;
			byte[] buffer = new byte[1024 * 8];
			while ((bytesRead = fromFileInputStream.read(buffer)) != -1) {
				os.write(buffer, 0, bytesRead);
			}

			os.close();
			fromFileInputStream.close();
		} catch (IOException e) {
		}
	
	    OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
		try {
			connection.connect();
		} catch (ConnectException e) {
			System.err.println("文件转换出错,请检查OpenOffice服务是否启动。");
		}
		// convert
		DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
		converter.convert(docInputFile, htmlOutputFile);
		connection.disconnect();
		// 转换完之后删除word文件
		docInputFile.delete();
		return htmFileName;
	}
	
	/**
	 * 转换文件成pdf
	 * 
	 * @param fromFileInputStream:
	 * @throws IOException 
	 */
	public String file2pdf(InputStream fromFileInputStream, String toFilePath,String type) throws IOException {
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		String timesuffix = sdf.format(date);
		String docFileName = null;
		String htmFileName = null;
		if("doc".equals(type)){
			docFileName = "doc_" + timesuffix + ".doc";
			htmFileName = "doc_" + timesuffix + ".pdf";
		}else if("docx".equals(type)){
			docFileName = "docx_" + timesuffix + ".docx";
			htmFileName = "docx_" + timesuffix + ".pdf";
		}else if("xls".equals(type)){
			docFileName = "xls_" + timesuffix + ".xls";
			htmFileName = "xls_" + timesuffix + ".pdf";
		}else if("ppt".equals(type)){
			docFileName = "ppt_" + timesuffix + ".ppt";
			htmFileName = "ppt_" + timesuffix + ".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();
		if (docInputFile.exists())
			docInputFile.delete();
		docInputFile.createNewFile();
		/**
		 * 由fromFileInputStream构建输入文件
		 */
		try {
			OutputStream os = new FileOutputStream(docInputFile);
			int bytesRead = 0;
			byte[] buffer = new byte[1024 * 8];
			while ((bytesRead = fromFileInputStream.read(buffer)) != -1) {
				os.write(buffer, 0, bytesRead);
			}

			os.close();
			fromFileInputStream.close();
		} catch (IOException e) {
		}

		OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
		try {
			connection.connect();
		} catch (ConnectException e) {
			System.err.println("文件转换出错,请检查OpenOffice服务是否启动。");
		}
		// convert
		DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
		converter.convert(docInputFile, htmlOutputFile);
		connection.disconnect();
		// 转换完之后删除word文件
		docInputFile.delete();
		return htmFileName;
	}
	
	public static void main(String[] args) throws IOException {
		Doc2HtmlUtil coc2HtmlUtil = getDoc2HtmlUtilInstance();
		File file = null;
		FileInputStream fileInputStream = null;
		
		file = new File("D:/poi-test/exportExcel.xls");
		fileInputStream = new FileInputStream(file);
//		coc2HtmlUtil.file2Html(fileInputStream, "D:/poi-test/openOffice/xls","xls");
		coc2HtmlUtil.file2pdf(fileInputStream, "D:/poi-test/openOffice/xls","xls");
		
		file = new File("D:/poi-test/test.doc");
		fileInputStream = new FileInputStream(file);
//		coc2HtmlUtil.file2Html(fileInputStream, "D:/poi-test/openOffice/doc","doc");
		coc2HtmlUtil.file2pdf(fileInputStream, "D:/poi-test/openOffice/doc","doc");
		
		file = new File("D:/poi-test/周报模版.ppt");
		fileInputStream = new FileInputStream(file);
//		coc2HtmlUtil.file2Html(fileInputStream, "D:/poi-test/openOffice/ppt","ppt");
		coc2HtmlUtil.file2pdf(fileInputStream, "D:/poi-test/openOffice/ppt","ppt");
		
		file = new File("D:/poi-test/test.docx");
		fileInputStream = new FileInputStream(file);
//		coc2HtmlUtil.file2Html(fileInputStream, "D:/poi-test/openOffice/docx","docx");
		coc2HtmlUtil.file2pdf(fileInputStream, "D:/poi-test/openOffice/docx","docx");
		
	}

}

转换成html和转换成pdf的过程几乎一样,只是在创建输出的File时前者命名为XXX.html,后者命名为XXX.pdf,在执行converter.convert(docInputFile, htmlOutputFile);时,jodconverter会自己根据文件类型名转换成对应的文件。
注意,main方法里别file2Html和file2pdf都调用,会报错的,要么转html,要么转pdf,只能选一个。还有就是在执行之前,需要启动openOffice的服务:在openOffice目录下的命令窗口中执行soffice -headless -accept=“socket,host=127.0.0.1,port=8100;urp;” -nofirststartwizard即可启动。
以上需要引入jodconverter的jar包。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/133343.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • 静态代理详解[通俗易懂]

    静态代理详解[通俗易懂]1.什么是静态代理?代理这个词是来源于Java设计模式中的代理模式,代理模式最简单的理解就是通过第三方来代理我们的工作比如中介,房东需要将自己的房子租出去,而租客需要租房子,三者关系如此租客租房子一般都找不到房东,房东也不会轻易将自己暴露给广大租客,因此就需要中介来充当这个中间关系因此租客就只能通过中介来进行租房子这个工作,不需要通过房东,这就叫做代理—-就是中介代理房东来处理租房子这件事情那么我们应用于Java中又是什么样的情况呢?首先创建一个主题接口(别问为什么创建接口,J.

    2022年10月16日
  • mac phpstorm 2021.4.14 激活码_通用破解码

    mac phpstorm 2021.4.14 激活码_通用破解码,https://javaforall.cn/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

  • Linux stat函数_c++ stringbuffer

    Linux stat函数_c++ stringbuffer之前写过一篇关于stat命令的博客,介绍了stat命令的使用和输出信息表示,今天又见到了stat函数,因为输出原因,准备整理一下。stat函数介绍  根据《UNIX环境高级编程》中对于stat函数的解释,stat函数和stat命令一样,都是返回该文件的详细信息。函数定义为:#include<sys/types.h>#include&lt…

  • pycharm 搜索快捷键_word替换文字快捷键

    pycharm 搜索快捷键_word替换文字快捷键PyCharm快捷键——搜索/替换快捷键搜索/替换快捷键序号快捷键作用1CTRL+F查找2F3查找下一个3SHIFT+F3查找上一个4CTRL+R替换5CTRL+SHIFT+F指定路径下查找6CTRL+SHIFT+R指定路径下替换

  • OpenStack八大核心组件精讲之—neutron理论知识

    OpenStack八大核心组件精讲之—neutron理论知识OpenStack八大核心组件精讲之—neutron理论知识一、OpenStack网络二、Linux网络虚拟化(一)、Linux虚拟网桥(二)、虚拟局域网(三)、开发虚拟交换机三、openstack网络基础服务1、neutron网络结构2、网络子网与端口3、网络拓扑类型①、Local②、Flat③、VLAN④、VXLAN⑤、GRE⑥、GENEVE⑦、总结4、网络基本架构5、neutron-server6、Neutron遵循OpenStack的设计原则,采用开放性架构,通过插件.代理与网络提供者的配合来实

  • 三次握手几个rtt_tcp三次握手详细过程

    三次握手几个rtt_tcp三次握手详细过程一、RST包、本人学习后总结:RST包用于强制关闭TCP链接。TCP连接关闭的正常方法是四次握手。但四次握手不是关闭TCP连接的唯一方法.有时,如果主机需要尽快关闭连接(或连接超时,端口或主机不可达),RST(Reset)包将被发送.注意,由于RST包不是TCP连接中的必须部分,可以只发送RST包(即不带ACK标记).但在正常的TCP连接中RST包可以带ACK确认标记。 二、三次握手Th…

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号