引言:

最近公司在做一个教育培训学习及在线考试的项目,本人主要从事网络课程模块,主要做课程分类,课程,课件的创建及在线学习和统计的功能,因为课件涉及到多种类型,像视频,音频,图文,外部链接及文档类型.其中就涉及到一个问题,就是文档型课件课程在网页上的展示和学习问题,因为要在线统计学习的课程,学习的人员,学习的时长,所以不能像传统做法将文档下载到本地学习,那样就不受系统控制了,所以最终的方案是,在上传文档型课件的时候,将其文件对应的转换成HTML文件,以便在网页上能够浏览学习

下边主要针对word,pdf和txt文本文件进行转换

一:Java实现将word转换为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 转换成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转换成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());
//设置图片存放的位置
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()){//图片目录不存在则创建
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+"//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+"//uploadFile/"+strRanString+htmlName);
} /**
* 2007版本word转换成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配置 (这里设置IURIResolver来设置图片存放的目录)
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+"//uploadFile/"+strRanString +"/"+ uri;
}
}); options.setIgnoreStylesIfUnused(false);
options.setFragment(true); // 3) 将 XWPFDocument转换成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+"//uploadFile/"+strRanString+htmlName);
} catch (Exception e) {
e.printStackTrace();
} } else {
System.out.println("Enter only MS Office 2007+ files");
}
}
} /**
*功能说明:生成时间戳
*创建人:zsq
*创建时间: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实现将Pdf转换为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转换html
*/
public void pdfToHtmlTest(String inPdfPath,String outputHtmlPath) {
// String outputPath = "C:\\works\\files\\ZSQ保密知识测试题库.html";
//try() 写在()里面会自动关闭流
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实现将TXT转换为html

  /*
* txt文档转html
filePath:txt原文件路径
htmlPosition:转化后生成的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("读取文件内容出错");
e.printStackTrace();
}
}

Java实现Word/Pdf/TXT转html的更多相关文章

  1. java 抽取 word,pdf 的四种武器

    转自:https://www.ibm.com/developerworks/cn/java/l-java-tips/     感谢作者发布的文章 用 jacob 其实 jacob 是一个 bridag ...

  2. 搜索引擎Solr6.2.1 索引富文本(word/pdf/txt/html)

    一:首先建立Core 在core下面新建lib文件夹,存放相关的jar包,如图所示: lib文件夹打开所示,这些类库在solr6.2.1解压之后都能找到: 修改solrconfig.xml,把刚刚建的 ...

  3. word和.txt文件转html 及pdf文件, 使用poi jsoup itext心得

    word和.txt文件转html 及pdf文件, 使用poi jsoup  itext心得本人第一次写博客,有上面不足的或者需要改正的希望大家指出来,一起学习交流讨论.由于在项目中遇到了这一个问题,在 ...

  4. java操作word,excel,pdf

    在平常应用程序中,对office和pdf文档进行读取数据是比较常见的功能,尤其在很多web应用程序中.所以今天我们就简单来看一下java对word.excel.pdf文件的读取.本篇博客只是讲解简单应 ...

  5. iOS 应用中加载文档pdf/word/txt

    一.加载PDF文档:应用内打开文档.手机中其他应用打开文档 Demo 首先拖入一个文档pdf.word.txt,打开不同的文档知识 文件名字.类型修改即可 #import "ReadView ...

  6. java操作office和pdf文件java读取word,excel和pdf文档内容

    在平常应用程序中,对office和pdf文档进行读取数据是比较常见的功能,尤其在很多web应用程序中.所以今天我们就简单来看一下Java对word.excel.pdf文件的读取.本篇博客只是讲解简单应 ...

  7. c#上传文件并将word pdf转化成txt存储并将内容写入数据库

    c#上传文件并将word pdf转化成txt存储并将内容写入数据库 using System; using System.Data; using System.Configuration; using ...

  8. 利用aspose-words 实现 java中word转pdf文件

    利用aspose-words  实现 java中word转pdf文件 首先下载aspose-words-15.8.0-jdk16.jar包 引入jar包,编写Java代码 package test; ...

  9. java 实现word 转 pdf

    java 实现word  转 pdf 不知道网上为啥道友们写的这么复杂  ,自己看到过一篇还不错的  , 自己动手改了改 ,测试一下可以用  , 希望大家可以参考一下 , 对大家有帮助 1.引入jar ...

随机推荐

  1. centos6离线安装apache2.4

    在/usr/local中新建文件夹 Apache2.4,将httpd安装包放入该文件夹, 解压:tar xvf httpd-2.4.41.tar.gz 进入httpd-2.4.41文件夹中,对Apac ...

  2. linux基础之CentOS启动流程

    一.基本概念 内核设计流派: 单内核设计:Linux //所有功能集成于同一个程序 微内核设计:Windows,Solaris //每种功能使用一个单独子系统实现 Linux内核特点: 支持模块化:. ...

  3. Codeforce 230A - Dragons (sort)

    Kirito is stuck on a level of the MMORPG he is playing now. To move on in the game, he's got to defe ...

  4. [CF527D] Clique Problem - 贪心

    数轴上有n 个点,第i 个点的坐标为xi,权值为wi.两个点i,j之间存在一条边当且仅当 abs(xi-xj)>=wi+wj. 你需要求出这张图的最大团的点数. Solution 把每个点看作以 ...

  5. Wannafly Camp 2020 Day 6F 图与三角形 - 图论

    把黑边视为无边,那么答案之和每个点的度数有关 #include <bits/stdc++.h> using namespace std; #define int long long int ...

  6. linux - mysql:启动 mysql

    启动mysql 第一种: /etc/rc.d/init.d/mysqld start /etc/rc.d/init.d/mysqld stop 第二种:使用service 启动.关闭MySQL服务 s ...

  7. ActiveMQ分布式事务

    一.安装ActiveMQ 1.拷贝apache-activemq-5.14.4-bin.tar.gz到Linux服务器的/opt下 2.解压缩 tar -zxvf apache-activemq-5. ...

  8. JavaScript的严格检查模式

    JavaScript的严格检查模式 前提:IDEA设置为ECMAScript 6语法. 'use strict':严格检查模式,用来预防JS的随意性导致的问题. 比如:直接 i=1;这样定义成了全局变 ...

  9. Java 中的 匿名类

    什么是内部类? 在一个类中定义另一个类,这样定义的类称为内部类.包含内部类的类称为内部类的外部类. 如果想要通过一个类来使用另一个类,可以定义为内部类. 内部类的外部类的成员变量在内部类仍然有效,内部 ...

  10. zabbix4.2配置监控华为路由器:基于ENSP模拟器

    一.基于ENSP模拟器的华为路由器 这里是华为模拟器中的设备,并不是真机,所以要先保证华为模拟器中的网络设备可以和物理主机.虚拟机能通信,这是前提.如何保证通信请看之前的文章:https://www. ...