欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java實現(xiàn)Word/Pdf/TXT轉(zhuǎn)html的示例

 更新時間:2020年11月06日 11:14:46   作者:zsq_fengchen  
這篇文章主要介紹了Java實現(xiàn)Word/Pdf/TXT轉(zhuǎn)html的示例,幫助大家方便的進行文件格式轉(zhuǎn)換,完成需求,感興趣的朋友可以了解下

引言:

    最近公司在做一個教育培訓(xùn)學(xué)習(xí)及在線考試的項目,本人主要從事網(wǎng)絡(luò)課程模塊,主要做課程分類,課程,課件的創(chuàng)建及在線學(xué)習(xí)和統(tǒng)計的功能,因為課件涉及到多種類型,像視頻,音頻,圖文,外部鏈接及文檔類型.其中就涉及到一個問題,就是文檔型課件課程在網(wǎng)頁上的展示和學(xué)習(xí)問題,因為要在線統(tǒng)計學(xué)習(xí)的課程,學(xué)習(xí)的人員,學(xué)習(xí)的時長,所以不能像傳統(tǒng)做法將文檔下載到本地學(xué)習(xí),那樣就不受系統(tǒng)控制了,所以最終的方案是,在上傳文檔型課件的時候,將其文件對應(yīng)的轉(zhuǎn)換成HTML文件,以便在網(wǎng)頁上能夠瀏覽學(xué)習(xí)

 下邊主要針對word,pdf和txt文本文件進行轉(zhuǎn)換

一:Java實現(xiàn)將word轉(zhuǎn)換為html

1:引入依賴

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

2:代碼demo

package com.svse.controller;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
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.PictureType;
import org.apache.poi.xwpf.converter.core.BasicURIResolver;
import org.apache.poi.xwpf.converter.core.FileImageExtractor;
import org.apache.poi.xwpf.converter.core.FileURIResolver;
import org.apache.poi.xwpf.converter.core.IURIResolver;
import org.apache.poi.xwpf.converter.core.IXWPFConverter;
import org.apache.poi.xwpf.converter.xhtml.XHTMLConverter;
import org.apache.poi.xwpf.converter.xhtml.XHTMLOptions;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
/**
 * word 轉(zhuǎn)換成html
 */
public class TestWordToHtml {

 public static final String STORAGEPATH="C://works//files//";
 public static final String IP="192.168.30.222";
 public static final String PORT="8010";
 public static void main(String[] args) throws IOException, TransformerException, ParserConfigurationException {
  TestWordToHtml wt=new TestWordToHtml();
  //wt.Word2003ToHtml("甲骨文考證.doc");
  wt.Word2007ToHtml("甲骨文考證.docx");

 }

  /**
  * 2003版本word轉(zhuǎn)換成html
  * @throws IOException
  * @throws TransformerException
  * @throws ParserConfigurationException
  */
 public void Word2003ToHtml(String fileName) throws IOException, TransformerException, ParserConfigurationException {

  final String imagepath = STORAGEPATH+"fileImage/";//解析時候如果doc文件中有圖片 圖片會保存在此路徑
  final String strRanString=getRandomNum();
  String filepath =STORAGEPATH;
  String htmlName =fileName.substring(0, fileName.indexOf("."))+ "2003.html";
  final String file = filepath + fileName;
  InputStream input = new FileInputStream(new File(file));
  HWPFDocument wordDocument = new HWPFDocument(input);
  WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());
  //設(shè)置圖片存放的位置
  wordToHtmlConverter.setPicturesManager(new PicturesManager() {
   public String savePicture(byte[] content, PictureType pictureType, String suggestedName, float widthInches, float heightInches) {
    File imgPath = new File(imagepath);
    if(!imgPath.exists()){//圖片目錄不存在則創(chuàng)建
     imgPath.mkdirs();
    }

    File file = new File(imagepath +strRanString+suggestedName);
    try {
     OutputStream os = new FileOutputStream(file);
     os.write(content);
     os.close();
    } catch (FileNotFoundException e) {
     e.printStackTrace();
    } catch (IOException e) {
     e.printStackTrace();
    }

    return "http://"+IP+":"+PORT+"http://uploadFile/fileImage/"+strRanString+suggestedName;
    // return imagepath +strRanString+suggestedName;
   }
  });

  //解析word文檔
  wordToHtmlConverter.processDocument(wordDocument);
  Document htmlDocument = wordToHtmlConverter.getDocument();

  File htmlFile = new File(filepath +strRanString+htmlName);
  OutputStream outStream = new FileOutputStream(htmlFile);


  DOMSource domSource = new DOMSource(htmlDocument);
  StreamResult streamResult = new StreamResult(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);
  outStream.close();

  System.out.println("生成html文件路徑:"+ "http://"+IP+":"+PORT+"http://uploadFile/"+strRanString+htmlName);
 }

 /**
  * 2007版本word轉(zhuǎn)換成html
  * @throws IOException
  */
 public void Word2007ToHtml(String fileName) throws IOException {

  final String strRanString=getRandomNum();

  String filepath = STORAGEPATH+strRanString;
  String htmlName =fileName.substring(0, fileName.indexOf("."))+ "2007.html";
  File f = new File(STORAGEPATH+fileName);
  if (!f.exists()) {
   System.out.println("Sorry File does not Exists!");
  } else {
   if (f.getName().endsWith(".docx") || f.getName().endsWith(".DOCX")) {
    try {
     // 1) 加載word文檔生成 XWPFDocument對象
     InputStream in = new FileInputStream(f);
     XWPFDocument document = new XWPFDocument(in);

     // 2) 解析 XHTML配置 (這里設(shè)置IURIResolver來設(shè)置圖片存放的目錄)
     File imageFolderFile = new File(filepath);
     XHTMLOptions options = XHTMLOptions.create().URIResolver(new FileURIResolver(imageFolderFile));
     options.setExtractor(new FileImageExtractor(imageFolderFile));
     options.URIResolver(new IURIResolver() {
      public String resolve(String uri) {
       //http://192.168.30.222:8010//uploadFile/....
       return "http://"+IP+":"+PORT+"http://uploadFile/"+strRanString +"/"+ uri;
      }
     });

     options.setIgnoreStylesIfUnused(false);
     options.setFragment(true);

     // 3) 將 XWPFDocument轉(zhuǎn)換成XHTML
     OutputStream out = new FileOutputStream(new File(filepath + htmlName));
     IXWPFConverter<XHTMLOptions> converter = XHTMLConverter.getInstance();
     converter.convert(document,out, options);
     //XHTMLConverter.getInstance().convert(document, out, options);
     System.out.println("html路徑:"+"http://"+IP+":"+PORT+"http://uploadFile/"+strRanString+htmlName);
    } catch (Exception e) {
     e.printStackTrace();
    }

   } else {
    System.out.println("Enter only MS Office 2007+ files");
   }
  }
 }

  /**
  *功能說明:生成時間戳
  *創(chuàng)建人:zsq
  *創(chuàng)建時間:2019年12月7日 下午2:37:09
  *
  */
  public static String getRandomNum(){
   Date dt = new Date();
   SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
   String str=sdf.format(dt);
   return str;
  }

 }

二:Java實現(xiàn)將Pdf轉(zhuǎn)換為html

1: 引入依賴

<dependency>
   <groupId>net.sf.cssbox</groupId>
   <artifactId>pdf2dom</artifactId>
   <version>1.7</version>
  </dependency>
  <dependency>
   <groupId>org.apache.pdfbox</groupId>
   <artifactId>pdfbox</artifactId>
   <version>2.0.12</version>
  </dependency>
  <dependency>
   <groupId>org.apache.pdfbox</groupId>
   <artifactId>pdfbox-tools</artifactId>
   <version>2.0.12</version>
 </dependency>

2:代碼Demo

 public class PdfToHtml {
 
 /*
  pdf轉(zhuǎn)換html
  */
  public void pdfToHtmlTest(String inPdfPath,String outputHtmlPath) {
   // String outputPath = "C:\\works\\files\\ZSQ保密知識測試題庫.html";
    //try() 寫在()里面會自動關(guān)閉流
   try{
    BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(outputHtmlPath)),"utf-8"));
    //加載PDF文檔
    //PDDocument document = PDDocument.load(bytes);
    PDDocument document = PDDocument.load(new File(inPdfPath));
    PDFDomTree pdfDomTree = new PDFDomTree();
    pdfDomTree.writeText(document,out);
   } catch (Exception e) {
    e.printStackTrace();
   }
  }
 
  public static void main(String[] args) throws IOException {
   PdfToHtml ph=new PdfToHtml();
   String pdfPath="C:\\works\\files\\武研中心行政考勤制度.pdf";
   String outputPath="C:\\works\\files\\武研中心行政考勤制度.html";
   ph.pdfToHtmlTest(pdfPath,outputPath);
 }
 
 }

三:Java實現(xiàn)將TXT轉(zhuǎn)換為html

/*
  * txt文檔轉(zhuǎn)html
  filePath:txt原文件路徑
  htmlPosition:轉(zhuǎn)化后生成的html路徑
 */
 public static void txtToHtml(String filePath, String htmlPosition) {
  try {
   //String encoding = "GBK";
   File file = new File(filePath);
   if (file.isFile() && file.exists()) { // 判斷文件是否存在
    InputStreamReader read = new InputStreamReader(new FileInputStream(file), "GBK");
    // 考慮到編碼格式
    BufferedReader bufferedReader = new BufferedReader(read);
    // 寫文件
    FileOutputStream fos = new FileOutputStream(new File(htmlPosition));
    OutputStreamWriter osw = new OutputStreamWriter(fos, "GBK");
    BufferedWriter bw = new BufferedWriter(osw);
    String lineTxt = null;
    while ((lineTxt = bufferedReader.readLine()) != null) {
     bw.write("&nbsp&nbsp&nbsp"+lineTxt + "</br>");
    }
    bw.close();
    osw.close();
    fos.close();
    read.close();
   } else {
    System.out.println("找不到指定的文件");
   }
  } catch (Exception e) {
   System.out.println("讀取文件內(nèi)容出錯");
   e.printStackTrace();
  }
 }

以上就是Java實現(xiàn)Word/Pdf/TXT轉(zhuǎn)html的示例的詳細(xì)內(nèi)容,更多關(guān)于Java Word/Pdf/TXT轉(zhuǎn)html的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Java內(nèi)存區(qū)域管理詳解

    Java內(nèi)存區(qū)域管理詳解

    這篇文章主要介紹了Java內(nèi)存區(qū)域管理詳解,文章通過圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-09-09
  • java中以DES的方式實現(xiàn)對稱加密并提供密鑰的實例

    java中以DES的方式實現(xiàn)對稱加密并提供密鑰的實例

    這篇文章主要介紹了java中以DES的方式實現(xiàn)對稱加密并提供密鑰的實例的相關(guān)資料,這里提供實例幫助大家學(xué)習(xí)理解這部分知識,需要的朋友可以參考下
    2017-08-08
  • SpringBoot之LogBack配置詳解

    SpringBoot之LogBack配置詳解

    這篇文章主要介紹了SpringBoot之LogBack配置詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-02-02
  • SpringBoot基于Redis的分布式鎖實現(xiàn)過程記錄

    SpringBoot基于Redis的分布式鎖實現(xiàn)過程記錄

    Redis是一套 key-value 高性能數(shù)據(jù)庫,使用它可以大大提高我們的開發(fā)效率,在SpringBoot中,自動配置也幫我們節(jié)約了大量的配置,下面這篇文章主要給大家介紹了關(guān)于SpringBoot基于Redis的分布式鎖實現(xiàn)的相關(guān)資料,需要的朋友可以參考下
    2022-01-01
  • Java多線程下載文件實現(xiàn)案例詳解

    Java多線程下載文件實現(xiàn)案例詳解

    這篇文章主要介紹了Java多線程下載文件實現(xiàn)案例詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-05-05
  • java List中相同的數(shù)據(jù)合并到一起

    java List中相同的數(shù)據(jù)合并到一起

    這篇文章主要介紹了java List中相同的數(shù)據(jù)合并到一起,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-07-07
  • 解決response.setHeader設(shè)置下載文件名無效的問題

    解決response.setHeader設(shè)置下載文件名無效的問題

    這篇文章主要介紹了解決response.setHeader設(shè)置下載文件名無效的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • Spring Security實現(xiàn)基于角色的訪問控制框架

    Spring Security實現(xiàn)基于角色的訪問控制框架

    Spring Security是一個功能強大的安全框架,提供了基于角色的訪問控制、身份驗證、授權(quán)等安全功能,可輕松保護Web應(yīng)用程序的安全,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2023-04-04
  • IDEA創(chuàng)建SpringBoot項目整合mybatis時mysql-connector-java報錯異常的詳細(xì)分析

    IDEA創(chuàng)建SpringBoot項目整合mybatis時mysql-connector-java報錯異常的詳細(xì)分析

    最近工作中發(fā)現(xiàn)了個錯誤,分享給同樣遇到這個問題的朋友,這篇文章主要給大家介紹了關(guān)于IDEA創(chuàng)建SpringBoot項目整合mybatis時mysql-connector-j報錯異常的詳細(xì)分析,需要的朋友可以參考下
    2023-02-02
  • java eclipse 出現(xiàn) xxx cannot be resolved to a type 錯誤解決方法

    java eclipse 出現(xiàn) xxx cannot be resolved to a type 錯誤解決方法

    這篇文章主要介紹了java eclipse 出現(xiàn) xxx cannot be resolved to a type 錯誤解決方法的相關(guān)資料,需要的朋友可以參考下
    2017-03-03

最新評論