java word转html 空指针_Java word转html

importorg.apache.logging.log4j.LogManager;importorg.apache.logging.log4j.Logger;importorg.apache.poi.hwpf.HWPFDocument;importorg.apache.poi.hwpf.converter.PicturesManager;importorg.apache.poi.hwpf.converter.WordToHtmlConverter;importorg.apache.poi.hwpf.usermodel.PictureType;importorg.apache.poi.xwpf.converter.core.BasicURIResolver;importorg.apache.poi.xwpf.converter.core.FileImageExtractor;importorg.apache.poi.xwpf.converter.xhtml.XHTMLConverter;importorg.apache.poi.xwpf.converter.xhtml.XHTMLOptions;importorg.apache.poi.xwpf.usermodel.XWPFDocument;importorg.springframework.stereotype.Controller;importorg.springframework.web.bind.annotation.RequestMapping;importorg.w3c.dom.Document;importjavax.xml.parsers.DocumentBuilderFactory;importjavax.xml.parsers.ParserConfigurationException;importjavax.xml.transform.OutputKeys;importjavax.xml.transform.Transformer;importjavax.xml.transform.TransformerException;importjavax.xml.transform.TransformerFactory;importjavax.xml.transform.dom.DOMSource;importjavax.xml.transform.stream.StreamResult;import java.io.*;

@Controller

@RequestMapping("/manual/")public classManualController {private static final Logger logger = LogManager.getLogger(ManualController.class);/*** 将word2003转换为html文件

*

*@paramwordPath word文件路径

*@paramwordName word文件名称无后缀

*@paramsuffix word文件后缀

*@paramhtmlPath html存储地址

*@throwsIOException

*@throwsTransformerException

*@throwsParserConfigurationException*/

public staticString Word2003ToHtml(String wordPath, String wordName, String suffix, String htmlPath)throwsIOException, TransformerException, ParserConfigurationException {

String htmlName= wordName + ".html";final String imagePath = htmlPath + "image" +File.separator;//判断html文件是否存在

File htmlFile = new File(htmlPath +htmlName);if(htmlFile.exists()) {returnhtmlFile.getAbsolutePath();

}//原word文档

final String file = wordPath + File.separator + wordName +suffix;

InputStream input= new FileInputStream(newFile(file));

HWPFDocument wordDocument= newHWPFDocument(input);

WordToHtmlConverter wordToHtmlConverter= newWordToHtmlConverter(

DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());//设置图片存放的位置

wordToHtmlConverter.setPicturesManager(newPicturesManager() {public String savePicture(byte[] content, PictureType pictureType, String suggestedName, floatwidthInches,floatheightInches) {

File imgPath= newFile(imagePath);if (!imgPath.exists()) {//图片目录不存在则创建

imgPath.mkdirs();

}

File file= new File(imagePath +suggestedName);try{

OutputStream os= newFileOutputStream(file);

os.write(content);

os.close();

}catch(FileNotFoundException e) {

e.printStackTrace();

}catch(IOException e) {

e.printStackTrace();

}//图片在html文件上的路径 相对路径

return "image/" +suggestedName;

}

});//解析word文档

wordToHtmlConverter.processDocument(wordDocument);

Document htmlDocument=wordToHtmlConverter.getDocument();//生成html文件上级文件夹

File folder = newFile(htmlPath);if (!folder.exists()) {

folder.mkdirs();

}

OutputStream outStream= newFileOutputStream(htmlFile);

DOMSource domSource= newDOMSource(htmlDocument);

StreamResult streamResult= newStreamResult(outStream);

TransformerFactory factory=TransformerFactory.newInstance();

Transformer serializer=factory.newTransformer();

serializer.setOutputProperty(OutputKeys.ENCODING,"utf-8");

serializer.setOutputProperty(OutputKeys.INDENT,"yes");

serializer.setOutputProperty(OutputKeys.METHOD,"html");

serializer.transform(domSource, streamResult);returnhtmlFile.getAbsolutePath();

}/***

* 2007版本word转换成html

*

*@paramwordPath word文件路径

*@paramwordName word文件名称无后缀

*@paramsuffix word文件后缀

*@paramhtmlPath html存储地址

*@return*@throwsIOException*/

public staticString Word2007ToHtml(String wordPath, String wordName, String suffix, String htmlPath)throwsIOException {

String htmlName= wordName + ".html";

String imagePath= htmlPath + "image" +File.separator;//判断html文件是否存在

File htmlFile = new File(htmlPath +htmlName);if(htmlFile.exists()) {returnhtmlFile.getAbsolutePath();

}//word文件

File wordFile = new File(wordPath + File.separator + wordName +suffix);//1) 加载word文档生成 XWPFDocument对象

InputStream in = newFileInputStream(wordFile);

XWPFDocument document= newXWPFDocument(in);//2) 解析 XHTML配置 (这里设置IURIResolver来设置图片存放的目录)

File imgFolder = newFile(imagePath);

XHTMLOptions options=XHTMLOptions.create();

options.setExtractor(newFileImageExtractor(imgFolder));//html中图片的路径 相对路径

options.URIResolver(new BasicURIResolver("image"));

options.setIgnoreStylesIfUnused(false);

options.setFragment(true);//3) 将 XWPFDocument转换成XHTML//生成html文件上级文件夹

File folder = newFile(htmlPath);if (!folder.exists()) {

folder.mkdirs();

}

OutputStream out= newFileOutputStream(htmlFile);

XHTMLConverter.getInstance().convert(document, out, options);returnhtmlFile.getAbsolutePath();

}public static voidmain(String[] args) {try{

Word2007ToHtml("D:\\Ning\\word2html\\", "33", ".docx", "D://Ning//word2html/");

}catch(Exception e) {

e.printStackTrace();

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值