Package com.vcredit.ddcash.monitor.sendmail;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import Java.io.InputStream;
Import Java.io.OutputStream;
Import java.util.ArrayList;
Import java.util.List;
Import java.util.Properties;
Import Java.util.Vector;
Import Org.slf4j.Logger;
Import Org.slf4j.LoggerFactory;
Import Com.jcraft.jsch.Channel;
Import com.jcraft.jsch.ChannelSftp;
Import Com.jcraft.jsch.ChannelSftp.LsEntry;
Import Com.jcraft.jsch.JSch;
Import com.jcraft.jsch.Session;
/**
* Created by Ding 2016-12-02 17:28 Friday
* */
public class Ftpsfilelist {
private static final Logger LOG = Loggerfactory.getlogger (Ftpsfilelist.class);
public static void Main (string[] args) {
Listfilenames ("* * * *", "**", "root", "}KCXBZC6IO]HWC>CFDKGD<41WHTP (", "/usr");
}
private static list<string> Listfilenames (string host, int port, string username, final string password,
String dir) {
list<string> list = new arraylist<string> ();
Channelsftp sftp = null;
Channel channel = NULL;
Session sshsession = null;
try {
Jsch Jsch = new Jsch ();
Jsch.getsession (username, host, port);
Sshsession = Jsch.getsession (username, host, port);
Sshsession.setpassword (password);
Properties Sshconfig = new properties ();
Sshconfig.put ("stricthostkeychecking", "no");
Sshsession.setconfig (Sshconfig);
Sshsession.connect ();
Log.debug ("Session connected!");
Channel = Sshsession.openchannel ("sftp");
Channel.connect ();
Log.debug ("Channel connected!");
SFTP = (channelsftp) channel;
File upload this way you can also
File File = new file ("15888888_back_cut.jpg");
File.getabsolutepath ();
if (!file.exists ()) {
File.createnewfile ();
// }
FileInputStream in = new FileInputStream (file);
Sftp.put (In, "/usr/local/ding/15888888_back_cut.jpg1");
Sftp.put (SRC, DST);//src local files, files on the DST server
Sftp.put ("15888888_back_cut.jpg", "/usr/local/ding/");
File download
SFTP.CD ("/usr/local/ding/");
Sftp.get (SRC, DST); SRC for the file on the server, DST asks the local directory, if only the path, the default file name is the same
Sftp.get ("/usr/local/ding/15888888_back_cut.jpg", "d:/");
vector<?> vector = Sftp.ls (dir);
for (Object item:vector) {
Lsentry entry = (lsentry) item;
System.out.println (Entry.getfilename ());
}
} catch (Exception e) {
E.printstacktrace ();
} finally {
Closechannel (SFTP);
Closechannel (channel);
CloseSession (sshsession);
}
return list;
}
private static void Closechannel (Channel channel) {
if (channel! = NULL) {
if (channel.isconnected ()) {
Channel.disconnect ();
}
}
}
private static void CloseSession (session session) {
if (session! = NULL) {
if (session.isconnected ()) {
Session.disconnect ();
}
}
}
}
Jsch code example to implement file upload