Package com.hct.util;/** * @ HCT * @ time: December 29, 2016 3:13:20 * Description: * */import java.io.*;import Java.util.*;import Java.uti L.regex.matcher;import java.util.regex.pattern;import com.jcraft.jsch.*;p ublic class Upanddownfilesftp {/** * Using Jsch package to implement SFTP download, upload files (user name password mode login) * @param IP Host IP * @param user host login username * @param PSW Host Login password * @param port host SSH2 login port, if the default Value (default value 22), pass-1 * */public Session Connect (string ip, int port,string user, string psw) throws Exception{system.out.println ("Start user name password mode login"); Session session = NULL; Jsch Jsch = new Jsch (); if (port <=0) {//Connect server with default port session = jsch.getsession (user, IP);} The else{//uses the specified port to connect to the server session = jsch.getsession (user, IP, port);} If the server is not connected, throw an exception if (session = = null) {throw new Exception ("session is null");} Set the login host's password Session.setpassword (PSW);//Set password//Set prompt when first login, optional value: (Ask | yes | no) session.setconfig (" Stricthostkeychecking "," no ");//Set Login timeout time session.connect (30000); return session;} /** * @Title: Sftpuploadfile * @Description: The designated file specified in the following document to the remote designated item * @pAram Uploadfilename----The name of the file under the specified folder, eg,uploadfilename= "Trade_balance_file_20161220_301.txt"; * @param uploadfilepath----to the remote designated folder name, eg,uploadfilepath= "/alidata1/6080/share/20161222/301"; * @param UploadFile----from the local folder and file name, eg,uploadfile= "c:/users/hechangting/desktop/file/trade_balance_file_20161220_ 301.txt "; * @return void return type * @throws */public void Sftpuploadfile (Session session, String uploadfilename,string up Loadfilepath,string uploadfile) throws Exception {Channel channel = null;try {//create SFTP communication channels Channel = (channel) session.ope Nchannel ("sftp"); Channel.connect (1000); Channelsftp sftp = (channelsftp) channel;//enters the server-specified folder sftp.cd (uploadfilepath);//Lists the list of files specified by the server vector v = sftp.ls ("*.txt "); for (int i=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 be outputstream OutStream = Sftp.put (uploadfilename); InputStream instream = new FileInputStream (New File (UploadFile)); byte b[] = new Byte[1024];int n; while ((n = instream.read (b))! =-1) { Outstream.write (b, 0, N); } System.out.println ("upload file = = =" +uploadfilename+ "Success"); Outstream.flush (); Outstream.close (); Instream.close ();} catch (Exception e) {e.printstacktrace ();} finally {session.disconnect (); Channel.disconnect ();}} /** * @Title: Sftpdownloadfile * @Description: Download the specified file below the specified document * @param downloadfilename----To download the file to the local place, name what, Eg,downlo Adfilename= "C:/users/hechangting/desktop/file/hhhhh.txt"; * @param downloadfilepath----To download files from Cheng, eg,downloadfilepath= "/alidata1/6080/share/20161222/301"; * @param downloadfile----The name of the file to be downloaded from Cheng, eg,downloadfile= "Redemption_balance_confirm_file_20161222_301.txt"; * @return void return type * @throws */public void Sftpdownloadfile (Session session, String downloadfilename,string DOWNLOADFI Lepath, String downloadfile) throws Exception {Channel channel = null;try {//create SFTP communication channels Channel = (channel) session.opench Annel ("sftp"); Channel.connect (1000); Channelsftp sftp = (channelsftp) channel;//enters the server-specified folder sftp.cd (downloadfilepath);//Lists the list of files specified by the server vector v = sftp.ls ("*.txt"); for (int i = 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 be InputStream instream = Sftp.get (downloadfile); OutputStream OutStream = new FileOutputStream (New File (DownloadFileName)); byte b[] = new Byte[1024];int N;while ((n = instream.read (b))! =-1) {Outstre Am.write (b, 0, n);} System.out.println ("Download file" + DownloadFile + "Success!"); o Utstream.flush (); Outstream.close (); Instream.close ();} catch (Exception e) {e.printstacktrace ();} finally {session.disconnect (); Channel.disconnect ();}}}
Test code
Package Com.hct.util;import com.jcraft.jsch.session;/** * @ HCT * @ time: December 29, 2016 3:49:25 * Description: * */public class Testupa nddownfile {/** * @Title: Main * @Description: TODO (Describe the function of this method in a word) * @param parameter description * @return void return type * @thr OWS */public static void Main (string[] args) throws Exception {String ip= "10.139.108.102"; int port=22; String user= "6080"; String psw= "6080"; Session session = NULL; String uploadfilename= "Trade_balance_file_20161220_301.txt"; Upanddownfilesftp upanddownfile = new Upanddownfilesftp (); session = Upanddownfile.connect (IP, port, user, PSW); String uploadfilepath= "/alidata1/6080/share/20161222/301"; String uploadfile= "c:/users/hechangting/desktop/file/trade_balance_file_20161220_301.txt";// Upanddownfile.sftpuploadfile (Session, Uploadfilename, Uploadfilepath, UploadFile); String downloadfilename= "C:/users/hechangting/desktop/file/hhhhh.txt"; String downloadfilepath= "/alidata1/6080/share/20161222/301/"; String downloadfile= "redemption_balance_confirm_file_20161222_301.txt ";//upanddownfile.sftpdownloadfile (Session, DownloadFileName, Downloadfilepath, DownloadFile); String downloaddirectorypath= "/alidata1/6080/share/20161222/301/"; String downloadlocaldirectorypath= "c:/users/hechangting/desktop/file/"; Upanddownfile.sftpdownloaddirectory ( Session, Downloaddirectorypath, Downloadlocaldirectorypath);}}
Write uploads, downloads, and files via Jsch