J2SSH Maverick
Version 1.2.6
一段连接sftp服务器,上传,下载的代码:import com.maverick.ssh1.Ssh1Client;
import com.maverick.ssh2.*;
import java.io.*;
import com.sshtools.sftp.*;
import com.maverick.sftp.*;
import com.sshtools.publickey.ConsoleKnownHostsKeyVerification;
* This example demonstrates the connection process connecting to an SSH2 server and
* usage of the SFTP client.
*
* @author Lee David Painter
* @version $Id: SftpConnect.java,v 1.8 2006/01/30 17:46:04 lee Exp $
*
*/
public class SftpConnect {
String str = "user.home";
try {
// String hostname = reader.readLine();
String hostname = "192.168.1.237";
int idx = hostname.indexOf(':');
int port = 22;
if(idx > -1) {
port = Integer.parseInt(hostname.substring(idx+1));
hostname = hostname.substring(0, idx);
//String username = reader.readLine();
String username = "linuxborder";
if(username==null || username.trim().equals(""))
username = System.getProperty("user.name");
* Create an SshConnector instance
*/
SshConnector con = SshConnector.getInstance();
Ssh2Context ssh2Context = (Ssh2Context)con.getContext(SshConnector.SSH2);
ssh2Context.setPreferredPublicKey(Ssh2Context.PUBLIC_KEY_SSHDSS);
ssh2Context.supportedCiphers().add("aes128-cbc", com.sshtools.cipher.AES128Cbc.class);
ssh2Context.setPreferredCipherCS("aes128-cbc");
ssh2Context.setPreferredCipherSC("aes128-cbc");
* Connect to the host
*/
SocketTransport t = new SocketTransport(hostname, port);
t.setTcpNoDelay(true);
* Determine the version
*/
if(ssh instanceof Ssh1Client) {
System.out.println(hostname + " is an SSH1 server!! SFTP is not supported");
ssh.disconnect();
System.exit(0);
}
else
System.out.println(hostname + " is an SSH2 server");
/**
* Authenticate the user using password authentication
*/
com.maverick.ssh.PasswordAuthentication pwd = new com.maverick.ssh.PasswordAuthentication();
System.out.print("Password: ");
pwd.setPassword("linuxborder");
// pwd.setPassword(reader.readLine());
}
while(ssh2.authenticate(pwd)!=SshAuthentication.COMPLETE
&& ssh.isConnected());
* Start a session and do basic IO
*/
if(ssh.isAuthenticated()) {
System.out.println("******************************************");
System.out.println(" sftp.pwd() " +sftp.pwd());
sftp.mkdir("netsky");
System.out.println(" sftp.pwd() " +sftp.getAbsolutePath("netsky"));
sftp.cd("netsky");
* Perform some text mode operations
*/
sftp.setTransferMode(SftpClient.MODE_TEXT);
FileOutputStream tout = new FileOutputStream(textFile);
for(int i=0;i<5;i++) {
tout.write("All work and no play makes Jack a dull boy\r\n".getBytes());
}
// that this will be ignored with version 4 of the protocol
sftp.setRemoteEOL(SftpClient.EOL_LF);
sftp.put(textFile.getAbsolutePath());
* Now perform some binary operations
*/
sftp.setTransferMode(SftpClient.MODE_BINARY);
* List the contents of the directory
*/
SftpFile[] ls = sftp.ls();
for(int i=0;i<ls.length;i++) {
System.out.println(ls[i].toString());
System.out.println(SftpClient.formatLongname(ls[i]));
}
/**
* Generate a temporary file for uploading/downloading
*/
File f = new File(System.getProperty(str), "sftp-file");
java.util.Random rnd = new java.util.Random();
byte[] buf = new byte[1024];
for(int i=0;i<30;i++) {
rnd.nextBytes(buf);
out.write(buf);
}
out.close();
/**
* Create a directory
*/
sftp.mkdirs("sftp/test-files");
* Change directory
*/
sftp.cd("sftp/test-files");
* Put a file into our new directory
*/
long length = f.length();
System.out.println("Putting file");
long t1 = System.currentTimeMillis();
sftp.put(f.getAbsolutePath());
long t2 = System.currentTimeMillis();
System.out.println("Completed.");
long e = t2-t1;
System.out.println("Took " + String.valueOf(e) + " milliseconds");
float secs, kbs;
if(e >= 1000) {
kbs = ((float)length / 1024) / ((float)e / 1000);
System.out.println("Upload Transfered at " + String.valueOf(kbs) +
" kbs");
}
/**
* Get the attributes of the uploaded file
*/
System.out.println("Getting attributes of the remote file");
SftpFileAttributes attrs = sftp.stat(f.getName());
System.out.println(SftpClient.formatLongname(attrs, f.getName()));
* Download the file inot a new location
*/
File f2 = new File(System.getProperty(str), "downloaded");
f2.mkdir();
t1 = System.currentTimeMillis();
sftp.get(f.getName());
t2 = System.currentTimeMillis();
System.out.println("Completed.");
e = t2-t1;
System.out.println("Took " + String.valueOf(e) + " milliseconds");
if(e >= 1000) {
kbs = ((float)length / 1024) / ((float)e / 1000);
System.out.println("Download Transfered at " + String.valueOf(kbs) +
" kbs");
}
/**
* Set the permissions on the file and check they were changed
* they should be -rw-r--r--
*/
sftp.chmod(0644, f.getName());
attrs = sftp.stat(f.getName());
System.out.println(SftpClient.formatLongname(attrs, f.getName()));
System.out.println("******************************************");
System.out.println(" sftp.pwd() " + sftp.pwd());
sftp.quit();
ssh.disconnect();
}
} catch(Throwable th) {
th.printStackTrace();
}
}
5078




被折叠的 条评论
为什么被折叠?



