From 43df92ab68117119318224e59f700ebdc16b695a Mon Sep 17 00:00:00 2001 From: wangguangyuan Date: Tue, 23 Sep 2025 17:07:29 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=BB=E5=8A=A1=E8=AF=A6=E6=83=85=E5=AF=BC?= =?UTF-8?q?=E5=87=BA=EF=BC=8Cftp=E8=BF=9E=E6=8E=A5=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../inspect/base/core/sftp/SftpFactory.java | 63 ++++++++++++------- 1 file changed, 42 insertions(+), 21 deletions(-) diff --git a/inspect-base/inspect-base-core/src/main/java/com/inspect/base/core/sftp/SftpFactory.java b/inspect-base/inspect-base-core/src/main/java/com/inspect/base/core/sftp/SftpFactory.java index 08f83ef..ed62ac6 100644 --- a/inspect-base/inspect-base-core/src/main/java/com/inspect/base/core/sftp/SftpFactory.java +++ b/inspect-base/inspect-base-core/src/main/java/com/inspect/base/core/sftp/SftpFactory.java @@ -68,31 +68,52 @@ public class SftpFactory { } public FTPSClient connect() throws IOException { - boolean isImplicit = "true".equals(sftpLoginConfig.getIsImplicit()); - //boolean isImplicit = Boolean.getBoolean(sftpLoginConfig.getIsImplicit()); - logger.info(Color.MAGENTA + "[FTP] isImplicit: {}" + Color.END, isImplicit); - FTPSClient ftps = new FTPSClient(isImplicit); - try { - ftps.setControlEncoding("UTF-8"); - ftps.connect(sftpLoginConfig.getIp(), Integer.parseInt(sftpLoginConfig.getPort())); - } catch (Exception e) { - logger.error("[FTP] CONN FAIL: {}, CAUSE: {}", JSONObject.toJSONString(sftpLoginConfig), e.getMessage()); + int maxRetries = 5; + int retryCount = 0; + long retryDelay = 1000L; + while (retryCount < maxRetries) { + boolean isImplicit = "true".equals(sftpLoginConfig.getIsImplicit()); + //boolean isImplicit = Boolean.getBoolean(sftpLoginConfig.getIsImplicit()); + logger.info(Color.MAGENTA + "[FTP] isImplicit: {}" + Color.END, isImplicit); + FTPSClient ftps = new FTPSClient(isImplicit); try { - Thread.sleep(1000L); - } catch (InterruptedException e2) { + ftps.setControlEncoding("UTF-8"); + ftps.connect(sftpLoginConfig.getIp(), Integer.parseInt(sftpLoginConfig.getPort())); + boolean login = ftps.login(sftpLoginConfig.getUsername(), sftpLoginConfig.getPassword()); + if (!login) { + throw new ServiceException("LOGIN TO FTP FAIL!"); + } else { + return ftps; + } + } catch (Exception e) { + retryCount++; + logger.error("[FTP] CONN FAIL: {}, (attempt {}/{}): CAUSE: {}", + JSONObject.toJSONString(sftpLoginConfig), retryCount, maxRetries, e.getMessage()); + + // 清理资源 + if (ftps != null && ftps.isConnected()) { + try { + ftps.disconnect(); + } catch (IOException ex) { + logger.debug("[FTP] Error during disconnect", ex); + } + } - ; + if (retryCount < maxRetries) { + try { + logger.info("[FTP] Retrying in {} ms", retryDelay); + Thread.sleep(retryDelay); + retryDelay *= 2; + } catch (InterruptedException ie) { + Thread.currentThread().interrupt(); + throw new IOException("Interrupted during retry", ie); + } + } else { + throw new IOException("Failed to connect after " + maxRetries + " attempts", e); + } } - - return connect(); - } - - boolean login = ftps.login(sftpLoginConfig.getUsername(), sftpLoginConfig.getPassword()); - if (!login) { - throw new ServiceException("LOGIN TO FTP FAIL!"); - } else { - return ftps; } + throw new IOException("Unexpected error in connection process"); } public static void main(String[] args) {