J2SSH Maverick 一段连接sftp服务器,上传,下载的代码:

J2SSH Maverick
Version 1.2.6

一段连接sftp服务器,上传,下载的代码:
import com.maverick.ssh.*;
import com.maverick.ssh1.Ssh1Client;
import com.maverick.ssh2.*;
import java.io.*;
import com.sshtools.net.*;
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 {
     public static void main(String[] args) {
          final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
          String str = "user.home";

          try {
               System.out.print("Hostname: ");
              // 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);
               }
               System.out.print("Username [Enter for " + System.getProperty("user.name") + "]: ");
               //String username = reader.readLine();
               String username = "linuxborder";
               if(username==null || username.trim().equals(""))
                    username = System.getProperty("user.name");
               System.out.println("Connecting to " + hostname);
               /**
                * Create an SshConnector instance
                */
               SshConnector con = SshConnector.getInstance();
               // Lets do some host key verification
               con.getContext(SshConnector.SSH2).setHostKeyVerification(new ConsoleKnownHostsKeyVerification());
               // Set the preferred cipher to AES for best transfer performance
               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);
               SshClient ssh = con.connect(t, username);
               /**
                * 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");
               Ssh2Client ssh2 = (Ssh2Client)ssh;
               /**
                * Authenticate the user using password authentication
                */
               com.maverick.ssh.PasswordAuthentication pwd = new com.maverick.ssh.PasswordAuthentication();
               do {
                    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()) {
                   SftpClient sftp = new SftpClient(ssh2);
                   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);
                   File textFile = new File(System.getProperty("user.home"), "shining.txt");
                   FileOutputStream tout = new FileOutputStream(textFile);
                   // Create a file with \r\n as EOL
                   for(int i=0;i<5;i++) {
                       tout.write("All work and no play makes Jack a dull boy\r\n".getBytes());
                   }
                   tout.close();
                   // Tell the client which EOL the remote client is using - note
                   // that this will be ignored with version 4 of the protocol
                   sftp.setRemoteEOL(SftpClient.EOL_LF);
                   // Now put the file, the remote file should end up with all \r\n changed to \n
                   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();
                      FileOutputStream out = new FileOutputStream(f);
                      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();
                    sftp.lcd(f2.getAbsolutePath());
                    System.out.println("Getting file");
                    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();
          }
     }
}


你可能感兴趣的:(.net,F#,ssh,UP,performance)