SpringBoot集成tika實(shí)現(xiàn)word轉(zhuǎn)html的操作代碼
1.什么是tika?
Tika是一個(gè)內(nèi)容分析工具,自帶全面的parser工具類,能解析基本所有常見格式的文件,得到文件的metadata,content等內(nèi)容,返回格式化信息??偟膩碚f可以作為一個(gè)通用的解析工具。特別對(duì)于搜索引擎的數(shù)據(jù)抓去和處理步驟有重要意義。Tika是Apache的Lucene項(xiàng)目下面的子項(xiàng)目,在lucene的應(yīng)用中可以使用tika獲取大批量文檔中的內(nèi)容來建立索引,非常方便,也很容易使用。Apache Tika toolkit可以自動(dòng)檢測各種文檔(如word,ppt,xml,csv,ppt等)的類型并抽取文檔的元數(shù)據(jù)和文本內(nèi)容。Tika集成了現(xiàn)有的文檔解析庫,并提供統(tǒng)一的接口,使針對(duì)不同類型的文檔進(jìn)行解析變得更簡單。Tika針對(duì)搜索引擎索引、內(nèi)容分析、轉(zhuǎn)化等非常有用。
Tika架構(gòu)
應(yīng)用程序員可以很容易地在他們的應(yīng)用程序集成Tika。Tika提供了一個(gè)命令行界面和圖形用戶界面,使它比較人性化。在本章中,我們將討論構(gòu)成Tika架構(gòu)的四個(gè)重要模塊。下圖顯示了Tika的四個(gè)模塊的體系結(jié)構(gòu):
- 語言檢測機(jī)制。
- MIME檢測機(jī)制。
- Parser接口。
- Tika Facade 類.
語言檢測機(jī)制
每當(dāng)一個(gè)文本文件被傳遞到Tika,它將檢測在其中的語言。它接受沒有語言的注釋文件和通過檢測該語言添加在該文件的元數(shù)據(jù)信息。支持語言識(shí)別,Tika 有一類叫做語言標(biāo)識(shí)符在包org.apache.tika.language及語言識(shí)別資料庫里面包含了語言檢測從給定文本的算法。Tika 內(nèi)部使用N-gram算法語言檢測。
MIME檢測機(jī)制
Tika可以根據(jù)MIME標(biāo)準(zhǔn)檢測文檔類型。Tika默認(rèn)MIME類型檢測是使用org.apache.tika.mime.mimeTypes。它使用org.apache.tika.detect.Detector 接口大部分內(nèi)容類型檢測。內(nèi)部Tika使用多種技術(shù),如文件匹配替換,內(nèi)容類型提示,魔術(shù)字節(jié),字符編碼,以及其他一些技術(shù)。
解析器接口
org.apache.tika.parser 解析器接口是Tika解析文檔的主要接口。該接口從提取文檔中的文本和元數(shù)據(jù),并總結(jié)了其對(duì)外部用戶愿意寫解析器插件。采用不同的具體解析器類,具體為各個(gè)文檔類型,Tika 支持大量的文件格式。這些格式的具體類不同的文件格式提供支持,無論是通過直接實(shí)現(xiàn)邏輯分析器或使用外部解析器庫。
Tika Facade 類
使用的Tika facade類是從Java調(diào)用Tika的最簡單和直接的方式,而且也沿用了外觀的設(shè)計(jì)模式。可以在 Tika API的org.apache.tika包Tika 找到外觀facade類。通過實(shí)現(xiàn)基本用例,Tika作為facade的代理。它抽象了的Tika庫的底層復(fù)雜性,例如MIME檢測機(jī)制,解析器接口和語言檢測機(jī)制,并提供給用戶一個(gè)簡單的接口來使用。
2.代碼工程
實(shí)驗(yàn)?zāi)繕?biāo)
實(shí)現(xiàn)word文檔轉(zhuǎn)html
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>springboot-demo</artifactId> <groupId>com.et</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>tika</artifactId> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.tika</groupId> <artifactId>tika-parsers</artifactId> <version>1.17</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependencies> </project>
controller
package com.et.tika.controller; import com.et.tika.convertor.WordToHtmlConverter; import com.et.tika.dto.ConvertedDocumentDTO; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import java.util.HashMap; import java.util.Map; @RestController @Slf4j public class HelloWorldController { @RequestMapping("/hello") public Map<String, Object> showHelloWorld(){ Map<String, Object> map = new HashMap<>(); map.put("msg", "HelloWorld"); return map; } @Autowired WordToHtmlConverter converter; /** * Transforms the Word document into HTML document and returns the transformed document. * * @return The content of the uploaded document as HTML. */ @RequestMapping(value = "/api/word-to-html", method = RequestMethod.POST) public ConvertedDocumentDTO convertWordDocumentIntoHtmlDocument(@RequestParam(value = "file", required = true) MultipartFile wordDocument) { log.info("Converting word document into HTML document"); ConvertedDocumentDTO htmlDocument = converter.convertWordDocumentIntoHtml(wordDocument); log.info("Converted word document into HTML document."); log.trace("The created HTML markup looks as follows: {}", htmlDocument); return htmlDocument; } }
WordToHtmlConverter
package com.et.tika.convertor; import com.et.tika.dto.ConvertedDocumentDTO; import com.et.tika.exception.DocumentConversionException; import lombok.extern.slf4j.Slf4j; import org.apache.tika.exception.TikaException; import org.apache.tika.metadata.Metadata; import org.apache.tika.parser.ParseContext; import org.apache.tika.parser.Parser; import org.apache.tika.parser.microsoft.ooxml.OOXMLParser; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import org.springframework.web.multipart.MultipartFile; import org.xml.sax.SAXException; import javax.xml.transform.OutputKeys; import javax.xml.transform.TransformerException; import javax.xml.transform.sax.SAXTransformerFactory; import javax.xml.transform.sax.TransformerHandler; import javax.xml.transform.stream.StreamResult; import java.io.IOException; import java.io.InputStream; import java.io.StringWriter; /** * */ @Component @Slf4j public class WordToHtmlConverter { /** * Converts a .docx document into HTML markup. This code * is based on <a rel="external nofollow" >this StackOverflow</a> answer. * * @param wordDocument The converted .docx document. * @return */ public ConvertedDocumentDTO convertWordDocumentIntoHtml(MultipartFile wordDocument) { log.info("Converting word document: {} into HTML", wordDocument.getOriginalFilename()); try { InputStream input = wordDocument.getInputStream(); Parser parser = new OOXMLParser(); StringWriter sw = new StringWriter(); SAXTransformerFactory factory = (SAXTransformerFactory) SAXTransformerFactory.newInstance(); TransformerHandler handler = factory.newTransformerHandler(); handler.getTransformer().setOutputProperty(OutputKeys.ENCODING, "utf-8"); handler.getTransformer().setOutputProperty(OutputKeys.METHOD, "html"); handler.getTransformer().setOutputProperty(OutputKeys.INDENT, "yes"); handler.setResult(new StreamResult(sw)); Metadata metadata = new Metadata(); metadata.add(Metadata.CONTENT_TYPE, "text/html;charset=utf-8"); parser.parse(input, handler, metadata, new ParseContext()); return new ConvertedDocumentDTO(wordDocument.getOriginalFilename(), sw.toString()); } catch (IOException | SAXException | TransformerException | TikaException ex) { log.error("Conversion failed because an exception was thrown", ex); throw new DocumentConversionException(ex.getMessage(), ex); } } }
dto
package com.et.tika.dto; import org.apache.commons.lang.builder.ToStringBuilder; /** * */ public class ConvertedDocumentDTO { private final String contentAsHtml; private final String filename; public ConvertedDocumentDTO(String filename, String contentAsHtml) { this.contentAsHtml = contentAsHtml; this.filename = filename; } public String getContentAsHtml() { return contentAsHtml; } public String getFilename() { return filename; } @Override public String toString() { return new ToStringBuilder(this) .append("filename", this.filename) .append("contentAsHtml", this.contentAsHtml) .toString(); } }
自定義異常
package com.et.tika.exception; /** * */ public final class DocumentConversionException extends RuntimeException { public DocumentConversionException(String message, Exception ex) { super(message, ex); } }
以上只是一些關(guān)鍵代碼,所有代碼請參見下面代碼倉庫
代碼倉庫
https://github.com/Harries/springboot-demo
3.測試
啟動(dòng)Spring Boot應(yīng)用
測試word轉(zhuǎn)html
4.引用
以上就是SpringBoot集成tika實(shí)現(xiàn)word轉(zhuǎn)html的操作代碼的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot tika實(shí)現(xiàn)word轉(zhuǎn)html的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Java 8 Stream Api 中的 map和 flatMap 操作方法
Java 8提供了非常好用的 Stream API ,可以很方便的操作集合。今天通過這篇文章給大家分享Java 8 Stream Api 中的 map和 flatMap 操作方法,需要的朋友可以參考下2019-11-11Redis使用RedisTemplate模板類的常用操作方式
這篇文章主要介紹了Redis使用RedisTemplate模板類的常用操作方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09使用Apache Spark進(jìn)行Java數(shù)據(jù)分析的步驟詳解
今天我們將探討如何使用Apache Spark進(jìn)行Java數(shù)據(jù)分析,Apache Spark是一個(gè)強(qiáng)大的大數(shù)據(jù)處理引擎,它支持批處理和流處理,特別適合處理大規(guī)模數(shù)據(jù)集,在Java中使用Spark,我們可以利用其強(qiáng)大的數(shù)據(jù)處理能力來進(jìn)行各種數(shù)據(jù)分析任務(wù),需要的朋友可以參考下2024-07-07從0開始教你開發(fā)一個(gè)springboot應(yīng)用
這篇文章主要為大家介紹了從0開始開發(fā)一個(gè)springboot應(yīng)用教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05Spring Security實(shí)現(xiàn)5次密碼錯(cuò)誤觸發(fā)賬號(hào)自動(dòng)鎖定功能
在現(xiàn)代互聯(lián)網(wǎng)應(yīng)用中,賬號(hào)安全是重中之重,然而,暴力 破解攻擊依然是最常見的安全威脅之一,攻擊者通過自動(dòng)化腳本嘗試大量的用戶名和密碼組合,試圖找到漏洞進(jìn)入系統(tǒng),所以為了解決這一問題,賬號(hào)鎖定機(jī)制被廣泛應(yīng)用,本文介紹了Spring Security實(shí)現(xiàn)5次密碼錯(cuò)誤觸發(fā)賬號(hào)鎖定功能2024-12-12spring如何實(shí)現(xiàn)依賴注入DI(spring-test方式)
本文主要介紹如何實(shí)現(xiàn)spring 的依賴注入,并且淺顯的講述一下注入需要注意的事項(xiàng)。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03