Jsch code example to implement file upload

Source: Internet
Author: User

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

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.