gpt4 book ai didi

java - 如何使用SFTP协议(protocol)多次尝试上传?

转载 作者:行者123 更新时间:2023-12-01 11:12:43 29 4
gpt4 key购买 nike

如果文件上传失败,那么如何在使用 JSCH API 执行 SFTP 时进行多次尝试?如何保证文件上传成功?如何创建线程安全的文件上传实用程序?

最佳答案

创建一个可以从外部类调用的公共(public)静态实用方法。此方法有一个映射参数来保存 sFTPUser、sFTPHost、sFTPPort、sFTPPwd、destinationLocation 和 uploadedFileName 的值:

public static void doSFTP(Map<String, String> ftpParameters) {

if (ftpParameters.get("ID_NAME").equals(
NAPSCommonConstants.MFT_NAPSGPCS_INTF)) {
// do sftp for NAPS GPCS Interface.
uploadUsingSFTP(ftpParameters);
}
}

使用synchronized方法保证线程安全:

private static synchronized void uploadUsingSFTP(
Map<String, String> ftpPrameterList) {
new SFTPUtility().uploadFileMFT(ftpPrameterList.get("sFTPUser"),
ftpPrameterList.get("sFTPHost"), new Integer(ftpPrameterList
.get("sFTPPort")), ftpPrameterList.get("sFTPPwd"),
ftpPrameterList.get("sourceLocation"), ftpPrameterList
.get("destinationLocation"), ftpPrameterList
.get("uploadedFileName"));
}

使用 SFTP 尝试 5 次上传文件的可靠方法:

private void uploadFileMFT(String sFTPUser, String sFTPHost, int sFTPPort,
String sFTPPwd, String sourceLocation, String destinationLocation,
String uploadedFileName) {

LOG.info("Inside uploadFileMFT to upload and verify the file.");

JSch jsch = new JSch();
Vector<String> fileList = null;

/** 5 re-attempt logic to get session */
int attempts = 0;
boolean successfulConnect;
do {
try {
successfulConnect = true;
session = jsch.getSession(sFTPUser, sFTPHost, sFTPPort);
LOG.debug("session connected ...");

session.setPassword(sFTPPwd);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);

session.connect();
LOG.debug("Sftp Session connected ...");

channel = session.openChannel("sftp");
LOG.debug("Sftp Channel opened ...");

channelSftp = (ChannelSftp) channel;
channelSftp.connect();
LOG.info(" Sftp channel opened and connected ...");

channelSftp.put(sourceLocation, destinationLocation);

fileList = channelSftp.ls(destinationLocation);

} catch (JSchException e) {
++attempts;
successfulConnect = false;
LOG.error(e);
} catch (SftpException e) {
++attempts;
successfulConnect = false;
LOG.error(e);
} finally {
if (null != channelSftp) {
channelSftp.exit();
LOG.debug(" sftp Channel exited.");
}

if (null != channel) {
channel.disconnect();
LOG.debug(" Channel disconnected.");
}

if (null != session) {
session.disconnect();
LOG.debug(" Host Session disconnected.");
}
}
} while (attempts < 5 && successfulConnect == false);

fileUploadValidation(fileList, uploadedFileName);

LOG.info("Exiting from method - uploadFileMFT ...");
}

终于可以验证上传的文件了:

private void fileUploadValidation (Vector<String> fileList, String uploadedFileName){
boolean isFileExistis = false;
Object[] objArr = fileList.toArray();
for (int i = 0; i < objArr.length; i++) {
String fileName = objArr[i].toString();
isFileExistis = fileName.contains(uploadedFileName);
if (isFileExistis) {
LOG.info("Uploaded file '" + uploadedFileName + "' was transferred successfull ...");
break;
}else if(i >= objArr.length){
LOG.info("Uploaded file '" + uploadedFileName + "' was failed to transfer ...");
}

}
}

关于java - 如何使用SFTP协议(protocol)多次尝试上传?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32165812/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com