需求:将本地上传的word文档解析并放入数据库中

代码:

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.converter.PicturesManager;
import org.apache.poi.hwpf.converter.WordToHtmlConverter;
import org.apache.poi.hwpf.usermodel.Picture;
import org.apache.poi.hwpf.usermodel.PictureType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.w3c.dom.Document;

import com.google.common.io.Files;
import com.zhaozhi.writing.service.service.FileService;
import com.zhaozhi.writing.service.service.WordParseService;
import com.zhaozhi.writing.service.util.FileUtil;

@Service
public class WordParseServiceImpl implements WordParseService {

@Autowired
private FileService fileService;

@Override
public String docToHtmlResult(MultipartFile file) throws Exception {
HWPFDocument wordDocument = new HWPFDocument(file.getInputStream());

Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(document);
long currentTime = System.currentTimeMillis();
//设置图片URL
wordToHtmlConverter.setPicturesManager( new PicturesManager()
{
public String savePicture( byte[] content,
PictureType pictureType, String suggestedName,
float widthInches, float heightInches )
{
return FileUtil.OSS_DOMAIN+"/"+currentTime+"_"+suggestedName;
}
} );
// save pictures
List<Picture> pics = wordDocument.getPicturesTable().getAllPictures();
if (pics != null) {
for (int i = 0; i < pics.size(); i++) {
Picture pic = (Picture) pics.get(i);
System.out.println();
try {
String path = System.getProperty("java.io.tmpdir")+"/"+currentTime+"_"+pic.suggestFullFileName();
File picFile = new File(path);
Files.write(pic.getContent(),picFile );
fileService.upload(picFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

wordToHtmlConverter.processDocument(wordDocument);
Document htmlDocument = wordToHtmlConverter.getDocument();
ByteArrayOutputStream out = new ByteArrayOutputStream();
DOMSource domSource = new DOMSource(htmlDocument);
StreamResult streamResult = new StreamResult(out);

TransformerFactory tf = TransformerFactory.newInstance();
Transformer serializer = tf.newTransformer();
serializer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
serializer.setOutputProperty(OutputKeys.METHOD, "html");
serializer.transform(domSource, streamResult);
out.close();

return new String(out.toByteArray());
}

}

通过poi包中的WordToHtmlConverter类实现word与html的转换;

关于图片的解析思路:先将word文档中的图片抽取出来,把图片流生成到tomcat的临时文件中(详见代码):

String path = System.getProperty("java.io.tmpdir")+"/"+currentTime+"_"+pic.suggestFullFileName();
File picFile = new File(path);
Files.write(pic.getContent(),picFile );

再将临时文件上传到阿里云服务器上生成图片url,最后将url地址放到html中进行展示即可;

poi解析word文档转换成html(包括图片解析)的更多相关文章

  1. JAVA:借用OpenOffice将上传的Word文档转换成Html格式

    为什么会想起来将上传的word文档转换成html格式呢?设想,如果一个系统需要发布在页面的文章都是来自word文档,一般会执行下面的流程:使用word打开文档,Ctrl+A,进入发布文章页面,Ctrl ...

  2. OpenOffice Word文档转换成Html格式

    为什么会想起来将上传的word文档转换成html格式呢?设想,如果一个系统需要发布在页面的文章都是来自word文档,一般会执行下面的流程:使用word打开文档,Ctrl+A,进入发布文章页面,Ctrl ...

  3. POI 生成 word 文档 简单版(包括文字、表格、图片、字体样式设置等)

      POI 生成word 文档 一般有两种方法: ① word模板 生成word 文档 : ② 写代码直接生成 word 文档: 我这里演示的是第二种方法,即写代码生成 word文档,不多说废话,直接 ...

  4. Python将word文档转换成PDF文件

    如题. 代码: ''' #將word文档转换为pdf文件 #用到的库是pywin32 #思路上是调用了windows和office功能 ''' #导入所需库 from win32com.client ...

  5. Java利用aspose-words将word文档转换成pdf(破解 无水印)

    首先下载aspose-words-15.8.0-jdk16.jar包 http://pan.baidu.com/s/1nvbJwnv 引入jar包,编写Java代码 package doc; impo ...

  6. ASP.NET将word文档转换成pdf的代码

    一.添加引用 using Microsoft.Office.Interop.Word; 二.转换方法 1.方法 C# 代码 /// <summary> /// 把Word文件转换成pdf文 ...

  7. Java实现批量将word文档转换成PDF

    先导入words的jar包 需要jar包的私聊我发你 代码如下:import com.aspose.words.Document;import java.io.File; public class W ...

  8. C# word文档转换成PDF格式文档

    最近用到一个功能word转pdf,有个方法不错,挺方便的,直接调用即可,记录下 方法:ConvertWordToPdf(string sourcePath, string targetPath) so ...

  9. word ppt excel文档转换成pdf

    1.把word文档转换成pdf (1).添加引用 using Microsoft.Office.Interop.Word; 添加引用 (2).转换方法 /// <summary> /// ...

随机推荐

  1. java反射(三)--反射与操作类

    一.反射与操作类 在反射机制的处理过程之中不仅仅只是一个实例化对象的处理操作,更多的情况下还有类的组成的操作,任何一个类的基本组成结构:父类(父接口),包,属性,方法(构造方法,普通方法)--获取类的 ...

  2. if语句基本练习需求

    1.需求:键盘录入一个成绩,判断并输出成绩的等级. 90-100 优 80-89 良好 70-79 中等 60-69 及格 0-59 不及格 import java.util.Scanner; cla ...

  3. vue-router(路由)详细教程

    vue-router(路由)详细教程:https://blog.csdn.net/wulala_hei/article/details/80488727 vue路由组件传参-页面通信:https:// ...

  4. HDU多校训练第一场 1012 Sequence

    题目链接:acm.hdu.edu.cn/showproblem.php?pid=6589 题意:给出一个长度为n的数组,有m次操作,操作有3种1,2,3,问操作m次后的数组,输出i*a[i]的异或和 ...

  5. chrome浏览器屏蔽广告插件小例子

    1.创建一个文件夹,名为"清除页面广告插件" 2.在文件夹内创建"manifest.json"文件, { "name": "第一个 ...

  6. 在eclipse中添加svn插件

    1.点击菜单栏中的help选项,然后选择Install New Software,然后点击ADD,输入: name:subclipse     url:http://subclipse.tigris. ...

  7. [javascript模块化]require.js简单使用

    1.javascript模块规范 CommonJS 主要用于服务器端编程,比如node.js的模块系统,就是参照CommonJS规范实现的.在CommonJS中,有一个全局性方法require(),用 ...

  8. 小鱼易连 for mac如何使用?小鱼易连 mac版使用教程

    小鱼易连 for mac如何使用?小鱼易连 mac版是参加远程会议人士的首选,高效极致,简单流畅,视频流畅,语音清晰,无需专用网络的功能深受用户的喜欢,它提供的文件和电脑的共享,让你的会议更加高效.下 ...

  9. 【leetcode】1002. Find Common Characters

    题目如下: Given an array A of strings made only from lowercase letters, return a list of all characters ...

  10. SQL Server 2014 各版本介绍

    SQL Server 2014 各版本介绍 目前,SQL Server 2014 分为主要版本和专业版. 在选择版本的时候可以根据您具体的需要进行抉择,如果你需要一个免费的数据库管理系统,那么就选择 ...