package com.hanserwei.chat.reader; import org.springframework.ai.document.Document; import org.springframework.ai.reader.TextReader; import org.springframework.stereotype.Component; import org.springframework.web.multipart.MultipartFile; import java.util.List; @Component public class MyTextReader implements DocumentParser { /** * 读取 Txt 文档 * @return 读取的文档集合 */ @Override public List parse(MultipartFile file) { TextReader textReader = new TextReader(MultipartFileResource.of(file)); textReader.getCustomMetadata().put("filename", file.getOriginalFilename()); return textReader.read(); } @Override public boolean supports(String filename, String contentType) { return hasExtension(filename, "txt") || matchesContentType(contentType, "text/plain"); } }