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

时间:2015-08-23 10:51:18

标签: java sftp jsch

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

1 个答案:

答案 0 :(得分:1)

创建一个可以从外部类调用的公共静态实用程序方法。此方法有一个map参数来保存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 ...");
            }

        }
    }