SFtpGetFile.java
package dk.topsecurity; import java.io.*; import java.lang.*; import com.sshtools.j2ssh.*; import com.sshtools.j2ssh.authentication.*; //Code to be used at own risk. // //Code by courtesy of : http://www.spindriftpages.net/blog/dave/2007/11/27/sshtools-j2ssh-java-sshsftp-library/comment-page-1/ // public class SFtpGetFile { public static void main(String[] argv) { String host = "127.0.0.1", userName="guest01", password="guest01", filePathRemote="/readme.1st", filePathLocal="readme.1st.local"; int port = 22; try { SshClient ssh = new SshClient(); ssh.connect(host, port); System.out.println("Authenticate"); PasswordAuthenticationClient passwordAuthenticationClient = new PasswordAuthenticationClient(); passwordAuthenticationClient.setUsername(userName); passwordAuthenticationClient.setPassword(password); int result = ssh.authenticate(passwordAuthenticationClient); if(result != AuthenticationProtocolState.COMPLETE){ throw new Exception("Login to " + host + ":" + port + " " + userName + "/" + password + " failed"); } System.out.println("Open the SFTP channel"); SftpClient client = ssh.openSftpClient(); System.out.println("Get the file"); client.get(filePathRemote,filePathLocal); System.out.println("disconnect"); client.quit(); ssh.disconnect(); } catch(IOException iox) { iox.printStackTrace(); } catch(Exception ex) { ex.printStackTrace(); } } } |
getFile.cmd
java -classpath .;commons-logging.jar;j2ssh-0.2.9-autoresume-patch.jar dk.topsecurity.SFtpGetFile |
SFtpClient.java
... public FileAttributes resume(String remote, String local, FileTransferProgress progress) throws IOException, TransferCancelledException { String remotePath = resolveRemotePath(remote); FileAttributes attrs = stat(remotePath); if (progress != null) { progress.started(attrs.getSize().longValue(), remotePath); } long resumeOffset = 0; File localFile = new File(local); RandomAccessFile randomAccessFile = new RandomAccessFile(localFile, "rw"); if(localFile.exists()) { resumeOffset = (int)randomAccessFile.length(); randomAccessFile.seek(resumeOffset); } SftpFileInputStream in = new SftpFileInputStream(sftp.openFile( remotePath, SftpSubsystemClient.OPEN_READ), resumeOffset); transferFile(in, randomAccessFile, progress); if (progress != null) { progress.completed(); } return attrs; } ... |
SftpFileInputStream.java
... public SftpFileInputStream(SftpFile file, long resumeOffset) throws IOException { if (file.getHandle() == null) { throw new IOException("The file does not have a valid handle!"); } if (file.getSFTPSubsystem() == null) { throw new IOException( "The file is not attached to an SFTP subsystem!"); } this.file = file; position = UnsignedInteger64.add(position, resumeOffset); } ... |
UnsignedInteger64.java
... public static UnsignedInteger64 add(UnsignedInteger64 x, long l) { return new UnsignedInteger64(x.bigInt.add(BigInteger.valueOf(l))); } ... |
SFtpResumeFile.java
package dk.topsecurity; import java.io.*; import java.lang.*; import com.sshtools.j2ssh.*; import com.sshtools.j2ssh.authentication.*; //Code to be used at own risk. // //Code by courtesy of : http://www.topsecurity.dk // public class SFtpResumeFile { public static void main(String[] argv) { String host = "127.0.0.1", userName="guest01", password="guest01", filePathRemote="/readme.1st", filePathLocal="readme.1st.local"; int port = 22; try { SshClient ssh = new SshClient(); ssh.connect(host, port); System.out.println("Authenticate"); PasswordAuthenticationClient passwordAuthenticationClient = new PasswordAuthenticationClient(); passwordAuthenticationClient.setUsername(userName); passwordAuthenticationClient.setPassword(password); int result = ssh.authenticate(passwordAuthenticationClient); if(result != AuthenticationProtocolState.COMPLETE){ throw new Exception("Login to " + host + ":" + port + " " + userName + "/" + password + " failed"); } System.out.println("Open the SFTP channel"); SftpClient client = ssh.openSftpClient(); System.out.println("Auto-resume the file from last position (creating from new if necessary)"); client.resume(filePathRemote,filePathLocal); System.out.println("disconnect"); client.quit(); ssh.disconnect(); } catch(IOException iox) { iox.printStackTrace(); } catch(Exception ex) { ex.printStackTrace(); } } } |
resumeFile.cmd
java -classpath .;commons-logging.jar;j2ssh-0.2.9-autoresume-patch.jar dk.topsecurity.SFtpResumeFile |
resumeFile.cmd output
2009-03-05 01:01:27 com.sshtools.j2ssh.transport.cipher.SshCipherFactory |