Tika是一個(gè)內(nèi)容分析工具,自帶全面的parser工具類,能解析基本所有常見格式的文件,得到文件的metadata,content等內(nèi)容,返回格式化信息。總的來說可以作為一個(gè)通用的解析工具。特別對于搜索引擎的數(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)一的接口,使針對不同類型的文檔進(jìn)行解析變得更簡單。Tika針對搜索引擎索引、內(nèi)容分析、轉(zhuǎn)化等非常有用。
應(yīng)用程序員可以很容易地在他們的應(yīng)用程序集成Tika。Tika提供了一個(gè)命令行界面和圖形用戶界面,使它比較人性化。在本章中,我們將討論構(gòu)成Tika架構(gòu)的四個(gè)重要模塊。下圖顯示了Tika的四個(gè)模塊的體系結(jié)構(gòu):
每當(dāng)一個(gè)文本文件被傳遞到Tika,它將檢測在其中的語言。它接受沒有語言的注釋文件和通過檢測該語言添加在該文件的元數(shù)據(jù)信息。支持語言識別,Tika 有一類叫做語言標(biāo)識符在包org.apache.tika.language及語言識別資料庫里面包含了語言檢測從給定文本的算法。Tika 內(nèi)部使用N-gram算法語言檢測。
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é)了其對外部用戶愿意寫解析器插件。采用不同的具體解析器類,具體為各個(gè)文檔類型,Tika 支持大量的文件格式。這些格式的具體類不同的文件格式提供支持,無論是通過直接實(shí)現(xiàn)邏輯分析器或使用外部解析器庫。
使用的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è)簡單的接口來使用。
實(shí)現(xiàn)word文檔轉(zhuǎn)html
<?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>
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;
}
}
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 href="http://stackoverflow.com/a/9053258/313554">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);
}
}
}
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)鍵代碼,所有代碼請參見下面代碼倉庫
啟動(dòng)Spring Boot應(yīng)用
者: 阿寶哥
轉(zhuǎn)發(fā)鏈接:https://mp.weixin.qq.com/s/1ztZLSCEhBpBuTEdqJSS2w
x0文件頭部內(nèi)容
1、----------------------設(shè)置頁面標(biāo)題<title>
2、----------------------設(shè)置基底網(wǎng)址<base>
3、----------------------設(shè)置基準(zhǔn)文字<basefont>
4、----------------------定義元信息<meta>
5、----------------------設(shè)置頁面關(guān)鍵字<keywords>
6、----------------------設(shè)置頁面過期時(shí)間<expires>
0x02
設(shè)置標(biāo)題<title>
實(shí)例代碼:
<html>
<head>
<title>請?jiān)谶@里輸入標(biāo)題</title>
</head>
<body>
請看標(biāo)題欄
</body>
</html>
設(shè)置基底網(wǎng)址<base>
<html>
<head>
<!--href是連接地址;target是頁面顯示的目標(biāo)窗口-->
<base target="_self">
</head>
<body>
<A href="">點(diǎn)擊</A>
</body>
</html>
點(diǎn)擊藍(lán)色字體“點(diǎn)擊”,直接跳到網(wǎng)頁上面。
設(shè)置基準(zhǔn)文字<basefont>
<html>
<head>
<!--face屬性用于設(shè)置文字名稱 size字體大小 color字體顏色 -->
<basefont face="宋體" size="h2" color="#666666">
</head>
<body>
<A href="">點(diǎn)擊</A>HHHHHHHHH
</body>
</html>
由于顏色不明顯就不截圖了。。。哈哈
定義元信息<meta>
<meta http-equiv=" " name=" " content=" ">
<meta> 元素可提供有關(guān)頁面的元信息(meta-information),比如針對搜索引擎和更新頻度的描述和關(guān)鍵詞。
<meta> 標(biāo)簽位于文檔的頭部,不包含任何內(nèi)容。<meta> 標(biāo)簽的屬性定義了與文檔相關(guān)聯(lián)的名稱/值對。
name 屬性
提供了名稱/值對中的名稱。HTML 和 XHTML 標(biāo)簽都沒有指定任何預(yù)先定義的 <meta> 名稱。通常情況下,您可以自由使用對自己和源文檔的讀者來說富有意義的名稱。
類似這樣的 meta 標(biāo)簽可能對于進(jìn)入搜索引擎的索引有幫助:
<meta name="keywords" content="HTML,ASP,PHP,SQL">
如果沒有提供 name 屬性,那么名稱/值對中的名稱會采用 http-equiv 屬性的值。
http-equiv 屬性
http-equiv 屬性為名稱/值對提供了名稱。并指示服務(wù)器在發(fā)送實(shí)際的文檔之前先在要傳送給瀏覽器的 MIME 文檔頭部包含名稱/值對。
當(dāng)服務(wù)器向?yàn)g覽器發(fā)送文檔時(shí),會先發(fā)送許多名稱/值對。雖然有些服務(wù)器會發(fā)送許多這種名稱/值對,但是所有服務(wù)器都至少要發(fā)送一個(gè):content-type:text/html。這將告訴瀏覽器準(zhǔn)備接受一個(gè) HTML 文檔。
使用帶有 http-equiv 屬性的 <meta> 標(biāo)簽時(shí),服務(wù)器將把名稱/值對添加到發(fā)送給瀏覽器的內(nèi)容頭部。例如,添加:
<meta http-equiv="charset" content="iso-8859-1"> <meta http-equiv="expires" content="31 Dec 2008">
設(shè)置頁面關(guān)鍵字<keywords>/設(shè)置頁面過期時(shí)間<expires>
"keywords" 是一個(gè)經(jīng)常被用到的名稱。它為文檔定義了一組關(guān)鍵字。某些搜索引擎在遇到這些關(guān)鍵字時(shí),會用這些關(guān)鍵字對文檔進(jìn)行分類
"expires"用于設(shè)計(jì)頁面過期時(shí)間,content屬性設(shè)置具體過期時(shí)間。
<html>
<head>
<title>設(shè)置頁面時(shí)間過期時(shí)間</title>
<meta http-equiv=" expires" content="FRI,1 JUN 2007 00 00 00 GMT" charset="UTF-8">
</head>
<body>
</body>
</html>
0X03body內(nèi)容
設(shè)置頁面背景-------------------bgcolor
設(shè)置頁面邊距-------------------topmargin leftmargin rightmargin bottomnargin
設(shè)計(jì)正文顏色-------------------text
bgcolor
<html>
<head>
<title>設(shè)置頁面時(shí)間過期時(shí)間</title>
<meta http-equiv=" expires" content="FRI,1 JUN 2007 00 00 00 GMT" charset="UTF-8">
</head>
<body bgcolor="red">
</body>
</html>
顯示情況:
topmargin:顯示內(nèi)容和瀏覽器頂部的距離
leftmargin :顯示內(nèi)容和瀏覽器左邊的距離
rightmargin:顯示內(nèi)容和瀏覽器右邊的距離
bottomnargin:顯示內(nèi)容和瀏覽器底部的距離
<body text="">字體顏色
<html>
<head>
<title>設(shè)置頁面時(shí)間過期時(shí)間</title>
<meta http-equiv=" expires" content="FRI,1 JUN 2007 00 00 00 GMT" charset="UTF-8">
</head>
<body text="blue" bgcolor="red" topmargin=100 leftmargin=20 rightmargin=20 bottomnargin=180>
</body>
</html>
例子:
認(rèn)識各個(gè)html標(biāo)簽的作用,有助于web滲透。。。下個(gè)文章看看文字和段落。
*請認(rèn)真填寫需求信息,我們會在24小時(shí)內(nèi)與您取得聯(lián)系。