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

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

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

2.导入相关jar包

<dependency>
		    <groupId>org.artofsolving.jodconverter</groupId>
		    <artifactId>jodconverter-core</artifactId>
		    <version>3.0-beta-3</version>
		</dependency>
	  	<dependency>
		    <groupId>com.artofsolving</groupId>
		    <artifactId>jodconverter</artifactId>
		    <version>2.2.1</version>
		    <exclusions>
		    	<exclusion>
		    		<artifactId>commons-io</artifactId>
		    		<groupId>commons-io</groupId>
		    	</exclusion>
		    </exclusions>
	  	</dependency>

3.openoffice服务启动

安装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格式文档绝对地址返回给前端利用浏览器打开是先预览





  • 2
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值