private void parseAndFetchFile(final String serverResponse, final File localFile, final OutputStream out, final InputStream in) throws IOException, JSchException { int start = 0; int end = serverResponse.indexOf(' ', start + 1); start = end + 1; end = serverResponse.indexOf(' ', start + 1); final long filesize = Long.parseLong(serverResponse.substring(start, end)); final String filename = serverResponse.substring(end + 1); log("Receiving: " + filename + " : " + filesize); final File transferFile = localFile.isDirectory() ? new File(localFile, filename) : localFile; fetchFile(transferFile, filesize, out, in); waitForAck(in); sendAck(out); }
/** * Carry out the transfer. * @throws IOException on i/o errors * @throws JSchException on errors detected by scp */ @Override public void execute() throws IOException, JSchException { String command = "scp -f "; if (isRecursive) { command += "-r "; } if (getCompressed()) { command += "-C "; } command += remoteFile; final Channel channel = openExecChannel(command); try { // get I/O streams for remote scp final OutputStream out = channel.getOutputStream(); final InputStream in = channel.getInputStream(); channel.connect(); sendAck(out); startRemoteCpProtocol(in, out, localFile); } finally { if (channel != null) { channel.disconnect(); } } log("done\n"); }
throws IOException, JSchException { final byte[] buf = new byte[BUFFER_SIZE]; sendAck(out); final boolean trackProgress = getVerbose() && filesize > HUNDRED_KILOBYTES; percentTransmitted = trackProgress(initFilesize, totalLength, percentTransmitted); logStats(startTime, endTime, totalLength); fos.flush(); fos.close(); if (getPreserveLastModified()) { setLastModified(localFile);
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(); } } }
private void setLastModified(final File localFile) throws JSchException { SftpATTRS fileAttributes = null; final ChannelSftp channel = openSftpChannel(); channel.connect(); try { fileAttributes = channel.lstat(remoteDir(remoteFile) + localFile.getName()); } catch (final SftpException e) { throw new JSchException("failed to stat remote file", e); } FileUtils.getFileUtils().setFileLastModified(localFile, ((long) fileAttributes.getMTime()) * 1000); }
private File parseAndCreateDirectory(final String serverResponse, final File localFile) { int start = serverResponse.indexOf(' '); // appears that the next token is not used and it's zero. start = serverResponse.indexOf(' ', start + 1); final String directoryName = serverResponse.substring(start + 1); if (localFile.isDirectory()) { final File dir = new File(localFile, directoryName); dir.mkdir(); log("Creating: " + dir); return dir; } return null; }