文件夹图片批量上传到数据库

只适合多数据没有图片又需要图片,整理好的图片统一上传并保存到每一条数据上。
这里是根据上传文件的名称,清洗之后用名称去数据库找,找到后将上传保存到这条数据。

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);
            }
        };
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Java中的JSch和SCP实现批量上传图片到Linux服务器某个文件夹。以下是一个示例代码: ``` import com.jcraft.jsch.*; import java.io.File; import java.io.FileInputStream; import java.util.Properties; public class UploadImages { public static void main(String[] args) { String localPath = "/path/to/local/folder"; // 本地图片文件夹路径 String remotePath = "/path/to/remote/folder"; // 远程Linux服务器文件夹路径 String username = "your_username"; // Linux服务器用户名 String password = "your_password"; // Linux服务器密码 String host = "your_linux_server_host"; // Linux服务器IP地址 JSch jsch = new JSch(); Session session = null; Channel channel = null; try { session = jsch.getSession(username, host, 22); session.setPassword(password); Properties config = new Properties(); config.put("StrictHostKeyChecking", "no"); session.setConfig(config); session.connect(); channel = session.openChannel("sftp"); channel.connect(); ChannelSftp sftp = (ChannelSftp) channel; File localFolder = new File(localPath); File[] files = localFolder.listFiles(); for (File file : files) { if (file.isFile()) { FileInputStream fis = new FileInputStream(file); sftp.put(fis, remotePath + "/" + file.getName()); fis.close(); } } sftp.disconnect(); channel.disconnect(); session.disconnect(); System.out.println("上传完成"); } catch (JSchException | SftpException | java.io.IOException e) { e.printStackTrace(); } } } ``` 首先需要引入JSch的jar包,可以从官网下载。然后根据实际情况修改代码中的本地图片文件夹路径、远程Linux服务器文件夹路径、Linux服务器用户名、密码、IP地址等信息。运行代码后,程序会将本地图片文件夹中的所有图片传到远程Linux服务器的指定文件夹中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值