Java Code Examples for com.jcraft.jsch.SftpException#printStackTrace()

The following examples show how to use com.jcraft.jsch.SftpException#printStackTrace() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SftpConnection.java    From spring-boot with Apache License 2.0 6 votes vote down vote up
/**
 * Upload a single file to the sFTP server.未支持断点续传
 *
 * @param localFilePath       Path of the file on local computer
 * @param remoteDirectoryPath path of directory where the file will be stored
 * @return true if the file was uploaded successfully, false otherwise
 * @throws IOException if any network or IO error occurred.
 */
@Override
public void uploadFile(String localFilePath, String remoteDirectoryPath, boolean logProcess) throws FtpException {

    if (!Paths.get(localFilePath).toFile().exists()) {
        throw new FtpException("Unable to upload file, file does not exist :  " + localFilePath);
    }

    if (!existsDirectory(remoteDirectoryPath))
        createDirectory(remoteDirectoryPath);

    try {
        channel.put(localFilePath, remoteDirectoryPath);
    } catch (SftpException e) {
        e.printStackTrace();
        throw new FtpException("Unable to upload file :  " + localFilePath);
    }

    logger.info("upload file succeed : " + localFilePath);
}
 
Example 2
Source File: SftpConnection.java    From spring-boot with Apache License 2.0 6 votes vote down vote up
/**
 * Create a directory and all missing parent-directories.
 * <p>
 * 设为 private ,不会有在服务器上仅仅创建文件夹的需求。
 *
 * @param remoteDirectoryPath
 * @throws IOException
 */
private void createDirectory(String remoteDirectoryPath) {

    String originalWorkingDirectory = getWorkingDirectory();

    String[] folders = remoteDirectoryPath.split("/");
    for (String folder : folders) {
        if (folder.length() > 0) {
            try {
                channel.cd(folder);
            } catch (SftpException e) {
                try {
                    channel.mkdir(folder);
                    channel.cd(folder);
                } catch (SftpException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }
    logger.info("create remote Directory '" + remoteDirectoryPath + "' succeed.");
    //还原当前目录
    changeDirectory(originalWorkingDirectory);
}
 
Example 3
Source File: SftpSupport.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void put(ChannelSftp cftp) throws SftpIOException {
    // the below is just the replacement for one code line:
    // cftp.put(srcFileName, dstFileName);
    // (connected with #184068 -  Instable remote unit tests failure)
    int attempt = 0;
    while (true) {
        attempt++;
        try {
            cftp.put(parameters.srcFile.getAbsolutePath(), parameters.dstFileName);
            if (attempt > 1) {
                if (LOG.isLoggable(Level.FINE)) {
                    LOG.log(Level.FINE, "Success on attempt {0} to copy {1} to {2}:{3} :\n",
                            new Object[] {attempt, parameters.srcFile.getAbsolutePath(), execEnv, parameters.dstFileName});
                }
            }
            return;
        } catch (SftpException e) {
            if (attempt > PUT_RETRY_COUNT) {
                throw decorateSftpException(e, parameters.dstFileName);
            } else {
                if (LOG.isLoggable(Level.FINE) || attempt == 2) {
                    String message = String.format("Error on attempt %d to copy %s to %s:%s :\n", // NOI18N
                            attempt, parameters.srcFile.getAbsolutePath(), execEnv, parameters.dstFileName);
                    LOG.log(Level.FINE, message, e);
                    if (attempt == 2) {
                        Logger.fullThreadDump(message);
                    }
                }
                e.printStackTrace(System.err);
            }
        }
    }
}
 
Example 4
Source File: SftpConnection.java    From spring-boot with Apache License 2.0 5 votes vote down vote up
/**
     * Determines whether a file exists or not
     * 如果是文件夹,返回 false
     *
     * @param remoteFilePath
     * @return true if exists, false otherwise
     * @throws IOException thrown if any I/O error occurred.
     */
    @Override
    public boolean existsFile(String remoteFilePath) {


        try {
            // System.out.println(channel.realpath(remoteFilePath));
            SftpATTRS attrs = channel.stat(remoteFilePath);
            return attrs.isReg();
        } catch (SftpException e) {
            e.printStackTrace();
            return false;
        }

//        try {
//            // ls 命令
//            // 如果是文件夹或文件夹的符号链接,会进入该文件夹,进行列表,则文件个数会大于 1
//            // 如果是文件,则只会返回该文件本身,文件个数 =1
//            Vector<LsEntry> lsEntries = channel.ls(remoteFilePath);
//
//            for(LsEntry entry:lsEntries)
//               System.out.println(entry.getFilename());
//
//            return lsEntries.size() == 1;
//        } catch (SftpException e) {
//            e.printStackTrace();
//            return false;
//        }

    }