利用Java如何实现将其他文件转换成html文件

  介绍

利用Java如何实现将其他文件转换成html文件?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

<强>一:Java实现将词转换为html

<强> 1:引入依赖

& lt; dependency>   & lt; groupId> fr.opensagres.xdocreport   & lt; artifactId> fr.opensagres.xdocreport.document   & lt; version> 1.0.5   & lt;/dependency>   & lt; dependency>   & lt; groupId> fr.opensagres.xdocreport   & lt; artifactId> org.apache.poi.xwpf.converter.xhtml   & lt; version> 1.0.5   & lt;/dependency>   & lt; dependency>   & lt; groupId> org.apache.poi   & lt; artifactId> poi   & lt; version> 3.12 & lt;/version>   & lt;/dependency>   & lt; dependency>   & lt; groupId> org.apache.poi   & lt; artifactId> poi-scratchpad   & lt; version> 3.12 & lt;/version>   & lt;/dependency>

<强> 2:代码演示

包com.svse.controller;
  
  进口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.core.IURIResolver;
  进口org.apache.poi.xwpf.converter.core.IXWPFConverter;
  进口org.apache.poi.xwpf.converter.xhtml.XHTMLConverter;
  进口org.apache.poi.xwpf.converter.xhtml.XHTMLOptions;
  进口org.apache.poi.xwpf.usermodel.XWPFDocument;/* *
  *字转换成html
  */公开课TestWordToHtml {
  
  公共静态最终字符串STORAGEPATH=癈://////文件“工作;;
  公共静态最终字符串IP=?92.168.30.222";
  公共静态最终字符串端口=?010”;
  公共静态void main (String [] args)抛出IOException, TransformerException ParserConfigurationException {
  TestWordToHtml wt=new TestWordToHtml ();//wt.Word2003ToHtml(“甲骨文考证.doc");
  wt.Word2007ToHtml(“甲骨文考证.docx");
  
  }/* *
  * 2003版转本词换成html
  * @throws IOException
  * @throws TransformerException
  * @throws ParserConfigurationException
  */公共空间Word2003ToHtml(文件名字符串)抛出IOException TransformerException ParserConfigurationException {
  
  最终字符串imagepath=STORAGEPATH +“fileImage/?//解析时候如果医生文件中有图片图片会保存在此路径
  最终字符串strRanString=getRandomNum ();
  字符串filepath=STORAGEPATH;
  字符串htmlName=文件名。substring (0, fileName.indexOf (“!”)) +“2003. html";
  最终字符串文件=filepath +文件名;
  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 + strRanString + suggestedName);
  尝试{
  OutputStream os=new FileOutputStream(文件);
  os.write(内容);
  os.close ();
  }捕捉(FileNotFoundException e) {
  e.printStackTrace ();
  }捕捉(IOException e) {
  e.printStackTrace ();
  }
  
  返回“http://" + IP +“:“+ +“港//还是fileImage/? strRanString + suggestedName;//返回imagepath + strRanString + suggestedName;
  }
  });//解析字文档
  wordToHtmlConverter.processDocument (wordDocument);
  文档htmlDocument=wordToHtmlConverter.getDocument ();
  
  文件htmlFile=新文件(filepath + strRanString + htmlName);
  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。缩进,“yes");
  serializer.setOutputProperty (OutputKeys。方法,“html");
  
  序列化器。变换(domSource streamResult);
  outStream.close ();
  
  System.out.println(“生成html文件路径:“+“http://" + IP +“:“+ +“港//还是“+ strRanString + htmlName);
  }/* *
  * 2007版转本词换成html
  * @throws IOException
  */公共空间Word2007ToHtml(字符串文件名)抛出IOException {
  
  最终字符串strRanString=getRandomNum ();
  
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null

利用Java如何实现将其他文件转换成html文件