Vous êtes sur la page 1sur 3

package example; import import import import import import import import import import import import import

java.io.ByteArrayInputStream; java.io.File; java.io.FileInputStream; java.io.IOException; java.io.InputStream; java.util.Iterator; java.util.Properties; java.util.Vector; com.jcraft.jsch.Channel; com.jcraft.jsch.ChannelSftp; com.jcraft.jsch.JSch; com.jcraft.jsch.Logger; com.jcraft.jsch.Session;

/** * This Demo will try connect to a given sftp server, list all directories and * files in the home directory and exit. * * @author Sander De Vos * */ public class SftpDemo { private private private private e_openssh"; static static static static final final final final String HOST = "127.0.0.1"; int PORT = 22; String USER = "test"; String PRIVATE_KEY_LOCATION = "C:\\sftpkeys\\privat

public static void main(String[] args) throws Exception { JSch.setLogger(new MyJschLogger()); /* * JSch manages identities and the creation of sessions. */ JSch jSch = new JSch(); final byte[] privateKey = getPrivateKeyAsByteStream(); final byte[] password = "certPass".getBytes(); jSch.addIdentity(USER, // String userName privateKey, // byte[] privateKey null, // byte[] publicKey password // byte[] passPhrase ); /* * Create a session, but don't check the hostkey. TODO: Don't us e this * ever outside tests. You should always need to make sure the s erver is * known to you (known_hosts). */ Session session = jSch.getSession(USER, HOST, PORT); Properties config = new Properties(); config.put("StrictHostKeyChecking", "no"); session.setConfig(config); session.connect();

Channel channel = session.openChannel("sftp"); ChannelSftp sftp = (ChannelSftp) channel; sftp.connect(); final Vector files = sftp.ls("."); Iterator itFiles = files.iterator(); while (itFiles.hasNext()) { System.out.println("Index: " + itFiles.next()); } final ByteArrayInputStream in = new ByteArrayInputStream( "This is a sample text".getBytes()); sftp.put(in, "test.txt", ChannelSftp.OVERWRITE); sftp.disconnect(); session.disconnect(); } /** * Gets the private key to use to authenticate the user as a byte stream and * returns it. * * @return the private key (openSSH) * @throws IOException * when file not found */ private static byte[] getPrivateKeyAsByteStream() throws IOException { final File privKeyLocation = new File(PRIVATE_KEY_LOCATION); InputStream is = new FileInputStream(privKeyLocation); long length = privKeyLocation.length(); if (length > Integer.MAX_VALUE) { throw new IOException( "File to process is too big to process i n this example."); } final byte[] bytes = new byte[(int) length]; // Read in the bytes int offset = 0; int numRead = 0; while ((offset < bytes.length) && ((numRead = is.read(bytes, offset, bytes.leng th - offset)) >= 0)) { offset += numRead; } // Ensure all the bytes have been read in if (offset < bytes.length) { throw new IOException("Could not completely read file " + privKeyLocation.getName()); } is.close(); return bytes;

} /** * Simple System logger. Nice to know what's going on internally. All ou tput * sent to System errorstream. * * @author Sander De Vos * */ static class MyJschLogger implements Logger { static java.util.Hashtable name = new java.util.Hashtable(); static { name.put(new Integer(DEBUG), "DEBUG: "); name.put(new Integer(INFO), "INFO: "); name.put(new Integer(WARN), "WARN: "); name.put(new Integer(ERROR), "ERROR: "); name.put(new Integer(FATAL), "FATAL: "); } public boolean isEnabled(int level) { return true; } public void log(int level, String message) { System.err.print(name.get(new Integer(level))); System.err.println(message); } } }