利用OpenOffice实现word文档在线预览

项目中遇到的word文档在线预览需求,查阅很多资料决定利用openoffice转换word文档为pdf/html进行预览实现。

1.下载openoffice4安装 www.openoffice.org;

2.导入相关jar包


		    org.artofsolving.jodconverter
		    jodconverter-core
		    3.0-beta-3
		
	  	
		    com.artofsolving
		    jodconverter
		    2.2.1
		    
		    	
		    		commons-io
		    		commons-io
		    	
		    
	  	

3.openoffice服务启动

安装OpenOffice成功后,您可以进入/program/目录并运行以下命令启动OpenOffice服务:

soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard

4.openoffice转换文档代码,这是一个工具类,将要预览的文档进行转换

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

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.StreamOpenOfficeDocumentConverter;

public class Doc2HtmlUtil {
	
	
    /**
     * 转换文件成pdf
     * 
     * @param fromFileInputStream:
     * @throws IOException 
     */
    public static String file2pdf(InputStream fromFileInputStream, String toFilePath,String type) throws Exception{
        String timesuffix = IDUtil.getID();
        String docFileName = null;
        String htmFileName = null;
        if("doc".equals(type)){
            docFileName = timesuffix.concat(".doc");
            htmFileName = timesuffix.concat(".pdf");
        }else if("xls".equals(type)){
            docFileName = timesuffix.concat(".xls");
            htmFileName = timesuffix.concat(".pdf");
        }else if("ppt".equals(type)){
            docFileName = timesuffix.concat(".ppt");
            htmFileName = timesuffix.concat(".pdf");
        }else if("txt".equals(type)){
            docFileName = timesuffix.concat(".txt");
            htmFileName = timesuffix.concat(".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();

        docInputFile.createNewFile();
        
        /**
         * 由fromFileInputStream构建输入文件
         */
        int bytesRead = 0;
        byte[] buffer = new byte[1024 * 8];
    	OutputStream os = new FileOutputStream(docInputFile);
		while ((bytesRead = fromFileInputStream.read(buffer)) != -1) {
		    os.write(buffer, 0, bytesRead);
		}
		os.close();
        fromFileInputStream.close();
        
        OpenOfficeConnection connection = new SocketOpenOfficeConnection(FSUtil.FS_FILE_CONVERT_HOST,8100);
		connection.connect();
        // convert
        DocumentConverter converter = new StreamOpenOfficeDocumentConverter(connection);
        converter.convert(docInputFile, htmlOutputFile);
        connection.disconnect();
        // 转换完之后删除word文件
        docInputFile.delete();
        return htmFileName;
    }
    /**
     * 文件转换成Html
     * @param fromFileInputStream
     * @param toFilePath
     * @param type
     * @return
     * @throws Exception
     */
    public static String file2Html (InputStream fromFileInputStream, String toFilePath,String type) throws Exception{
		String timesuffix = IDUtil.getID();
        String docFileName = null;
        String htmFileName = null;
        if("doc".equals(type)){
            docFileName = timesuffix.concat(".doc");
            htmFileName = timesuffix.concat(".html");
        }else if("xls".equals(type)){
            docFileName = timesuffix.concat(".xls");
            htmFileName = timesuffix.concat(".html");
        }else if("ppt".equals(type)){
            docFileName = timesuffix.concat(".ppt");
            htmFileName = timesuffix.concat(".html");
        }else if("txt".equals(type)){
            docFileName = timesuffix.concat(".txt");
            htmFileName = timesuffix.concat(".html");
        }else if("pdf".equals(type)){
        	docFileName = timesuffix.concat(".pdf");
			htmFileName = timesuffix.concat(".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();
        docInputFile.createNewFile();
        /**
         * 由fromFileInputStream构建输入文件
         */
        int bytesRead = 0;
        byte[] buffer = new byte[1024 * 8];
    	OutputStream os = new FileOutputStream(docInputFile);
		while ((bytesRead = fromFileInputStream.read(buffer)) != -1) {
		    os.write(buffer, 0, bytesRead);
		}
		os.close();
        fromFileInputStream.close();
        OpenOfficeConnection connection = new SocketOpenOfficeConnection(FSUtil.FS_FILE_CONVERT_HOST,8100);
		connection.connect();
        // convert
        DocumentConverter converter = new StreamOpenOfficeDocumentConverter(connection);
        converter.convert(docInputFile, htmlOutputFile);
        connection.disconnect();
        // 转换完之后删除word文件
        docInputFile.delete();
        return htmFileName;
	}	
}

5.action类调用转换工具类进行转换

//转换为pdf
@SuppressWarnings("deprecation")
	private String convertToPDF(HttpServletRequest request, String fileName, InputStream fromFileInputStream, String currentId) {
		if(FSUtil.isOfficeFile(fileName.toLowerCase())){
			String convertFileSavePath=request.getRealPath("/")+FSUtil.FS_HADOOP_FILE_CONVERT_PATH+File.separatorChar+currentId;
			File file=new File(convertFileSavePath);
			if(!file.exists()){
				file.mkdirs();
			}
			String type=fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
			try {
				if(type.endsWith("x")){
					return Doc2HtmlUtil.file2pdf(fromFileInputStream, convertFileSavePath, type.substring(0, type.length()-1));
				}else{
					return Doc2HtmlUtil.file2pdf(fromFileInputStream, convertFileSavePath, type);
				}
			} catch (Exception e) {
				log.error(e.getMessage());
				return null;
			}
		}
		return null;
	}
//转换为html
	@SuppressWarnings("deprecation")
	public String convertToHtml(HttpServletRequest request,String fileName, InputStream fromFileInputStream, String currentId){
		if(!FSUtil.isImage(fileName.toLowerCase())){
			String convertFileSavePath=request.getRealPath("/")+FSUtil.FS_HADOOP_FILE_CONVERT_PATH+File.separatorChar+currentId;
			File file=new File(convertFileSavePath);
			if(!file.exists()){
				file.mkdirs();
			}
			String type=fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
			try {
				if(type.endsWith("x")){
					return Doc2HtmlUtil.file2Html(fromFileInputStream, convertFileSavePath, type.substring(0, type.length()-1));
				}else{
					return Doc2HtmlUtil.file2Html(fromFileInputStream, convertFileSavePath, type);
				}
			} catch (Exception e) {
				log.error(e.getMessage());
				return null;
			}
		}
		return null;
	}

convertFileSavePath为转换后的PDF格式文档绝对地址返回给前端利用浏览器打开是先预览





你可能感兴趣的:(java,openoffice)