只适合多数据没有图片又需要图片,整理好的图片统一上传并保存到每一条数据上。
这里是根据上传文件的名称,清洗之后用名称去数据库找,找到后将上传保存到这条数据。
package com.nbomb.route.util;
/**
* @author SunWenKe
* @version v1.0
* @date 2023-07-13 11:08
*/
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.nbomb.route.domain.Village;
import com.nbomb.route.service.IVillageService;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.file.FileUploadUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
@Component
public class ReadFolderImages {
@Autowired
IVillageService villageService;
List<String> list = new ArrayList<>();
List<String> listUp = new ArrayList<>();
public void uploadImag() {
// 指定文件夹路径
String folderPath = "E:\\图片裁剪";
// 创建一个File对象来表示文件夹
File folder = new File(folderPath);
// 使用递归函数来遍历文件夹
traverseFolder(folder);
HashSet<String> set = new HashSet<>(list);
System.out.println("所有未上传的"+set);
HashSet<String> listUpSst = new HashSet<>(listUp);
System.out.println("所有上传的"+listUpSst);
}
public void traverseFolder(File folder) {
if (folder.isDirectory()) {
// 获取文件夹下的所有文件和子文件夹
File[] files = folder.listFiles();
if (files != null) {
for (File file : files) {
// 递归调用自身处理子文件夹
traverseFolder(file);
}
}
} else {
// 如果是图片文件则上传
if (isImageFile(folder.getName())) {
try {
System.out.println("图片名称"+folder.getName());
String name = folder.getName();
String all = name.replaceAll("\\..*$", "");
String result = all.replaceAll("\\([^\\)]*\\)", "").replaceAll(" ","");
System.out.println("干净的文件名是:"+result);
// 将图片文件转换为MultipartFile类型
MultipartFile multipartFile = convertToMultipartFile(folder);
String filePath = RuoYiConfig.getUploadPath();
// System.out.println("文件路径"+filePath);
// 在这里实现上传到服务器的逻辑
String upload = FileUploadUtils.upload(filePath, multipartFile);
System.out.println("上传路径:"+upload);
Thread.sleep(2000);
//上传完之后更新到数据库表中
boolean update = villageService.updateImage(result,upload);
if (update){
System.out.println("当前村庄已上传:"+result);
listUp.add(result);
}else {
list.add(result);
System.out.println("当前村庄未上传:"+result);
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
}
}
}
}
public boolean isImageFile(String fileName) {
// 定义支持的图片文件类型,根据需要可添加其他类型
String[] imageExtensions = {".jpg", ".jpeg", ".png", ".gif"};
for (String extension : imageExtensions) {
if (fileName.toLowerCase().endsWith(extension)) {
return true;
}
}
return false;
}
public MultipartFile convertToMultipartFile(File file) throws IOException {
BufferedImage bufferedImage = ImageIO.read(file);
return new MultipartFile() {
@Override
public String getName() {
return file.getName();
}
@Override
public String getOriginalFilename() {
return file.getName();
}
@Override
public String getContentType() {
// 设置图片类型,根据需要自行修改
return "image/jpeg";
}
@Override
public boolean isEmpty() {
return false;
}
@Override
public long getSize() {
return file.length();
}
@Override
public byte[] getBytes() throws IOException {
return null;
}
@Override
public InputStream getInputStream() throws IOException {
return null;
}
@Override
public void transferTo(File dest) throws IOException, IllegalStateException {
// 将缓冲图像写入目标文件
ImageIO.write(bufferedImage, "jpg", dest);
}
};
}
}