|
@@ -17,10 +17,12 @@ import lombok.SneakyThrows;
|
|
|
import okhttp3.MediaType;
|
|
|
import okhttp3.MultipartBody;
|
|
|
import okhttp3.RequestBody;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
+import java.io.File;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Arrays;
|
|
|
import java.util.List;
|
|
@@ -42,6 +44,8 @@ public class KnowledgeFileServiceImpl implements KnowledgeFileService {
|
|
|
private UploadDocsService uploadDocsService;
|
|
|
@Resource
|
|
|
private UploadDocsService2 uploadDocsService2;
|
|
|
+ @Value("${folder.path}")
|
|
|
+ private String folderPath;
|
|
|
|
|
|
@Override
|
|
|
@SneakyThrows
|
|
@@ -102,4 +106,45 @@ public class KnowledgeFileServiceImpl implements KnowledgeFileService {
|
|
|
|
|
|
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void initKnowledgeDocument() {
|
|
|
+ try {
|
|
|
+ // 读取文件夹中的所有文件
|
|
|
+ List<String> fileNames = readFileNamesFromFolder(folderPath);
|
|
|
+ List<KnowledgeFileDO> list = new ArrayList<>(fileNames.size());
|
|
|
+ fileNames.forEach(s -> {
|
|
|
+ KnowledgeFileDO fileDO = new KnowledgeFileDO();
|
|
|
+ fileDO.setName(s);
|
|
|
+ fileDO.setType(FileTypeUtils.getMineType(s));
|
|
|
+ list.add(fileDO);
|
|
|
+ });
|
|
|
+ knowledgeFileMapper.insertBatch(list);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 读取指定文件夹中的所有文件名
|
|
|
+ */
|
|
|
+ private static List<String> readFileNamesFromFolder(String folderPath) {
|
|
|
+ List<String> fileNames = new ArrayList<>();
|
|
|
+ File folder = new File(folderPath);
|
|
|
+
|
|
|
+ if (!folder.exists() || !folder.isDirectory()) {
|
|
|
+ throw new IllegalArgumentException("Invalid folder path: " + folderPath);
|
|
|
+ }
|
|
|
+
|
|
|
+ File[] files = folder.listFiles();
|
|
|
+ if (files != null) {
|
|
|
+ for (File file : files) {
|
|
|
+ if (file.isFile()) {
|
|
|
+ fileNames.add(file.getName());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return fileNames;
|
|
|
+ }
|
|
|
}
|