import java.io.IOException; import java.net.MalformedURLException; import java.util.Properties; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPFile; import org.apache.commons.net.ftp.FTPReply; import com.jcraft.jsch.*; import lombok.extern.slf4j.Slf4j; @Slf4j public class FtpUtil { private final String odsRead; private final String ftpAddress; private final int ftpPort; private final String ftpUserName; private final String ftpPassWord; private final String ftpFile; public FTPClient ftpClient = null; public FtpUtil(String odsRead, String ftpAddress, int ftpPort, String ftpUserName, String ftpPassWord, String ftpFile) { this.odsRead = odsRead; this.ftpAddress = ftpAddress; this.ftpPort = ftpPort; this.ftpUserName = ftpUserName; this.ftpPassWord = ftpPassWord; this.ftpFile = ftpFile; } /** * 连接sftp服务器 */ public void sftpLogin(){ ftpClient = new FTPClient(); ftpClient.setControlEncoding("utf-8"); try { log.info("正在连接服务器,地址:{}:{}",ftpAddress,ftpPort); ftpClient.connect(ftpAddress, ftpPort); ftpClient.login(ftpUserName, ftpPassWord); int replyCode = ftpClient.getReplyCode(); if(FTPReply.isPositiveCompletion(replyCode)){ log.info("连接服务器成功,地址:{}:{}",ftpAddress,ftpPort); }else{ log.info("连接服务器失败,用户名:{},密码:{}",ftpUserName,ftpPassWord); } } catch (IOException e) { log.info("登录失败:地址:{},端口:{},用户名:{},密码:{},详情:{}",ftpAddress,ftpPort,ftpUserName,ftpPassWord,e); } } public void downloadFile(){ try { FTPFile[] listFiles = ftpClient.listFiles(); if (listFiles != null){ for(FTPFile ftpFile : listFiles){ log.info("文件名称:{}",ftpFile.getName()); } } } catch (IOException e) { e.printStackTrace(); } } /** * 关闭FTP服务链接 * @throws IOException */ public void disConnection() { try { if (ftpClient.isConnected()) { ftpClient.disconnect(); } } catch (IOException e) { log.info("关闭FTP服务链接,详情:"+e); } } }
FTP取数协议
最新推荐文章于 2022-11-23 11:24:40 发布