|
|
@ -68,31 +68,52 @@ public class SftpFactory { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public FTPSClient connect() throws IOException { |
|
|
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 { |
|
|
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) { |
|
|
public static void main(String[] args) { |
|
|
|