package com.keystar.plane.inspection.utils;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;

import java.io.IOException;

public class FtpUtils {

    /**
     *  获取服务器中最新的文件
     **/
    public static void getFtpFile() {
        String server = "10.138.6.221";
        int port = 22;
        String user = "root";
        String pass = "keystar";
        FTPClient ftpClient = new FTPClient();
        try {
            ftpClient.connect(server, port);
            ftpClient.login(user, pass);
            ftpClient.enterLocalPassiveMode();
            String remoteFilePath = "/data/minio/data/nest/autoUpload/photo/";
//            FTPFile[] ftpFiles = ftpClient.listFiles(remoteFilePath);
            FTPFile ftpFile = ftpClient.mlistFile(remoteFilePath);
            if (ftpFile != null) {
                String name = ftpFile.getName();
                long size = ftpFile.getSize();
                String timestamp = ftpFile.getTimestamp().getTime().toString();
                String type = ftpFile.isDirectory() ? "Directory" : "File";
                System.out.println("Name: " + name);
                System.out.println("Size: " + size);
                System.out.println("Type: " + type);
                System.out.println("Timestamp: " + timestamp);
            } else {
                System.out.println("文件不存在");
            }
            ftpClient.logout();
            ftpClient.disconnect();

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (ftpClient.isConnected()) {
                try {
                    ftpClient.disconnect();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }
    }




}
