Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.InputStreamReader;
Import Java.io.LineNumberReader;
Import java.util.ArrayList;
Import java.util.List;
Import java.util.Properties;
Import Com.jcraft.jsch.Channel;
Import com.jcraft.jsch.ChannelSftp;
Import Com.jcraft.jsch.JSch;
Import com.jcraft.jsch.Session;
Import com.jcraft.jsch.SftpException;
public class Sftputil {private String host = "127.0.0.1";
Private String username= "Mingmac";
Private String password= "xxx";
private int port = 22;
Private channelsftp sftp = null;
Private String LocalPath = "/var/test";
Private String RemotePath = "/var/tset";
Private String Filelistpath = "/var/test/java/test.txt";
Private final String seperator = "/";
/** * Connect server via SFTP */public void Connect () {try {if (sftp!= null) {System.out.println ("SFTP is not null");
} Jsch Jsch = new Jsch ();
Jsch.getsession (username, host, port);
Session sshsession = Jsch.getsession (username, host, port);
System.out.println ("session created.");
Sshsession.setpassword (password);
Properties Sshconfig = new properties ();
Sshconfig.put ("stricthostkeychecking", "no");
Sshsession.setconfig (Sshconfig);
Sshsession.connect ();
System.out.println ("Session connected.");
System.out.println ("Opening Channel.");
Channel Channel = Sshsession.openchannel ("sftp");
Channel.connect ();
SFTP = (channelsftp) channel; System.out.println ("Connected to" + Host + ".");}
catch (Exception e) {e.printstacktrace ();
}/** * Disconnect with server */public void Disconnect () { if (this.sftp!= null) {if (this.sftp.isConnected ()) {this.sftp.disconnect ();
}else if (this.sftp.isClosed ()) {System.out.println ("SFTP is closed already");
}} public void Download () {//TODO auto-generated method stub}
private void Download (string directory, String downloadfile,string savefile, channelsftp sftp) {try {
SFTP.CD (directory);
File File = new file (savefile);
Sftp.get (DownloadFile, new FileOutputStream (file));
catch (Exception e) {e.printstacktrace (); }/** * Upload all of the files to the server */public void upload () {LIST<STRING&G T
FileList = This.getfileentrylist (Filelistpath); try {if (filelist!= null) {for (string filepath:filelist) {string Loca Lfile = This.localpath +this.seperator+ filepath;
File File = new file (LocalFile);
if (File.isfile ()) {System.out.println ("LocalFile:" + File.getabsolutepath ());
String remotefile = this.remotepath + this.seperator + filepath;
System.out.println ("RemotePath:" + remotefile);
File Rfile = new file (remotefile);
String rpath = Rfile.getparent ();
try {createdir (rpath, SFTP);
catch (Exception e) {System.out.println ("*******create path failed" + rpath);
} this.sftp.put (new FileInputStream (file), File.getname ());
System.out.println ("=========upload down for" + localfile); catch (filenotfoundexceptIon e) {//TODO auto-generated catch block E.printstacktrace ();
catch (Sftpexception e) {//TODO auto-generated catch block E.printstacktrace (); }/** * Create Directory * @param filepath * @param sftp/private void Create
Dir (String filepath, channelsftp sftp) {Boolean bcreated = false;
Boolean bparent = false;
File File = new file (filepath);
String Ppath = File.getparent ();
try {this.sftp.cd (Ppath);
Bparent = true;
catch (Sftpexception e1) {bparent = false;
try {if (bparent) {try {this.sftp.cd (filepath);
Bcreated = true;
catch (Exception e) {bcreated = false;
} if (!bcreated) {this.sftp.mkdir (filepath); Bcreated = true;
} return;
}else{Createdir (PPATH,SFTP);
THIS.SFTP.CD (Ppath);
This.sftp.mkdir (filepath);
The catch (Sftpexception e) {System.out.println ("MkDir failed:" + filepath);
E.printstacktrace ();
try {this.sftp.cd (filepath);
catch (Sftpexception e) {e.printstacktrace ();
System.out.println ("Can not CD into:" + filepath);
}/** * Get all of the files need to be upload or download * @param file * @return * * Private list<string> getfileentrylist (String file) {arraylist<string> filelist = new arraylist<
String> ();
InputStream in = null;
try {in = new FileInputStream (file);
InputStreamReader Inreader = new InputStreamReader (in);
LineNumberReader Linreader = new LineNumberReader (Inreader);
String filepath = Linreader.readline ();
while (filepath!= null) {Filelist.add (filepath);
filepath = Linreader.readline ();
} in.close ();
catch (FileNotFoundException e) {//TODO auto-generated catch block E.printstacktrace ();
catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();
}finally{if (in!= null) {in = null;
} return filelist;
}/** * @return the host */public String gethost () {return host;
/** * @param host to set */public void Sethost (String host) {this.host = host;
}/** * @return the username */public String GetUserName () {return username;
}
/** * @param username the username to set */public void Setusername (String username) {This.username = us
Ername;
}/** * @return the password */public String GetPassword () {return password; }/** * @param password the password to set */public void SetPassword (String password) {this.
Password = password;
}/** * @return the port */public int getport () {return port;
}/** * @param port the port to set */public void setport (int port) {this.port = port;
}/** * @return the SFTP * * * public channelsftp getsftp () {return sftp; }/** * @param sftp the SFTP to set */public void setsftp (Channelsftp sftp) {this.sftp = Sftp
;
}/** * @return the LocalPath */public String Getlocalpath () {return localpath; }/** * @param localpath the LocalPath to Set */public void Setlocalpath (String localpath) {this.localpath = LocalPath;
}/** * @return the RemotePath */public String Getremotepath () {return remotepath;
/** * @param remotepath the RemotePath to set */public void Setremotepath (String remotepath) {
This.remotepath = RemotePath;
}/** * @return the Filelistpath */public String Getfilelistpath () {return filelistpath; }/** * @param filelistpath the Filelistpath to set */public void Setfilelistpath (String filelistpath
) {This.filelistpath = Filelistpath; public static void Main (string[] args) {//TODO auto-generated method stub sftputil ftp= new S
Ftputil ();
Ftp.connect ();
Ftp.upload ();
Ftp.disconnect ();
System.exit (0);
}
}