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