Etape 1 :Comme c'est assez simple et classique (connexion, login, configuration du mode) je mets directement un extrait du code ...
// on doit se connecter en FTP /users/toto/build/
ftpClient = new FTPClient();
try {
ftpClient.connect(host, Integer.valueOf(port));
ftpClient.login(username, password);
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
// Use passive mode as default because most of us are
// behind firewalls these days.
ftpClient.enterLocalPassiveMode();
ftpClient
.addProtocolCommandListener(new PrintCommandListener(
new PrintWriter(System.out)));
} catch (NumberFormatException e) {
e.printStackTrace();
System.exit(-1);
} catch (SocketException e) {
e.printStackTrace();
System.exit(-1);
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
Etape 2 :une fonction de copie (un appel a la méthode storeFile):
private static void copyFtpFile(String srcDirValue, String dstDirValue,
String fileName, FTPClient ftpClient) throws IOException {
FileInputStream local = new FileInputStream(srcDirValue + "/"
+ fileName);
String remote = dstDirValue + fileName;
remote = remote.replaceAll("\\\\", "/");
if (ftpClient.storeFile(remote, local)) {
/**
* -s D:\ArchCvs\BC\src -d D:\ArchCvs\V1-9-v1-patch\src -c
* D:\MesDocs\myProjet\listeFichiers.txt -f
* 192.168.161.16:21:dev:dev
*/
System.out.println("copy file [" + local.getFD().toString()
+ "] to [" + remote + "]");
} else {
System.err.println("erreur copy file [" + local + "] to [" + remote
+ "]");
}
local.close();
}
Le transfert est terminé ...
Aucun commentaire:
Enregistrer un commentaire