文档附件在线查看(类似百度文库的实现)

需求:用户上传附件后,点击查看,可以在页面直接查看到附件内容,样式排版需要和附件文档里一致。另外可以查看附件信息,下载附件。

                   附件格式 为 excle word 文档,pdf 扫描件

分析:一个附件管理的功能 + 在线查看功能。


附件管理的功能好实现,略过。


在线查看,是通过一个播放器查看flash文件,网上例子很多。

flash播放器 搜索 找到了 FlexPaper  (项目中导入flexpaper文件,在显示页面指定路径即可显示)

<script type="text/javascript">
	                var fp = new FlexPaperViewer(  
	                         'hlbussiness/planbaseViewer/FlexPaperViewer',
	                         'viewerPlaceHolder', { config : {
	                         SwfFile : escape("${swfPath}"),	//swf文件路径
	                         Scale : 0.6,
	                         ZoomTransition : 'easeOut',
	                         ZoomTime : 0.5,
	                         ZoomInterval : 0.2,
	                         FitPageOnLoad : true,
	                         FitWidthOnLoad : true,	//适合初始页宽度大小的装载页
	                         FullScreenAsMaxWindow : true,
	                         ProgressiveLoading : false,
	                         MinZoomSize : 0.2,
	                         MaxZoomSize : 5,
	                         SearchMatchAll : false,
	                         InitViewMode : 'Portrait',
	                         PrintPaperAsBitmap : false,
	                         ViewModeToolsVisible : true,
	                         ZoomToolsVisible : true,
	                         NavToolsVisible : true,
	                         CursorToolsVisible : true,
	                         SearchToolsVisible : true,                        
	                         localeChain: 'zh_CN'
	                 }});  
	            </script>


剩下要做的,是将用户上传的文件转为 swf。 

package com.function.util;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.ConnectException;
import java.util.ResourceBundle;

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;

/**
 * 将文件转成swf格式
 * 
 * @author Administrator
 * 
 */
public class ConvertSwf {

	/**
	 * 入口方法-通过此方法转换文件至swf格式
	 * @param filePath	上传文件所在文件夹的绝对路径
	 * @param dirPath	文件夹名称
	 * @param fileName	文件名称
	 * @return			生成swf文件名
	 */
	public String beginConvert(String filePath, String dirName, String fileName) {
		final String DOC = ".doc";
		final String DOCX = ".docx";
		final String XLS = ".xls";
		final String XLSX = ".xlsx";
		final String PDF = ".pdf";
		final String SWF = ".swf";
		final String TOOL = "pdf2swf.exe";
		String outFile = "";
		String fileNameOnly = "";
		String fileExt = "";
		if (null != fileName && fileName.indexOf(".") > 0) {
			int index = fileName.indexOf(".");
			fileNameOnly = fileName.substring(0, index);
			fileExt = fileName.substring(index).toLowerCase();
		}
		String inputFile = filePath + File.separator + fileName;
		String outputFile = "";

		//如果是office文档,先转为pdf文件
		if (fileExt.equals(DOC) || fileExt.equals(DOCX) || fileExt.equals(XLS)
				|| fileExt.equals(XLSX)) {
			outputFile = filePath + File.separator + fileNameOnly + PDF;
			office2PDF(inputFile, outputFile);
			inputFile = outputFile;
			fileExt = PDF;
		}

		if (fileExt.equals(PDF)) {
			String toolFile = filePath + File.separator + TOOL;
			outputFile = filePath + File.separator + fileNameOnly + SWF;
			convertPdf2Swf(inputFile, outputFile, toolFile);
			outFile = outputFile;
		}
		return outFile;
	}

	/**
	 * 将pdf文件转换成swf文件
	 * @param sourceFile pdf文件绝对路径
	 * @param outFile	 swf文件绝对路径
	 * @param toolFile	 转换工具绝对路径
	 */
	private void convertPdf2Swf(String sourceFile, String outFile,
			String toolFile) {
		String command = toolFile + " \"" + sourceFile + "\" -o  \"" + outFile
				+ "\" -s flashversion=9 ";
		try {
			Process process = Runtime.getRuntime().exec(command);
			System.out.println(loadStream(process.getInputStream()));
			System.err.println(loadStream(process.getErrorStream()));
			System.out.println(loadStream(process.getInputStream()));
			System.out.println("###--Msg: swf 转换成功");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * office文档转pdf文件
	 * @param sourceFile	office文档绝对路径
	 * @param destFile		pdf文件绝对路径
	 * @return
	 */
	private int office2PDF(String sourceFile, String destFile) {
		ResourceBundle rb = ResourceBundle.getBundle("OpenOfficeService");
		String OpenOffice_HOME = rb.getString("OO_HOME");
		String host_Str = rb.getString("oo_host");
		String port_Str = rb.getString("oo_port");
		try {
			File inputFile = new File(sourceFile);
			if (!inputFile.exists()) {
				return -1; // 找不到源文件 
			}
			// 如果目标路径不存在, 则新建该路径  
			File outputFile = new File(destFile);
			if (!outputFile.getParentFile().exists()) {
				outputFile.getParentFile().mkdirs();
			}
			// 启动OpenOffice的服务  
			String command = OpenOffice_HOME
					+ "/program/soffice.exe -headless -accept=\"socket,host="
					+ host_Str + ",port=" + port_Str + ";urp;\"";
			System.out.println("###\n" + command);
			Process pro = Runtime.getRuntime().exec(command);
			// 连接openoffice服务
			OpenOfficeConnection connection = new SocketOpenOfficeConnection(
					host_Str, Integer.parseInt(port_Str));
			connection.connect();
			// 转换 
			DocumentConverter converter = new OpenOfficeDocumentConverter(
					connection);
			converter.convert(inputFile, outputFile);

			// 关闭连接和服务
			connection.disconnect();
			pro.destroy();

			return 0;
		} catch (FileNotFoundException e) {
			System.out.println("文件未找到!");
			e.printStackTrace();
			return -1;
		} catch (ConnectException e) {
			System.out.println("OpenOffice服务监听异常!");
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return 1;
	}
	
	static String loadStream(InputStream in) throws IOException{
		int ptr = 0;
		in = new BufferedInputStream(in);
		StringBuffer buffer = new StringBuffer();
		
		while ((ptr=in.read())!= -1){
			buffer.append((char)ptr);
		}
		return buffer.toString();
	}

}
这个类的运行的前提条件:

         1、安装openoffice,新建OpenOfficeService.properties文件,放到src目录下

OO_HOME = D:/Program Files/OpenOffice.org 3
oo_host = 127.0.0.1
oo_port =8100

         2、下载 jodconverter-2.2.2 ,将lib目录所有jar 复制到项目 lib目录
         3、 下载swftools-0.9.2.exe 安装后,复制安装目录中到 pdf2swf.exe 到项目附件目录中。



在action中

//转换上传文件格式为swf
		String outPath = new ConvertSwf().beginConvert(dirPath, dirName, fileName); 
		System.out.println("生成swf文件:" + outPath);

即可生成文件名与原文件名一直的swf文件。

  • 1
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 42
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 42
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值