在网页中在线查看文档(doc、docx 、xls 、xlsx、 pdf 、swf )

步骤一:(涉及到的工具)

             在线查看使用FlexPaper软件(http://flexpaper.devaldi.com/download/)

            doc、docs、xls、xlsx转换dpf工具openoffice(http://www.openoffice.org/download/index.html)

            pdf转成swf工具swftools-0.9.2(http://www.pc6.com/softview/SoftView_51612.html)


步骤二:(配置工程)

             1、在项目中导入下载FlexPaper解压后文件(js、css、swf文件等)

              2、编写一个html页面

          

 
 
	
     
                 
         
         
		
		
		
		 
        
     
     
    	

打开html页面就可以运行,效果如下:

  在网页中在线查看文档(doc、docx 、xls 、xlsx、 pdf 、swf )_第1张图片


步骤三:pdf转换成swf格式(借鉴别人)

               1、下载 jodconverter-2.2.2 ,将lib目录所有jar 复制到项目 lib目录

               2、写一个java工具类(doc、dcox、xls、xlsx->pdf->swf)

                  

package com.netsix.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 fileName  文件名称 
	 * @return          生成swf文件名 
	 */  
	public String beginConvert(String filePath, 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.lastIndexOf(".") > 0) {  
			int index = fileName.lastIndexOf(".");  
			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 = "D:\\Program Files\\OpenOffice 4";  //openoffice安装路径 
		String host_Str = "127.0.0.1";   //地址
		String port_Str = "8100";        //端口号
		try {  
			File inputFile = new File(sourceFile);  
			if (!inputFile.exists()) {  
				return -1; // 找不到源文件   
			}  
			// 如果目标路径不存在, 则新建该路径    
			File outputFile = new File(destFile);  
			if (!outputFile.getParentFile().exists()) {  
				outputFile.getParentFile().mkdirs();  
			}  
			// 如果从文件中读取的URL地址最后一个字符不是 '\',则添加'\'  
            if (OpenOffice_HOME.charAt(OpenOffice_HOME.length() - 1) != '\\') {  
                OpenOffice_HOME += "\\";  
            }  
			// 启动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();  
	}  

}  

测试类

package com.netsix.test;

import com.netsix.util.ConvertSwf;

public class MainClass {

	public static void main(String[] args) {
		
		String dirPath = "D:\\workspace\\20140403_WebScaner\\pdf";  // 下载swftools-0.9.2.exe 安装后,复制安装目录中 pdf2swf.exe 到项目附件目录中。
		String fileName = "lucene4.6.doc";
		
		String outPath = new ConvertSwf().beginConvert(dirPath,fileName);   
		System.out.println("生成swf文件:" + outPath);  
		
	}
}

demo例子:http://download.csdn.net/detail/lin062854/7145653


你可能感兴趣的:(网页在线查看文件)