Search in sources :

Example 6 with SftpException

use of com.jn.agileway.ssh.client.sftp.exception.SftpException in project agileway by fangjinuo.

the class SshjSftps method wrapSftpException.

public static SftpException wrapSftpException(Throwable ex) {
    if (ex instanceof SFTPException) {
        ResponseStatusCode statusCode = Enums.ofName(ResponseStatusCode.class, ((SFTPException) ex).getStatusCode().name());
        SftpException exception = null;
        if (statusCode == ResponseStatusCode.NO_SUCH_FILE) {
            exception = new NoSuchFileSftpException(ex);
        } else {
            exception = new SftpException(ex);
        }
        exception.setStatusCode(statusCode);
        return exception;
    } else {
        return new SftpException(ex);
    }
}
Also used : NoSuchFileSftpException(com.jn.agileway.ssh.client.sftp.exception.NoSuchFileSftpException) ResponseStatusCode(com.jn.agileway.ssh.client.sftp.ResponseStatusCode) SftpException(com.jn.agileway.ssh.client.sftp.exception.SftpException) NoSuchFileSftpException(com.jn.agileway.ssh.client.sftp.exception.NoSuchFileSftpException)

Example 7 with SftpException

use of com.jn.agileway.ssh.client.sftp.exception.SftpException in project agileway by fangjinuo.

the class Ssh2Sftps method wrapSftpException.

public static SftpException wrapSftpException(Throwable ex) {
    if (ex instanceof SFTPException) {
        ResponseStatusCode statusCode = Enums.ofCode(ResponseStatusCode.class, ((SFTPException) ex).getServerErrorCode());
        SftpException exception = null;
        if (statusCode == ResponseStatusCode.NO_SUCH_FILE) {
            exception = new NoSuchFileSftpException(ex);
        } else {
            exception = new SftpException(ex);
        }
        exception.setStatusCode(statusCode);
        return exception;
    } else {
        return new SftpException(ex);
    }
}
Also used : NoSuchFileSftpException(com.jn.agileway.ssh.client.sftp.exception.NoSuchFileSftpException) ResponseStatusCode(com.jn.agileway.ssh.client.sftp.ResponseStatusCode) SftpException(com.jn.agileway.ssh.client.sftp.exception.SftpException) NoSuchFileSftpException(com.jn.agileway.ssh.client.sftp.exception.NoSuchFileSftpException) SFTPException(com.trilead.ssh2.SFTPException)

Example 8 with SftpException

use of com.jn.agileway.ssh.client.sftp.exception.SftpException in project agileway by fangjinuo.

the class Sftps method copyFile.

/**
 * copy local file to remote dir
 */
public static int copyFile(@NonNull SftpSession session, @NonNull File file, @NotEmpty String remoteDir, @Nullable String newName) throws SftpException {
    boolean remoteDirExist = Sftps.existDirectory(session, remoteDir);
    if (!remoteDirExist) {
        session.mkdir(remoteDir, null);
    }
    String name = Emptys.isEmpty(newName) ? file.getName() : newName;
    String filepath = remoteDir + "/" + name;
    FileInputStream inputStream = null;
    SftpFile sftpFile = null;
    int sum = 0;
    try {
        sftpFile = session.open(filepath, OpenMode.WRITE, null);
        inputStream = new FileInputStream(file);
        byte[] buffer = new byte[4096];
        int readLength = 0;
        while ((readLength = inputStream.read(buffer, 0, buffer.length)) != -1) {
            sftpFile.write(sum, buffer, 0, readLength);
            sum += readLength;
        }
    } catch (Throwable ex) {
        throw new SftpException(ex);
    } finally {
        IOs.close(sftpFile);
        IOs.close(inputStream);
    }
    return sum;
}
Also used : SftpException(com.jn.agileway.ssh.client.sftp.exception.SftpException) NoSuchFileSftpException(com.jn.agileway.ssh.client.sftp.exception.NoSuchFileSftpException)

Aggregations

SftpException (com.jn.agileway.ssh.client.sftp.exception.SftpException)8 NoSuchFileSftpException (com.jn.agileway.ssh.client.sftp.exception.NoSuchFileSftpException)6 ResponseStatusCode (com.jn.agileway.ssh.client.sftp.ResponseStatusCode)4 ChannelSftp (com.jcraft.jsch.ChannelSftp)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 SFTPException (ch.ethz.ssh2.SFTPException)1 FileAttrs (com.jn.agileway.ssh.client.sftp.attrs.FileAttrs)1 FileMode (com.jn.agileway.ssh.client.sftp.attrs.FileMode)1 SFTPException (com.trilead.ssh2.SFTPException)1 SFTPv3FileHandle (com.trilead.ssh2.SFTPv3FileHandle)1 InputStream (java.io.InputStream)1