Récupérer un fichier d'un serveur ftp
Bonjour,
Voici mon problème, j'utilise actuellement la librairie de apache pour accèder à mon serveur ftp. Mais voila, envoyer un fichier sur le serveur marche sans problème à contrario, prendre un fichier sur le serveur ne fonctionne pas. Ca me créé un fichier vide au lieu de récupérer le fichier demander.
Voici le code de ma class FTP :
Code:
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
|
package fr.projet.outilprofilmobile;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.SocketException;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
public class MonFTP {
public static FTPClient ftp;
public static void envoyer(String chemin, String nomFichier) {
try {
int reply;
InputStream in = null;
ftp = new FTPClient();
ftp.enterLocalPassiveMode();
ftp.connect("ftpperso.free.fr");;
ftp.login("monlog", "monmdp");
System.out.println("Connected to server.");
System.out.print(ftp.getReplyString());
ftp.setFileType(FTP.ASCII_FILE_TYPE);
// After connection attempt, you should check the reply code to verify
// success.
reply = ftp.getReplyCode();;
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
System.exit(1);
}
//File f1 = new File("prout.xml");
File f1 = new File(chemin+"/"+nomFichier+".xml");
in = new BufferedInputStream (new FileInputStream(f1),8);
ftp.storeFile(nomFichier+"2.xml", in);
in.close();
ftp.logout();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void receptionner(String chemin, String nomFichier) {
try {
int reply;
OutputStream out = null;
ftp = new FTPClient();
ftp.enterLocalPassiveMode();
ftp.connect("ftpperso.free.fr");
ftp.login("monlog", "monmdp");
System.out.println("Connected to server.");
System.out.print(ftp.getReplyString());
ftp.setFileType(FTP.ASCII_FILE_TYPE);
// After connection attempt, you should check the reply code to verify
// success.
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
System.err.println("FTP server refused connection.");
System.exit(1);
}
//File f1 = new File(chemin+"/"+nomFichier+".xml");
File f1 = new File(chemin+"/"+nomFichier+".xml");
out = new BufferedOutputStream(new FileOutputStream(f1));
//ftp.retrieveFile(nomFichier+".xml", out);
ftp.retrieveFile(nomFichier+".xml", out);
ftp.logout();
} catch (IOException e) {
e.printStackTrace();
}
}
} |
Si quelqu'un pouvait m'expliquer se qui irait pas dans ce code.
Merci d'avance.
Cordialement MasterSkimo