/** * Carry out the transfer. * @throws IOException on i/o errors * @throws JSchException on errors detected by scp */ @Override public void execute() throws IOException, JSchException { final ChannelSftp channel = openSftpChannel(); try { channel.connect(); try { final SftpATTRS attrs = channel.stat(remoteFile); if (attrs.isDir() && !remoteFile.endsWith("/")) { remoteFile += "/"; } } catch (final SftpException ee) { // Ignored } getDir(channel, remoteFile, localFile); } catch (final SftpException e) { throw new JSchException("Could not get '" + remoteFile + "' to '" + localFile + "' - " + e.toString(), e); } finally { if (channel != null) { channel.disconnect(); } } log("done\n"); }
private void getDir(final ChannelSftp channel, final String remoteFile, final File localFile) throws SftpException { 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(); } @SuppressWarnings("unchecked") final List<ChannelSftp.LsEntry> files = channel.ls(remoteFile); for (ChannelSftp.LsEntry le : files) { final String name = le.getFilename(); if (le.getAttrs().isDir()) { if (".".equals(name) || "..".equals(name)) { continue; } getDir(channel, channel.pwd() + "/" + name + "/", new File(localFile, le.getFilename())); } else { getFile(channel, le, localFile); } } channel.cd(".."); }
final boolean trackProgress = getVerbose() && totalLength > HUNDRED_KILOBYTES; if (trackProgress) { monitor = getProgressMonitor(); log("Receiving: " + remoteFile + " : " + le.getAttrs().getSize()); channel.get(remoteFile, localFile.getAbsolutePath(), monitor); } finally { final long endTime = System.currentTimeMillis(); logStats(startTime, endTime, (int) totalLength); if (getPreserveLastModified()) { FileUtils.getFileUtils().setFileLastModified(localFile, ((long) le.getAttrs().getMTime()) * 1000);
private void download(final String fromSshUri, final String toPath) throws JSchException, IOException { final String file = parseUri(fromSshUri); Session session = null; try { session = openSession(); ScpFromMessage message = null; if (!isSftp) { message = new ScpFromMessage(getVerbose(), session, file, getProject().resolveFile(toPath), fromSshUri.endsWith("*"), preserveLastModified, compressed); } else { message = new ScpFromMessageBySftp(getVerbose(), session, file, getProject().resolveFile(toPath), fromSshUri.endsWith("*"), preserveLastModified); } log("Receiving file: " + file); message.setLogListener(this); message.execute(); } finally { if (session != null) { session.disconnect(); } } }