ScpToMessageBySftppublic class ScpToMessageBySftp extends ScpToMessage Utility class to carry out an upload by sftp. |
Fields Summary |
---|
private File | localFile | private String | remotePath | private List | directoryList |
Constructors Summary |
---|
public ScpToMessageBySftp(boolean verbose, com.jcraft.jsch.Session session, File aLocalFile, String aRemotePath)Constructor for a local file to remote.
this(verbose, session, aRemotePath);
this.localFile = aLocalFile;
| public ScpToMessageBySftp(boolean verbose, com.jcraft.jsch.Session session, List aDirectoryList, String aRemotePath)Constructor for a local directories to remote.
this(verbose, session, aRemotePath);
this.directoryList = aDirectoryList;
| private ScpToMessageBySftp(boolean verbose, com.jcraft.jsch.Session session, String aRemotePath)Constructor for ScpToMessage.
super(verbose, session);
this.remotePath = aRemotePath;
| public ScpToMessageBySftp(com.jcraft.jsch.Session session, File aLocalFile, String aRemotePath)Constructor for ScpToMessage.
this(false, session, aLocalFile, aRemotePath);
| public ScpToMessageBySftp(com.jcraft.jsch.Session session, List aDirectoryList, String aRemotePath)Constructor for ScpToMessage.
this(false, session, aDirectoryList, aRemotePath);
|
Methods Summary |
---|
private void | doMultipleTransfer()
ChannelSftp channel = openSftpChannel();
try {
channel.connect();
try {
channel.cd(remotePath);
for (Iterator i = directoryList.iterator(); i.hasNext();) {
Directory current = (Directory) i.next();
sendDirectory(channel, current);
}
} catch (SftpException e) {
throw new JSchException(e.toString());
}
} finally {
if (channel != null) {
channel.disconnect();
}
}
| private void | doSingleTransfer()
ChannelSftp channel = openSftpChannel();
try {
channel.connect();
try {
sendFileToRemote(channel, localFile, remotePath);
} catch (SftpException e) {
throw new JSchException(e.toString());
}
} finally {
if (channel != null) {
channel.disconnect();
}
}
| public void | execute()Carry out the transfer.
if (directoryList != null) {
doMultipleTransfer();
}
if (localFile != null) {
doSingleTransfer();
}
log("done.\n");
| public java.io.File | getLocalFile()Get the local file.
return localFile;
| public java.lang.String | getRemotePath()Get the remote path.
return remotePath;
| private void | sendDirectory(com.jcraft.jsch.ChannelSftp channel, Directory current)
for (Iterator fileIt = current.filesIterator(); fileIt.hasNext();) {
sendFileToRemote(channel, (File) fileIt.next(), null);
}
for (Iterator dirIt = current.directoryIterator(); dirIt.hasNext();) {
Directory dir = (Directory) dirIt.next();
sendDirectoryToRemote(channel, dir);
}
| private void | sendDirectoryToRemote(com.jcraft.jsch.ChannelSftp channel, Directory directory)
String dir = directory.getDirectory().getName();
try {
channel.stat(dir);
} catch (SftpException e) {
// dir does not exist.
if (e.id == ChannelSftp.SSH_FX_NO_SUCH_FILE) {
channel.mkdir(dir);
}
}
channel.cd(dir);
sendDirectory(channel, directory);
channel.cd("..");
| private void | sendFileToRemote(com.jcraft.jsch.ChannelSftp channel, java.io.File localFile, java.lang.String remotePath)
long filesize = localFile.length();
if (remotePath == null) {
remotePath = localFile.getName();
}
long startTime = System.currentTimeMillis();
long totalLength = filesize;
// only track progress for files larger than 100kb in verbose mode
boolean trackProgress = getVerbose() && filesize > 102400;
SftpProgressMonitor monitor = null;
if (trackProgress) {
monitor = getProgressMonitor();
}
try {
if (this.getVerbose()) {
log("Sending: " + localFile.getName() + " : " + filesize);
}
channel.put(localFile.getAbsolutePath(), remotePath, monitor);
} finally {
if (this.getVerbose()) {
long endTime = System.currentTimeMillis();
logStats(startTime, endTime, (int) totalLength);
}
}
|
|