|
@@ -0,0 +1,92 @@
|
|
|
+package com.miniframe;
|
|
|
+
|
|
|
+import com.miniframe.core.ext.UtilTools;
|
|
|
+import com.miniframe.model.es.EsCom;
|
|
|
+import com.miniframe.model.es.FileBase64;
|
|
|
+import com.miniframe.model.es.dao.EsComMapper;
|
|
|
+import com.miniframe.model.es.dao.FileBase64Mapper;
|
|
|
+import com.miniframe.model.system.dao.SysFileMapper;
|
|
|
+import com.miniframe.tools.ali.Data;
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.boot.test.context.SpringBootTest;
|
|
|
+
|
|
|
+import java.nio.file.Files;
|
|
|
+import java.nio.file.Path;
|
|
|
+import java.nio.file.Paths;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Base64;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 删除filebase64记录,重新从本地读取并存储
|
|
|
+ */
|
|
|
+@SpringBootTest
|
|
|
+public class base64Test {
|
|
|
+ @Autowired
|
|
|
+ private FileBase64Mapper fileBase64Mapper;
|
|
|
+ @Autowired
|
|
|
+ private EsComMapper esComMapper;
|
|
|
+ @Autowired
|
|
|
+ private SysFileMapper sysFileMapper;
|
|
|
+ @Test
|
|
|
+ public void test() throws Exception {
|
|
|
+ List<EsCom> esComList = esComMapper.selectAll();
|
|
|
+ List<String> stringList = new ArrayList<>();
|
|
|
+ //在escom表中查找image编号
|
|
|
+ for (EsCom esCom : esComList) {
|
|
|
+ String image = esCom.getImage();
|
|
|
+ if(image!=null&&!image.equals("")&&!image.isEmpty()){
|
|
|
+ stringList.add(image);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //去重
|
|
|
+ List<String> stringList1 = stringList.stream()
|
|
|
+ .distinct()
|
|
|
+ .collect(Collectors.toList());
|
|
|
+// System.out.println(stringList1);
|
|
|
+ //在sys_file表中查找文件名
|
|
|
+ List<String> filenameList = new ArrayList<>();
|
|
|
+ for(String str : stringList1){
|
|
|
+ filenameList.add(sysFileMapper.selectByPrimaryKey(str).getFilename());
|
|
|
+ }
|
|
|
+
|
|
|
+ for(int i=0;i<filenameList.size();i++){
|
|
|
+ FileBase64 fileBase64 = new FileBase64();
|
|
|
+ fileBase64.setFbId(stringList1.get(i)+getFilePrefix(filenameList.get(i)));
|
|
|
+ fileBase64.setFileBase64(convertToBase64("C:\\Users\\Administrator\\Desktop\\fsdownload\\"+filenameList.get(i)));
|
|
|
+ fileBase64.setFilename(filenameList.get(i));
|
|
|
+ fileBase64.setMime(getFileExtension(filenameList.get(i)));
|
|
|
+ fileBase64.setCreateTime(new Date());
|
|
|
+ fileBase64.setUpdateTime(new Date());
|
|
|
+ fileBase64.setRemark("");
|
|
|
+ fileBase64Mapper.insert(fileBase64);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //转base64
|
|
|
+ public static String convertToBase64(String filePath) throws Exception {
|
|
|
+ // 读取图片文件字节
|
|
|
+ byte[] fileContent = Files.readAllBytes(Paths.get(filePath));
|
|
|
+
|
|
|
+ // 使用Base64编码器进行编码
|
|
|
+ String baseStr = Base64.getEncoder().encodeToString(fileContent);
|
|
|
+ return "data:image/png;base64,"+baseStr;
|
|
|
+ }
|
|
|
+
|
|
|
+ //取后缀
|
|
|
+ public String getFileExtension(String fileName) {
|
|
|
+ Path path = Paths.get(fileName);
|
|
|
+ String fullFileName = path.getFileName().toString();
|
|
|
+ int dotIndex = fullFileName.lastIndexOf('.');
|
|
|
+ return (dotIndex == -1) ? "" : fullFileName.substring(dotIndex + 1);
|
|
|
+ }
|
|
|
+ //取前缀
|
|
|
+ public static String getFilePrefix(String fileName) {
|
|
|
+ Path path = Paths.get(fileName);
|
|
|
+ String fullFileName = path.getFileName().toString();
|
|
|
+ int dotIndex = fullFileName.lastIndexOf('.');
|
|
|
+ return (dotIndex > 0) ? fullFileName.substring(0, dotIndex) : fullFileName;
|
|
|
+ }
|
|
|
+}
|