Comment puis-je accéder à un serveur FTP avec JSch?

J'ai installé FileZilla Serveur FTP sur mon Windows 7 local de la machine.
J'ai aussi installé FileZilla FTP client sur la même machine.
La connexion est réussie entre les deux d'entre eux confirmant le serveur et le client de partenariat existe.

J'ai écrit un petit rapide et dirtry Jsch programme pour la connexion au serveur FileZilla FTP et ci-dessous le programme:

public class TestJSch {
/** Creates a new instance of TestCommonsNet */
public TestJSch() {
}
/**
* main - Unit test program
* 
* @param args
*            Command line arguments
* 
*/
public static void main(String[] args) {
try {
String ftpHost = "127.0.0.1";
int ftpPort = 21;//14147;
//int ftpPort = 990;//14147;
String ftpUserName = "kedar";
String ftpPassword = "XXXXXXXXXXX";
String ftpRemoteDirectory = "C:\\KEDAR\\Java\\FTP_Folder";
String fileToTransmit = "C:\\KEDAR\\Java\\File_Folder\\Customer.txt";
String identityfile = "C:\\KEDAR\\Java\\Ftp\\certificate.crt";
//
//First Create a JSch session
//
JSch.setLogger(new MyLogger());
System.out.println("Creating session.");
JSch jsch = new JSch();
String knownHostsFilename = "C:\\Windows\\System32\\drivers\\etc\\hosts";
jsch.setKnownHosts(knownHostsFilename);
jsch.addIdentity(identityfile);
Session session = null;
Channel channel = null;
ChannelSftp c = null;
//
//Now connect and SFTP to the SFTP Server
//
try {
//Create a session sending through our username and password
session = jsch.getSession(ftpUserName, ftpHost, ftpPort);
System.out.println("Session created.");
session.setPassword(ftpPassword);
//Security.addProvider(new com.sun.crypto.provider.SunJCE());
//b
//Setup Strict HostKeyChecking to no so we dont get the
//unknown host key exception
//
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
System.out.println("Session connected.");
//
//Open the SFTP channel
//
System.out.println("Opening Channel.");
channel = session.openChannel("sftp");
channel.connect();
c = (ChannelSftp) channel;
} catch (Exception e) {
System.err.println("Unable to connect to FTP server."
+ e.toString());
throw e;
}
//
//Change to the remote directory
//
System.out.println("Changing to FTP remote dir: "
+ ftpRemoteDirectory);
c.cd(ftpRemoteDirectory);
//
//Send the file we generated
//
try {
File f = new File(fileToTransmit);
System.out.println("Storing file as remote filename: "
+ f.getName());
c.put(new FileInputStream(f), f.getName());
} catch (Exception e) {
System.err
.println("Storing remote file failed." + e.toString());
throw e;
}   
//
//Disconnect from the FTP server
//
try {
c.quit();
} catch (Exception exc) {
System.err.println("Unable to disconnect from FTPserver. "
+ exc.toString());
}
} catch (Exception e) {
System.err.println("Error: " + e.toString());
}
System.out.println("Process Complete.");
System.exit(0);
}
public static class MyLogger implements com.jcraft.jsch.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);
}
}
}

J'ai essayé de lancer ce programme et ci-dessous le log FTP:

(000033)9/12/2011 13:08:53 PM - (non connecté) (127.0.0.1)> Connecté, l'envoi de message de bienvenue...
(000033)9/12/2011 13:08:53 - (non connecté) (127.0.0.1)> 220-FileZilla Server version 0.9.39 bêta
(000033)9/12/2011 13:08:53 - (non connecté) (127.0.0.1)> 220-écrit par Tim Kosse ([email protected])
(000033)9/12/2011 13:08:53 - (non connecté) (127.0.0.1)> 220 Veuillez visiter http://sourceforge.net/projects/filezilla/
(000033)9/12/2011 13:08:53 - (non connecté) (127.0.0.1)> SSH-2.0-JSCH-0.1.44
(000033)9/12/2011 13:08:53 - (non connecté) (127.0.0.1)> 500 erreur de Syntaxe, commande non reconnue.
(000033)9/12/2011 13:09:54 - (non connecté) (127.0.0.1)> 421 temps de Connexion dépassé. La fermeture de la connexion de contrôle.
(000033)9/12/2011 13:09:54 - (non connecté) (127.0.0.1)> déconnecté.

Je ne comprends pas pourquoi le JSch programme de l'émission SSH-2.0-JSCH-0.1.44 de commande et de la communication est alors tourné vers le bas. Comment faire pour éviter cela?

OriginalL'auteur Kedar Pulaparthi | 2011-09-12