java实现词文件转html文件

  

最近在项目开发中用户提出要在电脑上没有装办公室时在浏览器中打开词文件,最后确定的逻辑:用户选择想要查看的文件,页面js判断文件是否为词。不是执行下载,是后端根据词文件后缀访问对应转换方法。文件已存在对应的html文件直接返回html文件地址,不存在先生成对应的html文件再返回地址. js直接通过开放()打开新的页签,展示词文件内容。新人一枚,如果代码中存在错误或有更好的实现万望指正!

  

相关的jar包

  

癹ava实现词文件转html文件"

  

代码         进口java.io.ByteArrayOutputStream;   进口java.io.File;   进口java.io.FileInputStream;   进口java.io.FileNotFoundException;   进口java.io.FileOutputStream;   进口java.io.IOException;   进口java.io.InputStream;   进口java.io.OutputStream;      进口javax.xml.parsers.DocumentBuilderFactory;   进口javax.xml.parsers.ParserConfigurationException;   进口javax.xml.transform.OutputKeys;   进口javax.xml.transform.Transformer;   进口javax.xml.transform.TransformerException;   进口javax.xml.transform.TransformerFactory;   进口javax.xml.transform.dom.DOMSource;   进口javax.xml.transform.stream.StreamResult;      进口org.apache.poi.hwpf.HWPFDocument;   进口org.apache.poi.hwpf.converter.PicturesManager;   进口org.apache.poi.hwpf.converter.WordToHtmlConverter;   进口org.apache.poi.hwpf.usermodel.PictureType;   进口org.apache.poi.xwpf.converter.core.BasicURIResolver;   进口org.apache.poi.xwpf.converter.core.FileImageExtractor;   进口org.apache.poi.xwpf.converter.core.FileURIResolver;   进口org.apache.poi.xwpf.converter.xhtml.XHTMLConverter;   进口org.apache.poi.xwpf.converter.xhtml.XHTMLOptions;   进口org.apache.poi.xwpf.usermodel.XWPFDocument;   进口org.w3c.dom.Document;/* *   *字转换成html 2017-2-27   */公开课WordToHtml {/* *   *将word2003转换为html文件2017-2-27   * @param wordPath词文件路径   * @param wordName词文件名称无后缀   * @param后缀词文件后缀   * @throws IOException   * @throws TransformerException   * @throws ParserConfigurationException   */wordName wordPath Word2003ToHtml公共字符串(字符串,字符串,字符串的后缀)抛出IOException, TransformerException ParserConfigurationException {   字符串htmlPath=wordPath +文件。分离器+ wordName +“_show”+ File.separator;   字符串htmlName=wordName +“html”;   最终字符串imagePath=htmlPath + + File.separator“形象”;//判断html文件是否存在   文件htmlFile=新文件(htmlPath + htmlName);   如果(htmlFile.exists ()) {   返回htmlFile.getAbsolutePath ();   }//原词文档   最终字符串文件=wordPath +文件。分离器+ wordName +后缀;   InputStream输入=new FileInputStream(新文件(文件);      HWPFDocument wordDocument=new HWPFDocument(输入);   .newDocumentBuilder WordToHtmlConverter WordToHtmlConverter=new WordToHtmlConverter (DocumentBuilderFactory.newInstance () () .newDocument ());//设置图片存放的位置   wordToHtmlConverter。setPicturesManager(新PicturesManager () {   公共字符串savePicture (byte[]内容,PictureType PictureType,字符串suggestedName widthInches浮动,浮动heightInches) {   文件imgPath=新文件(imagePath);   如果(! imgPath.exists()){//图片目录不存在则创建   imgPath.mkdirs ();   }   文件文件=新文件(imagePath + suggestedName);   尝试{   OutputStream os=new FileOutputStream(文件);   os.write(内容);   os.close ();   }捕捉(FileNotFoundException e) {   e.printStackTrace ();   }捕捉(IOException e) {   e.printStackTrace ();   }//图片在html文件上的路径相对路径   返回“图像/? suggestedName;   }   });//解析字文档   wordToHtmlConverter.processDocument (wordDocument);   文档htmlDocument=wordToHtmlConverter.getDocument ();//生成html文件上级文件夹   文件夹=新文件(htmlPath);   如果(! folder.exists ()) {   folder.mkdirs ();   }//生成html文件地址   OutputStream outStream=new FileOutputStream (htmlFile);      DOMSource DOMSource=new DOMSource (htmlDocument);   StreamResult StreamResult=new StreamResult (outStream);      TransformerFactory工厂=TransformerFactory.newInstance ();   变压器序列化器=factory.newTransformer ();   serializer.setOutputProperty (OutputKeys。编码,“utf - 8”);   serializer.setOutputProperty (OutputKeys。缩进:“是的”);   serializer.setOutputProperty (OutputKeys。方法,“html”);      序列化器。变换(domSource streamResult);      outStream.close ();      返回htmlFile.getAbsolutePath ();   }/* *   2017-2-27 * 2007版转本词换成html   * @param wordPath词文件路径   * @param wordName词文件名称无后缀   * @param后缀词文件后缀   * @return   * @throws IOException   */wordName wordPath Word2007ToHtml公共字符串(字符串,字符串,字符串的后缀)抛出IOException {   字符串htmlPath=wordPath +文件。分离器+ wordName +“_show”+ File.separator;   字符串htmlName=wordName +“html”;   字符串imagePath=htmlPath + + File.separator“形象”;//判断html文件是否存在   文件htmlFile=新文件(htmlPath + htmlName);   如果(htmlFile.exists ()) {   返回htmlFile.getAbsolutePath ();   }//字文件   文件wordFile=新文件(wordPath +文件。分离器+ wordName +后缀);//1)加载词文档生成XWPFDocument对象   InputStream=新FileInputStream (wordFile);   XWPFDocument文档=new XWPFDocument(的);//2)解析XHTML配置(这里设置IURIResolver来设置图片存放的目录)   文件imgFolder=新文件(imagePath);   XHTMLOptions选项=XHTMLOptions.create ();   选项。setExtractor(新FileImageExtractor (imgFolder));//html中图片的路径相对路径   选项。URIResolver(新BasicURIResolver(“图像”);   options.setIgnoreStylesIfUnused(假);   options.setFragment(真正的);//3)将XWPFDocument转换成XHTML//生成html文件上级文件夹   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

java实现词文件转html文件