java word转换成html格式_JAVA:借用OpenOffice将上传的Word文档转换成Html格式

为什么会想起来将上传的word文档转换成html格式呢?设想,如果一个系统需要发布在页面的文章都是来自word文档,一般会执行下面的流程:使用word打开文档,Ctrl+A,进入发布文章页面,Ctrl+V。看起来也不麻烦,但是,如果文档中包含大量图片呢?尴尬的事是图片都需要重新上传吧?

如果可以将已经编写好的word文档上传到服务器就可以在相应页面进行展示,将会是一件非常惬意的事情,最起码信息发布人员会很开心。程序员可能就不会这么想了,囧。

将Word转Html的原理是这样的:

1、客户上传Word文档到服务器

2、服务器调用OpenOffice程序打开上传的Word文档

3、OpenOffice将Word文档另存为Html格式

4、Over

至此可见,这要求服务器端安装OpenOffice软件,其实也可以是MS Office,不过OpenOffice的优势是跨平台,你懂的。恩,说明一下,本文的测试基于 MS Win7 Ultimate X64 系统。

下面就是规规矩矩的实现。

3、泡杯热茶,等待下载。

4、安装OpenOffice,安装结束后,调用cmd,启动OpenOffice的一项服务:C:\Program Files (x86)\OpenOffice.org 3\program>soffice -headless -accept="socket,port=8100;urp;"

e01d4df9f65199493bfaa512db33158d.png

5、打开eclipse

6、喝杯热茶,等待eclipse打开。

7、新建eclipse项目,导入Jodconverter/lib 下得jar包。

8、Coding...

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png查看代码

package com.mzule.doc2html.util;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.InputStreamReader;

import java.net.ConnectException;

import java.util.Date;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

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;

/*** 将Word文档转换成html字符串的工具类

*

*@authorMZULE

**/

public class Doc2Html {

public static void main(String[] args) {

System.out

.println(toHtmlString(new File("C:/test/test.doc"), "C:/test"));

}

/*** 将word文档转换成html文档

*

*@paramdocFile

* 需要转换的word文档

*@paramfilepath

* 转换之后html的存放路径

*@return转换之后的html文件*/

public static File convert(File docFile, String filepath) {

//创建保存html的文件 File htmlFile = new File(filepath + "/" + new Date().getTime()

+ ".html");

//创建Openoffice连接 OpenOfficeConnection con = new SocketOpenOfficeConnection(8100);

try {

//连接 con.connect();

} catch (ConnectException e) {

System.out.println("获取OpenOffice连接失败...");

e.printStackTrace();

}

//创建转换器 DocumentConverter converter = new OpenOfficeDocumentConverter(con);

//转换文档问html converter.convert(docFile, htmlFile);

//关闭openoffice连接 con.disconnect();

return htmlFile;

}

/*** 将word转换成html文件,并且获取html文件代码。

*

*@paramdocFile

* 需要转换的文档

*@paramfilepath

* 文档中图片的保存位置

*@return转换成功的html代码*/

public static String toHtmlString(File docFile, String filepath) {

//转换word文档 File htmlFile = convert(docFile, filepath);

//获取html文件流 StringBuffer htmlSb = new StringBuffer();

try {

BufferedReader br = new BufferedReader(new InputStreamReader(

new FileInputStream(htmlFile)));

while (br.ready()) {

htmlSb.append(br.readLine());

}

br.close();

//删除临时文件 htmlFile.delete();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

//HTML文件字符串 String htmlStr = htmlSb.toString();

//返回经过清洁的html文本 return clearFormat(htmlStr, filepath);

}

/*** 清除一些不需要的html标记

*

*@paramhtmlStr

* 带有复杂html标记的html语句

*@return去除了不需要html标记的语句*/

protected static String clearFormat(String htmlStr, String docImgPath) {

//获取body内容的正则 String bodyReg = "

";

Pattern bodyPattern = Pattern.compile(bodyReg);

Matcher bodyMatcher = bodyPattern.matcher(htmlStr);

if (bodyMatcher.find()) {

//获取BODY内容,并转化BODY标签为DIV htmlStr = bodyMatcher.group().replaceFirst("

.replaceAll("", "

");
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值