Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537
Tika是一個內容分析工具,自帶全面的parser工具類,能解析基本所有常見格式的文件,得到文件的metadata,content等內容,返回格式化信息。總的來說可以作為一個通用的解析工具。特別對于搜索引擎的數據抓去和處理步驟有重要意義。Tika是Apache的Lucene項目下面的子項目,在lucene的應用中可以使用tika獲取大批量文檔中的內容來建立索引,非常方便,也很容易使用。Apache Tika toolkit可以自動檢測各種文檔(如word,ppt,xml,csv,ppt等)的類型并抽取文檔的元數據和文本內容。Tika集成了現有的文檔解析庫,并提供統一的接口,使針對不同類型的文檔進行解析變得更簡單。Tika針對搜索引擎索引、內容分析、轉化等非常有用。
應用程序員可以很容易地在他們的應用程序集成Tika。Tika提供了一個命令行界面和圖形用戶界面,使它比較人性化。在本章中,我們將討論構成Tika架構的四個重要模塊。下圖顯示了Tika的四個模塊的體系結構:
每當一個文本文件被傳遞到Tika,它將檢測在其中的語言。它接受沒有語言的注釋文件和通過檢測該語言添加在該文件的元數據信息。支持語言識別,Tika 有一類叫做語言標識符在包org.apache.tika.language及語言識別資料庫里面包含了語言檢測從給定文本的算法。Tika 內部使用N-gram算法語言檢測。
Tika可以根據MIME標準檢測文檔類型。Tika默認MIME類型檢測是使用org.apache.tika.mime.mimeTypes。它使用org.apache.tika.detect.Detector 接口大部分內容類型檢測。內部Tika使用多種技術,如文件匹配替換,內容類型提示,魔術字節,字符編碼,以及其他一些技術。
org.apache.tika.parser 解析器接口是Tika解析文檔的主要接口。該接口從提取文檔中的文本和元數據,并總結了其對外部用戶愿意寫解析器插件。采用不同的具體解析器類,具體為各個文檔類型,Tika 支持大量的文件格式。這些格式的具體類不同的文件格式提供支持,無論是通過直接實現邏輯分析器或使用外部解析器庫。
使用的Tika facade類是從Java調用Tika的最簡單和直接的方式,而且也沿用了外觀的設計模式。可以在 Tika API的org.apache.tika包Tika 找到外觀facade類。通過實現基本用例,Tika作為facade的代理。它抽象了的Tika庫的底層復雜性,例如MIME檢測機制,解析器接口和語言檢測機制,并提供給用戶一個簡單的接口來使用。
實現word文檔轉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);
}
}
以上只是一些關鍵代碼,所有代碼請參見下面代碼倉庫
啟動Spring Boot應用
者: 阿寶哥
轉發鏈接:https://mp.weixin.qq.com/s/1ztZLSCEhBpBuTEdqJSS2w
x0文件頭部內容
1、----------------------設置頁面標題<title>
2、----------------------設置基底網址<base>
3、----------------------設置基準文字<basefont>
4、----------------------定義元信息<meta>
5、----------------------設置頁面關鍵字<keywords>
6、----------------------設置頁面過期時間<expires>
0x02
設置標題<title>
實例代碼:
<html>
<head>
<title>請在這里輸入標題</title>
</head>
<body>
請看標題欄
</body>
</html>
設置基底網址<base>
<html>
<head>
<!--href是連接地址;target是頁面顯示的目標窗口-->
<base target="_self">
</head>
<body>
<A href="">點擊</A>
</body>
</html>
點擊藍色字體“點擊”,直接跳到網頁上面。
設置基準文字<basefont>
<html>
<head>
<!--face屬性用于設置文字名稱 size字體大小 color字體顏色 -->
<basefont face="宋體" size="h2" color="#666666">
</head>
<body>
<A href="">點擊</A>HHHHHHHHH
</body>
</html>
由于顏色不明顯就不截圖了。。。哈哈
定義元信息<meta>
<meta http-equiv=" " name=" " content=" ">
<meta> 元素可提供有關頁面的元信息(meta-information),比如針對搜索引擎和更新頻度的描述和關鍵詞。
<meta> 標簽位于文檔的頭部,不包含任何內容。<meta> 標簽的屬性定義了與文檔相關聯的名稱/值對。
name 屬性
提供了名稱/值對中的名稱。HTML 和 XHTML 標簽都沒有指定任何預先定義的 <meta> 名稱。通常情況下,您可以自由使用對自己和源文檔的讀者來說富有意義的名稱。
類似這樣的 meta 標簽可能對于進入搜索引擎的索引有幫助:
<meta name="keywords" content="HTML,ASP,PHP,SQL">
如果沒有提供 name 屬性,那么名稱/值對中的名稱會采用 http-equiv 屬性的值。
http-equiv 屬性
http-equiv 屬性為名稱/值對提供了名稱。并指示服務器在發送實際的文檔之前先在要傳送給瀏覽器的 MIME 文檔頭部包含名稱/值對。
當服務器向瀏覽器發送文檔時,會先發送許多名稱/值對。雖然有些服務器會發送許多這種名稱/值對,但是所有服務器都至少要發送一個:content-type:text/html。這將告訴瀏覽器準備接受一個 HTML 文檔。
使用帶有 http-equiv 屬性的 <meta> 標簽時,服務器將把名稱/值對添加到發送給瀏覽器的內容頭部。例如,添加:
<meta http-equiv="charset" content="iso-8859-1"> <meta http-equiv="expires" content="31 Dec 2008">
設置頁面關鍵字<keywords>/設置頁面過期時間<expires>
"keywords" 是一個經常被用到的名稱。它為文檔定義了一組關鍵字。某些搜索引擎在遇到這些關鍵字時,會用這些關鍵字對文檔進行分類
"expires"用于設計頁面過期時間,content屬性設置具體過期時間。
<html>
<head>
<title>設置頁面時間過期時間</title>
<meta http-equiv=" expires" content="FRI,1 JUN 2007 00 00 00 GMT" charset="UTF-8">
</head>
<body>
</body>
</html>
0X03body內容
設置頁面背景-------------------bgcolor
設置頁面邊距-------------------topmargin leftmargin rightmargin bottomnargin
設計正文顏色-------------------text
bgcolor
<html>
<head>
<title>設置頁面時間過期時間</title>
<meta http-equiv=" expires" content="FRI,1 JUN 2007 00 00 00 GMT" charset="UTF-8">
</head>
<body bgcolor="red">
</body>
</html>
顯示情況:
topmargin:顯示內容和瀏覽器頂部的距離
leftmargin :顯示內容和瀏覽器左邊的距離
rightmargin:顯示內容和瀏覽器右邊的距離
bottomnargin:顯示內容和瀏覽器底部的距離
<body text="">字體顏色
<html>
<head>
<title>設置頁面時間過期時間</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>
例子:
認識各個html標簽的作用,有助于web滲透。。。下個文章看看文字和段落。
*請認真填寫需求信息,我們會在24小時內與您取得聯系。