Spring boot 使用FTP协议传输文件

亲测:

FTP方式必须事先开发特定的采集程序进行日志数据采集,每次连接都是完整下载整个日志文本文件,网络传输数据量可能非常大,属于主动采集日志数据方式。

想要通过FTP协议传输文件,需先在FTP服务器配置FTP协议:

Windows搭建FTP/SFTP:

https://jingyan.baidu.com/article/574c5219d466c36c8d9dc138.html

Centos 7搭建FTP:

# yum -y install ftp

Centos 7搭建SFTP:

https://blog.csdn.net/xundh/article/details/80346210

Spring Boot 通过FTP/SFTP协议传输文件:

需求:下载 centos 7服务器下的指定路径下的所有文件(Windows也是一样的)

FTP方式:

    implementation('commons-net:commons-net:3.6')
@Service
public class FTPService {

    private static String host = "192.168.1.129";
    private static String user = "ftpuser";
    private static String password = "ftpuser";
    private static String directory = "/var/ftp/pub";
    private static String saveFile = "E:/saveftp";

    /**
     * 获取FTPCLIENT
     */
    private FTPClient getFtpClient() throws SocketException, IOException {
        FTPClient ftp = new FTPClient();
        // 连接
        ftp.connect(host);// 连接FTP服务器
        ftp.login(user,password);// 登陆FTP服务器
        //验证FTP服务器是否登录成功
        int replyCode = ftp.getReplyCode();
        if(!FTPReply.isPositiveCompletion(replyCode)){
            System.out.println("登录验证失败");
        }
        ftp.setControlEncoding("UTF-8"); // 中文支持
        ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
//		ftp.enterLocalPassiveMode();  // 被动模式
        ftp.enterLocalActiveMode();  // 主动模式
        ftp.changeWorkingDirectory(directory);
        return ftp;
    }

    /**
     * FTP下载文件
     */
    public String download() {
        OutputStream os = null;
        String result = "";
        FTPClient ftpClient = new FTPClient();
        try {
            ftpClient = getFtpClient();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("FTP连接发生异常!", e);
        }
        try {
            //切换FTP目录
            ftpClient.changeWorkingDirectory(directory);
            FTPFile[] ftpFiles = ftpClient.listFiles();
            //遍历目录下所有文件
            for(FTPFile file : ftpFiles){
                File localFile = new File(saveFile + File.separator + file.getName());
                os = new FileOutputStream(localFile);
                ftpClient.retrieveFile(file.getName(), os);
                os.close();
            }
            System.out.println("ftp dowmload over");
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("FTP客户端出错!", e);
        } finally {
            try {
                os.close();
                ftpClient.logout();
            } catch (IOException e) {
                e.printStackTrace();
                throw new RuntimeException("关闭FTP连接发生异常!", e);
            }
        }
        return result;
    }
}

SFTP方式:

    implementation 'com.jcraft:jsch:0.1.51'
    implementation 'com.jcraft:jzlib:1.0.7'
@Service
public class SFTPService {

    private FtpRedis ftpRedis;

    private static ChannelSftp sftp = null;

    //账号
    private static String user = "root";
    //主机ip
    private static String host =  "192.168.1.129";
    //密码
    private static String password = "******";
    //上传地址
    private static String directory = "/data/sftp/mysftp";
    //下载目录
    private static String saveFile = "E:/savesftp";

    public SFtpJSch(FtpRedis ftpRedis) {
        this.ftpRedis = ftpRedis;
    }

    private void getConnect(){
        try {
            JSch jsch = new JSch();
            //获取sshSession  账号-ip
            Session sshSession =jsch.getSession(user, host);
            //添加密码
            sshSession.setPassword(password);
            Properties sshConfig = new Properties();
            //严格主机密钥检查
            sshConfig.put("StrictHostKeyChecking", "no");

            sshSession.setConfig(sshConfig);
            //开启sshSession链接
            sshSession.connect();
            //获取sftp通道
            Channel channel = sshSession.openChannel("sftp");
            //开启
            channel.connect();
            sftp = (ChannelSftp) channel;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

   /**
     * sftp下载文件
     */
    public void download() {
        //连接sftp
        getConnect();
        try {
            sftp.cd(directory);
            Vector vector = sftp.ls(directory);
            //遍历目录下所有文件
            for (ChannelSftp.LsEntry lsEntry : vector) {
                //过滤隐藏文件
                if (lsEntry.getFilename().startsWith(".")){
                    continue;
                }
                File localFile = new File(saveFile+File.separator+lsEntry.getFilename());
                sftp.get(lsEntry.getFilename(), new FileOutputStream(localFile));
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 

你可能感兴趣的:(Java)