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包。

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

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

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • pip如何卸载库_查看python版本的命令是

    pip如何卸载库_查看python版本的命令是pipfreeze>allpackages.txtpipuninstall-rallpackages.txt-y

    2022年10月10日
    1
  • SpringMVC学习—使用ajax返回Json出现乱码解决方法[通俗易懂]

    在使用ajax请求后台访问数据的数据,后台返回的数据是乱码,带??问号的乱码,之前还一直没有遇到过,在这里记录整理一下,贴出解决代码!

    2022年2月25日
    35
  • 解决Pycharm和pip都安装TensorFlow失败的问题(Windows 10)

    解决Pycharm和pip都安装TensorFlow失败的问题(Windows 10)pip报错:Couldnotfindaversionthatsatisfiestherequirementtensorflow(fromversions:)NomatchingdistributionfoundfortensorflowPycharm报错:Erroroccuredwheninstallingpackage‘tensorflow’解决…

    2022年8月26日
    2
  • vb.net 教程 12-4 msHtml 3

    版权声明:本文为博主原创文章,转载请在显著位置标明本文出处以及作者网名,未经作者允许不得用于商业目的。相比之前学习的HtmlDocument类和HtmlElement类,mshtml还提供了网页元素更详细的分类,比如IHTMLScriptElement:脚本元素IHTMLStyleSheet:样式表IHTMLFormElement:表单元素等等这些不同的元素分类有着自己的…

    2022年4月8日
    37
  • 提高系统可用性

    提高系统可用性计算机网络发展史首先前解释一下什么是计算机网络,计算机网络是指将地理位置不同的具有独立功能的多台计算机及其外部设备,通过通信线路连接起来,在网络操作系统,网络管理软件及网络通信协议的管理和协调下,实现资源共享和信息传递的计算机系统。计算机网络也称计算机通信网。关于计算机网络的最简单定义是:一些相互…

    2022年7月12日
    16
  • 什么是UDP攻击_机器人打电话营销效果

    什么是UDP攻击_机器人打电话营销效果UDP协议UDP是一个简单的面向数据报的运输层协议,也是最常见的作为流量攻击最多的一种协议,需要用到UDP的主要都是视频通讯,枪战类实时通讯的游戏类。UDP不提供可靠性,它只是把应用程序传给IP层的数据报发送出去,但并不保证它们能到达目的地。由于UDP传输数据前传输数据之前源端和终端不建立连接,且没有超时重发等机制,故而传输速度很快。UDP攻击UDP攻击是DDoS攻击的一种,是典型的流量型攻击。就好比学校中午放学时的食堂,学生大量飞奔食堂,但食堂窗口就那么多,学生数量太多,就只能挤在窗口前等待。

    2022年10月2日
    0

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

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