JAVA:借用OpenOffice将上传的Word文档转换成Html格式(二)

2014-11-24 03:07:52 · 作者: · 浏览: 4
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标记

*

* @param htmlStr

* 带有复杂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("", "

");

}

// 调整图片地址

htmlStr = htmlStr.replaceAll("

+ "/");

// 把

转换成
保留样式

// content = content.replaceAll("(]*>.* )(<\\/P>)",

// "");

// 把

转换成