简体   繁体   English

如何使用SFTP协议进行多次上传尝试?

[英]How to do multiple attempts to upload using SFTP protocol?

In case of file upload fails then how to do multiple attempts while doing SFTP by using JSCH API? 如果文件上传失败,那么如何使用JSCH API在进行SFTP时进行多次尝试? How to ensure file is uploaded successfully? 如何确保文件上传成功? How to create thread-safe file upload utility? 如何创建线程安全文件上传实用程序?

Create a common static utility method which can be invoked from the external class. 创建一个可以从外部类调用的通用静态实用程序方法。 This method has a map argument to persist the values of sFTPUser, sFTPHost, sFTPPort, sFTPPwd, destinationLocation and uploadedFileName : 此方法具有一个映射参数,用于保留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);
        }
    }

Use synchronized method to ensure thread safety: 使用同步方法以确保线程安全:

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"));
    }

Responsible method to upload files using SFTP with 5 attempts: 尝试5次使用SFTP上传文件的负责方法:

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 ...");
    }

Finally uploaded file can be validated: 最后上传的文件可以验证:

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 ...");
            }

        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM