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