1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
|
/* <!-- in case someone opens this in a browser... --> <pre> */
package Test;
import java.io.*;
import java.util.*;
/* This program/class is meant to test the functionality
* of the FTPConnection class.
*/
class TestFTP
{
public static void main (String[] args)
{
String serverName;
FTPConnection ftp = null;
try
{
if (args.length == 0)
{
serverName = "localhost";
//serverName = getStringFromUser("Enter the server you would like to connect to: ");
if (serverName.length() == 0) { return; }
} else {
serverName = args[0];
}
// set the FTPConnection parameter to true if you want to
// see debug output in your console window
ftp = new FTPConnection(false);
System.out.println("Connection à " + serverName);
ftp.connect(serverName);
if (ftp.login("GARDIEN", "admin"))
{
System.out.println("Authentification réussie!");
System.out.println("Le type de système est: " + ftp.getSystemType());
System.out.println("Le répertoire courant est: " + ftp.getCurrentDirectory());
String files = ftp.listFiles();
String subDirs = ftp.listSubdirectories();
System.out.println("Fichiers dans le répertoire:\n" + files);
System.out.println("Sous-répertoires:\n" + subDirs);
// try to change to the first subdirectory
StringTokenizer st = new StringTokenizer(subDirs, ftp.lineTerm);
String sdName = "//";
if (st.hasMoreTokens()) { sdName = st.nextToken(); }
if (sdName.length() > 0)
{
System.out.println("Changement de répertoire " + sdName);
if (ftp.changeDirectory(sdName))
{
// just for kicks, try to download the first 3 files in the directory
files = ftp.listFiles();
st = new StringTokenizer(files, ftp.lineTerm);
String fileName;
int count = 1;
while ((st.hasMoreTokens()) && (count < 3)) {
fileName = st.nextToken();
System.out.println("Téléchargement " + fileName + " de C:\\");
try
{
if (ftp.downloadFile(fileName, "C:\\Documents and Settings\\Jérémy GRESLON\\Mes documents\\Photos_telechargees\\" + fileName))
{
System.out.println("Téléchargement réussi!");
} else {
System.out.println("Erreur de téléchargement " + fileName);
}
} catch(Exception de) {
System.out.println("ERREUR: " + de.getMessage());
}
count++;
}
}
} else {
System.out.println("Il n'y a pas de sous répertoires!");
}
ftp.logout();
ftp.disconnect();
System.out.println("Déconnecté et déloggé.");
} else {
System.out.println("Désolé, impossible de se connecter au serveur.");
}
} catch(Exception e) {
e.printStackTrace();
try { ftp.disconnect(); } catch(Exception e2) {}
}
}
// private function that gets console input from the user
private static String getStringFromUser(String prompt) throws IOException
{
System.out.print(prompt);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
return br.readLine();
}
} |
Partager