JAVA 在线预览

最近系统需要做在线预览,于是我就上网上找现成的插件,查阅相关资料,他们说目前主流实现在线预览的方案有以下3种:

1.flash 的flexpaper 将文档转换为swf格式,然后使用flash在网页中浏览
2.使用开源的软件openoffice+pdf2htmlEx,利用openoffice的接口先将文档转换为pdf格式,然后在使用pdf2htmlEx将文档转换为html直接在网页中显示,
3.自己搭建一个文档预览服务器,基于office web app,也就是微软的office online,开源的内容管理系统KodExplorer就是这么干的。文档在线预览基本上就这么几种方案.

经过测试,只需要将文件转换为PDF格式,再输出到浏览器中就可以实现在线预览了,这也是性能最高的一种方式,避免了多次转换所消耗的时间。
预览样式如下:

图片预览

是不是很简洁??

将文件转换为PDF使用的是openOffice+jodConverter。openOffice可以去网上下载(免费的文字处理软件,注意是一款实实在在的软件,支持MAC OS, LINUX,WINDOS…,具体可以去网上看),需要下载安装。网上人说使用openOffice需要开启服务,但是这边我是在代码中控制开启和关闭openOffice的,因为openOffice服务大约要占100M内存,所以不使用时就不要开启啦。

其次,jar包我在附件中提供了,找不到的可以发私信我。
点我下载jar包

话不多说,看代码:

/** 
 *  
 */
package com.nd.core.util;

import java.io.File;
import java.util.regex.Pattern;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.artofsolving.jodconverter.OfficeDocumentConverter;
import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;
import org.artofsolving.jodconverter.office.OfficeManager;
import org.springframework.util.StringUtils;

/**
 * 这是一个工具类,主要是为了使Office2003-2007全部格式的文档(.doc|.docx|.xls|.xlsx|.ppt|.pptx)
 * 转化为pdf文件<br>
 * Office2010的没测试<br>
 * 
 * @date 2017-03-03
 * @author jjc
 * 
 */
public class Office2PDF {

	private static final Log LOG = LogFactory.getLog(Office2PDF.class);

	/**
	 * 使Office2003-2007全部格式的文档(.doc|.docx|.xls|.xlsx|.ppt|.pptx) 转化为pdf文件<br>
	 * 
	 * @param inputFilePath
	 *            源文件路径,如:"e:/test.docx"
	 * @return
	 */
	public static File openOfficeToPDF(String inputFilePath) {
		return office2pdf(inputFilePath);
	}

	/**
	 * 根据操作系统的名称,获取OpenOffice.org 3的安装目录<br>
	 * 如我的OpenOffice.org 3安装在:C:/Program Files (x86)/OpenOffice.org 3<br>
	 * 
	 * @return OpenOffice.org 3的安装目录
	 */
	public static String getOfficeHome() {
		String osName = System.getProperty("os.name");
		System.out.println("操作系统名称:" + osName);
		if (Pattern.matches("Linux.*", osName)) {
			return "/opt/openoffice.org3";
		} else if (Pattern.matches("Windows.*", osName)) {
			return "C:/Program Files/OpenOffice 4";
		} else if (Pattern.matches("Mac.*", osName)) {
			return "/Applications/OpenOffice.org.app/Contents/";
		}
		return null;
	}

	/**
	 * 连接OpenOffice.org 并且启动OpenOffice.org
	 * 
	 * @return
	 */
	public static OfficeManager getOfficeManager() {
		DefaultOfficeManagerConfiguration config = new DefaultOfficeManagerConfiguration();
		// 设置OpenOffice.org 3的安装目录
		config.setOfficeHome(getOfficeHome());
		// 启动OpenOffice的服务
		OfficeManager officeManager = config.buildOfficeManager();
		officeManager.start();
		return officeManager;
	}

	/**
	 * 转换文件
	 * 
	 * @param inputFile
	 * @param outputFilePath_end
	 * @param inputFilePath
	 * @param outputFilePath
	 * @param converter
	 */
	public static File converterFile(File inputFile, String outputFilePath_end, String inputFilePath,
			OfficeDocumentConverter converter) {
		File outputFile = new File(outputFilePath_end);
		// 假如目标路径不存在,则新建该路径
		if (!outputFile.getParentFile().exists()) {
			outputFile.getParentFile().mkdirs();
		}
		converter.convert(inputFile, outputFile);
		System.out.println("文件:" + inputFilePath + "\n转换为\n目标文件:" + outputFile + "\n成功!");
		return outputFile;
	}

	/**
	 * 使Office2003-2007全部格式的文档(.doc|.docx|.xls|.xlsx|.ppt|.pptx) 转化为pdf文件<br>
	 * 
	 * @param inputFilePath
	 *            源文件路径,如:"e:/test.docx"
	 * @param outputFilePath
	 *            目标文件路径,如:"e:/test_docx.pdf"
	 * @return
	 */
	public static File office2pdf(String inputFilePath) {
		OfficeManager officeManager = null;
		try {
			if (StringUtils.isEmpty(inputFilePath)) {
				LOG.info("输入文件地址为空,转换终止!");
				return null;
			}

			File inputFile = new File(inputFilePath);
			// 转换后的文件路径
			String outputFilePath_end = getOutputFilePath(inputFilePath);

			if (!inputFile.exists()) {
				LOG.info("输入文件不存在,转换终止!");
				return null;
			}

			// 获取OpenOffice的安装路劲
			officeManager = getOfficeManager();
			// 连接OpenOffice
			OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);

			return converterFile(inputFile, outputFilePath_end, inputFilePath, converter);
		} catch (Exception e) {
			LOG.error("转化出错!", e);
		} finally {
			// 停止openOffice
			if (officeManager != null) {
				officeManager.stop();
			}
		}
		return null;
	}

	/**
	 * 获取输出文件
	 * 
	 * @param inputFilePath
	 * @return
	 */
	public static String getOutputFilePath(String inputFilePath) {
		String outputFilePath = inputFilePath.replaceAll("." + getPostfix(inputFilePath), ".pdf");
		return outputFilePath;
	}

	/**
	 * 获取inputFilePath的后缀名,如:"e:/test.pptx"的后缀名为:"pptx"<br>
	 * 
	 * @param inputFilePath
	 * @return
	 */
	public static String getPostfix(String inputFilePath) {
		return inputFilePath.substring(inputFilePath.lastIndexOf(".") + 1);
	}

	public static void main(String[] args) {
		Office2PDF.openOfficeToPDF("/Users/JJC/Downloads/20170302汽修服务测试反馈.docx");
	}

}

以上代码需要注意的是openOffice的安装路劲,需要自行更改getOfficeHome()函数的返回值。输入路劲必须要是绝对地址哦!

以上代码实现了文件转换为PDF,下面代码是如何实现PDF在浏览器中显示:

File file = Office2PDF.openOfficeToPDF("/Users/JJC/Downloads/20170302汽修服务测试反馈.docx");
		BufferedInputStream br = new BufferedInputStream(new FileInputStream(file));
		byte[] buf = new byte[1024];
		int len = 0;
		response.reset(); // 非常重要
		response.setContentType("application/pdf");
		response.setHeader("Content-Disposition",
				"inline; filename=" + java.net.URLEncoder.encode(file.getName(), "UTF-8"));

		OutputStream out = response.getOutputStream();
		while ((len = br.read(buf)) != -1)
			out.write(buf, 0, len);
		br.close();
		out.close();

以上就是在线预览的全部代码,如果需要部署上LINUX服务器,并且如果LINUX上使用到了FASTDFS等文件服务器的话,需要自行变动哦。


注意:以上代码不支持多线程环境,如果要支持多线程只需要将openoffice当成一个服务启动着,代码中每次转换完不要关闭就行了。
支持多线程代码如下:

/** 
 *  
 */
package com.nd.core.util;

import java.io.File;
import java.util.regex.Pattern;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;
import org.artofsolving.jodconverter.office.OfficeManager;
import org.springframework.util.StringUtils;

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;
import com.nd.core.constant.Constant;

/**
 * 这是一个工具类,主要是为了使Office2003-2007全部格式的文档(.doc|.docx|.xls|.xlsx|.ppt|.pptx)
 * 转化为pdf文件<br>
 * Office2010的没测试<br>
 * 
 * @date 2017-03-03
 * @author jjc
 * 
 */
public class Office2PDF {

	private static final Log LOG = LogFactory.getLog(Office2PDF.class);

	/**
	 * 使Office2003-2007全部格式的文档(.doc|.docx|.xls|.xlsx|.ppt|.pptx) 转化为pdf文件<br>
	 * 
	 * @param inputFilePath
	 *            源文件路径,如:"e:/test.docx"
	 * @return
	 */
	public synchronized static void openOfficeToPDF(String inputFilePath) {
		convert(inputFilePath, "pdf");
	}

	public synchronized static void openOfficeToHtml(String inputFilePath) {
		convert(inputFilePath, "html");
	}

	/**
	 * 根据操作系统的名称,获取OpenOffice.org 3的安装目录<br>
	 * 如我的OpenOffice.org 3安装在:C:/Program Files (x86)/OpenOffice.org 3<br>
	 * 
	 * @return OpenOffice.org 3的安装目录
	 */
	public static String getOfficeHome() {
		String osName = System.getProperty("os.name");
		System.out.println("操作系统名称:" + osName);
		if (Pattern.matches("Linux.*", osName)) {
			return "/home/soft/openoffice4";
		} else if (Pattern.matches("Windows.*", osName)) {
			return "C:/Program Files/OpenOffice 4";
		} else if (Pattern.matches("Mac.*", osName)) {
			return "/Applications/OpenOffice.org.app/Contents/";
		}
		return null;
	}

	/**
	 * 连接OpenOffice.org 并且启动OpenOffice.org
	 * 
	 * @return
	 */
	public static OfficeManager getOfficeManager() {
		DefaultOfficeManagerConfiguration config = new DefaultOfficeManagerConfiguration();
		// 设置OpenOffice.org 3的安装目录
		config.setOfficeHome(getOfficeHome());
		// 启动OpenOffice的服务
		OfficeManager officeManager = config.buildOfficeManager();
		try {
			officeManager.start();
		} catch (Exception e) {
			LOG.error("openOffice启动失败");
		}

		return officeManager;
	}

	/**
	 * 转换文件
	 * 
	 * @param inputFile
	 * @param outputFilePath_end
	 * @param inputFilePath
	 * @param outputFilePath
	 * @param converter
	 */
	public static void converterFile(File inputFile, String outputFilePath_end, String inputFilePath,
			DocumentConverter converter) {
		File outputFile = new File(outputFilePath_end);
		// 假如目标路径不存在,则新建该路径
		if (!outputFile.getParentFile().exists()) {
			outputFile.getParentFile().mkdirs();
		}
		converter.convert(inputFile, outputFile);
		System.out.println("文件:" + inputFilePath + "\n转换为\n目标文件:" + outputFile + "\n成功!");
	}

	/**
	 * 转换
	 * 
	 * @param inputFilePath
	 *            输入路径
	 * @param outputFileType
	 *            输出文件后缀如: html
	 */
	private static void convert(String inputFilePath, String outputFileType) {
		// OfficeManager officeManager = null;
		try {
			if (StringUtils.isEmpty(inputFilePath)) {
				LOG.info("输入文件地址为空,转换终止!");
				return;
			}

			File inputFile = new File(inputFilePath);
			if (!inputFile.exists()) {
				LOG.info("输入文件不存在,转换终止!");
				return;
			}

			// 转换后的文件路径
			String outputFilePath_end = getOutputFilePath(inputFilePath, outputFileType);

			// officeManager = getOfficeManager();
			//
			// // 连接OpenOffice
			// OfficeDocumentConverter converter = new
			// OfficeDocumentConverter(officeManager);

			OpenOfficeConnection connection = new SocketOpenOfficeConnection(
					Integer.valueOf(ConfigUtil.getInstant().getValue(Constant.PREVIEW_PORT)));
			connection.connect();
			DocumentConverter converter = new OpenOfficeDocumentConverter(connection);

			converterFile(inputFile, outputFilePath_end, inputFilePath, converter);

		} catch (Exception e) {
			LOG.error("OpenOffice error!", e);
		}

		// finally {
		// // 关闭OpenOffice
		// if (officeManager != null) {
		// officeManager.stop();
		// }
		// }

	}

	/**
	 * 获取输出文件
	 * 
	 * @param inputFilePath
	 * @return
	 */
	public static String getOutputFilePath(String inputFilePath, String outputFileType) {
		return inputFilePath.replaceAll(getPostfix(inputFilePath), outputFileType);
	}

	/**
	 * 获取inputFilePath的后缀名,如:"e:/test.pptx"的后缀名为:"pptx"<br>
	 * 
	 * @param inputFilePath
	 * @return
	 */
	public static String getPostfix(String inputFilePath) {
		return inputFilePath.substring(inputFilePath.lastIndexOf(".") + 1);
	}

	public static void main(String[] args) {
		String filePath = "/Users/JJC/Downloads/4月份考试安排.doc";
	}

}

从生产环境表现上看,openoffice转换成pdf对excel兼容性较差,可以转成html格式进行预览。转成html格式预览看这一篇 ,后来,发现了一个对word兼容性更好的工具叫做jacob,使用jacob将word、excel转成html

  • 11
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 33
    评论
评论 33
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值