ScpFromMessageBySftppublic class ScpFromMessageBySftp extends ScpFromMessage A helper object representing an scp download. |
Fields Summary |
---|
private String | remoteFile | private File | localFile | private boolean | isRecursive | private boolean | verbose |
Constructors Summary |
---|
public ScpFromMessageBySftp(boolean verbose, com.jcraft.jsch.Session session, String aRemoteFile, File aLocalFile, boolean recursive)Constructor for ScpFromMessageBySftp.
super(verbose, session);
this.verbose = verbose;
this.remoteFile = aRemoteFile;
this.localFile = aLocalFile;
this.isRecursive = recursive;
| public ScpFromMessageBySftp(com.jcraft.jsch.Session session, String aRemoteFile, File aLocalFile, boolean recursive)Constructor for ScpFromMessageBySftp.
this(false, session, aRemoteFile, aLocalFile, recursive);
|
Methods Summary |
---|
public void | execute()Carry out the transfer.
ChannelSftp channel = openSftpChannel();
try {
channel.connect();
try {
SftpATTRS attrs = channel.stat(remoteFile);
if (attrs.isDir() && !remoteFile.endsWith("/")) {
remoteFile = remoteFile + "/";
}
} catch (SftpException ee) {
// Ignored
}
getDir(channel, remoteFile, localFile);
} catch (SftpException e) {
throw new JSchException(e.toString());
} finally {
if (channel != null) {
channel.disconnect();
}
}
log("done\n");
| private void | getDir(com.jcraft.jsch.ChannelSftp channel, java.lang.String remoteFile, java.io.File localFile)
String pwd = remoteFile;
if (remoteFile.lastIndexOf('/") != -1) {
if (remoteFile.length() > 1) {
pwd = remoteFile.substring(0, remoteFile.lastIndexOf('/"));
}
}
channel.cd(pwd);
if (!localFile.exists()) {
localFile.mkdirs();
}
java.util.Vector files = channel.ls(remoteFile);
for (int i = 0; i < files.size(); i++) {
ChannelSftp.LsEntry le = (ChannelSftp.LsEntry) files.elementAt(i);
String name = le.getFilename();
if (le.getAttrs().isDir()) {
if (name.equals(".") || name.equals("..")) {
continue;
}
getDir(channel,
channel.pwd() + "/" + name + "/",
new File(localFile, le.getFilename()));
} else {
getFile(channel, le, localFile);
}
}
channel.cd("..");
| private void | getFile(com.jcraft.jsch.ChannelSftp channel, ChannelSftp.LsEntry le, java.io.File localFile)
String remoteFile = le.getFilename();
if (!localFile.exists()) {
String path = localFile.getAbsolutePath();
int i = 0;
if ((i = path.lastIndexOf(File.pathSeparator)) != -1) {
if (path.length() > File.pathSeparator.length()) {
new File(path.substring(0, i)).mkdirs();
}
}
}
if (localFile.isDirectory()) {
localFile = new File(localFile, remoteFile);
}
long startTime = System.currentTimeMillis();
long totalLength = le.getAttrs().getSize();
SftpProgressMonitor monitor = null;
boolean trackProgress = getVerbose() && totalLength > 102400;
if (trackProgress) {
monitor = getProgressMonitor();
}
try {
log("Receiving: " + remoteFile + " : " + le.getAttrs().getSize());
channel.get(remoteFile, localFile.getAbsolutePath(), monitor);
} finally {
long endTime = System.currentTimeMillis();
logStats(startTime, endTime, (int) totalLength);
}
|
|