JAVA:SSH connection server for local file upload and download

Source: Internet
Author: User
Tags lenovo

1. Connect to server: SSH [email protected]-P 5555

Download file: scp-r [email PROTECTED]:/CCC (server path, all files under folder)/path (local path)

Upload file: SCP/CCC (local file path)

2. File Upload:

ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.io.InputStream;ImportJava.io.OutputStream;ImportJava.util.Vector;Importcom.jcraft.jsch.ChannelSftp;ImportCom.jcraft.jsch.JSch;Importcom.jcraft.jsch.Session; Public classTest { Public Static voidMain (string[] args) {Try{sshsftp ("10.10.17.19", "Lenovo", "123456", 5555); } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); }    }     Public Static voidSshsftp (string IP, String user, String PSW,intPortthrowsexception{Session Session=NULL; CHANNELSFTP Channel=NULL; Jsch Jsch=NewJsch (); if(Port <=0){            //connect to server with default portSession =jsch.getsession (user, IP); }Else{            //connect the server with the specified portSession =jsch.getsession (user, IP, port); }        //throws an exception if the server is not connected        if(Session = =NULL) {            Throw NewException ("Session is null"); }                //Set the first login prompt, optional value: (Ask | yes | no)Session.setconfig ("stricthostkeychecking", "no"); //set the password for the login hostSession.setpassword (PSW);//Set Password//Setting the login time-out periodSession.connect (); Try {            //Create an SFTP communication channelChannel = (CHANNELSFTP) session.openchannel ("Sftp"); Channel.connect (1000); Channelsftp sftp=(CHANNELSFTP) channel; //Enter the folder specified by the serverSFTP.CD ("1047"); //lists the list of files specified by the serverVector v = sftp.ls ("*.txt");  for(intI=0;i<v.size (); i++) {System.out.println (V.get (i)); }                        //The following code implementation from the local upload a file to the server, if you want to implement the download, swap the following stream can beOutputStream OutStream = sftp.put ("666666666.txt"); InputStream instream=NewFileInputStream (NewFile ("C:/print.txt")); byteB[] =New byte[1024]; intN;  while((n = instream.read (b))! =-1) {Outstream.write (b,0, N);            } outstream.flush ();            Outstream.close ();        Instream.close (); } Catch(Exception e) {e.printstacktrace (); } finally{session.disconnect ();        Channel.disconnect (); }    }}    

3. File Download:

ImportJava.io.File;ImportJava.io.FileOutputStream;ImportJava.io.InputStream;ImportJava.io.OutputStream;ImportJava.util.Vector;Importcom.jcraft.jsch.ChannelSftp;ImportCom.jcraft.jsch.JSch;Importcom.jcraft.jsch.Session; Public classTest2 { Public Static voidMain (string[] args) {Try{sshsftp ("10.10.17.19", "Lenovo", "123456", 5555); } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); }    }     Public Static voidSshsftp (string IP, String user, String PSW,intPortthrowsexception{Session Session=NULL; CHANNELSFTP Channel=NULL; Jsch Jsch=NewJsch (); if(Port <=0){            //connect to server with default portSession =jsch.getsession (user, IP); }Else{            //connect the server with the specified portSession =jsch.getsession (user, IP, port); }        //throws an exception if the server is not connected        if(Session = =NULL) {            Throw NewException ("Session is null"); }                //Set the first login prompt, optional value: (Ask | yes | no)Session.setconfig ("stricthostkeychecking", "no"); //set the password for the login hostSession.setpassword (PSW);//Set Password//Setting the login time-out periodSession.connect (); Try {            //Create an SFTP communication channelChannel = (CHANNELSFTP) session.openchannel ("Sftp"); Channel.connect (1000); Channelsftp sftp=(CHANNELSFTP) channel; //Enter the folder specified by the serverSFTP.CD ("1047"); //lists the list of files specified by the serverVector v = sftp.ls ("*.txt");  for(intI=0;i<v.size (); i++) {System.out.println (V.get (i)); }                        //DownloadInputStream instream = Sftp.get ("666666666.txt"); OutputStream OutStream=NewFileOutputStream (NewFile ("C:/print.txt")); byteB[] =New byte[1024]; intN;  while((n = instream.read (b))! =-1) {Outstream.write (b,0, N);            } outstream.flush ();            Outstream.close ();        Instream.close (); } Catch(Exception e) {e.printstacktrace (); } finally{session.disconnect ();        Channel.disconnect (); }    }}

JAVA:SSH connection server for local file upload and download

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.