Spring Boot + Apache Tika 實現文檔內容解析
一、Tika簡介
Apache Tika
核心功能
- ?文檔類型識別?:通過文件頭字節、文件名擴展及容器格式深度解析多重檢測機制確定文件類型。
- ?元數據提取?:獲取文件作者、創建時間、修改日期、標題等元數據信息。 ?
- ?文本提取?:去除HTML標記并提取純文本內容,支持流式處理大文件以避免內存溢出。 ?
- ?語言檢測?:識別文本使用的語言(如中文、英文、法文)。 ?
- ?媒體元數據提取?:支持音頻/視頻文件的分辨率、時長、編碼格式等信息的提取。
二、添加 Apache Tika 依賴
<dependency> <groupId>org.apache.tika</groupId> <artifactId>tika-core</artifactId> <version>1.26</version> </dependency> <dependency> <groupId>org.apache.tika</groupId> <artifactId>tika-parsers</artifactId> <version>1.26</version> </dependency>
三、創建文檔解析服務
import org.apache.tika.parser.AutoDetectParser; import org.apache.tika.sax.BodyContentHandler; import org.springframework.stereotype.Service; import org.apache.tika.metadata.Metadata; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; /** * @ClassName:DocumentParserService * @Description: 創建文檔解析服務 * @Author: songwp * @Date: 2025/8/5 16:27 */ @Service public class DocumentParserService { /** * 解析文檔內容 * @param file * @return */ public String parseDocument(File file) { StringBuilder content = new StringBuilder(); try (InputStream stream = new FileInputStream(file)) { BodyContentHandler handler = new BodyContentHandler(); Metadata metadata = new Metadata(); AutoDetectParser parser = new AutoDetectParser(); parser.parse(stream, handler, metadata); content.append(handler.toString()); } catch (Exception e) { e.printStackTrace(); content.append("Error: ").append(e.getMessage()); } return content.toString(); } }
四、創建控制器類
import com.ruoyi.web.controller.tika.DocumentParserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import java.io.File; import java.io.IOException; /** * @ClassName:DocumentParserController * @Description: 解析文檔的控制器 * @Author: songwp * @Date: 2025/8/5 16:29 */ @RestController @RequestMapping("/api/documents") public class DocumentParserController { @Autowired private DocumentParserService documentParserService; @PostMapping("/parse") public ResponseEntity<String> parseDocument(@RequestParam("file") MultipartFile file) { if (file.isEmpty()) { return ResponseEntity.badRequest().body("File is empty"); } try { // 將上傳的文件轉換為臨時文件 File tempFile = File.createTempFile("document-", ".tmp"); file.transferTo(tempFile); tempFile.deleteOnExit(); // 調用文檔解析服務解析文檔內容 String parsedContent = documentParserService.parseDocument(tempFile); return ResponseEntity.ok(parsedContent); } catch (IOException e) { e.printStackTrace(); return ResponseEntity.status(500).body("Error: " + e.getMessage()); } } }
五、配置和運行
配置 Apache Tika 數據文件 確保你的項目根目錄有一個 tessdata 文件夾,其中包含 Apache Tika 的數據文件。 (1) Apache Tika 官方網站 下載合適的語言數據文件。 (2) 云盤數據下載:https://pan.baidu.com/s/13oPR2r7qOE6lt6SgbpWOQA 提取碼: uaaw
六、測試與驗證
(1)以下是準備的文檔內容截圖

(2) tika文檔內容解析如下:

七、注意事項
1、 文件格式支持 確保上傳的文件是 Apache Tika 支持的格式,如 PDF、Word 文檔、Excel 表格等。 2、優化解析性能 針對大文件和復雜格式的文檔,可能需要優化解析性能。可以考慮使用異步處理、文件流處理等技術來提升解析速度和穩定性。
古今成大事者,不唯有超世之才,必有堅韌不拔之志!

浙公網安備 33010602011771號