Chaque fois que j'essaie de me connecter, je reçois l'exception "La connexion n'est pas ouverte". Le serveur fonctionne bien lorsque j'essaie d'accéder à FileZilla ou au navigateur. J'utilise la bibliothèque apache-common-net."La connexion n'est pas ouverte" FTP Android
private Boolean downloadAndSaveFile(String server, int portNumber,
String user, String password, String filename, File localFile)
throws IOException {
FTPClient ftp = null;
try {
ftp = new FTPClient();
ftp.connect(server, portNumber);
ftp.login(user, password);
ftp.setFileType(FTP.BINARY_FILE_TYPE);
ftp.enterLocalPassiveMode();
OutputStream outputStream = null;
boolean success = false;
try {
outputStream = new BufferedOutputStream(new FileOutputStream(
localFile));
success = ftp.retrieveFile(filename, outputStream);
} finally {
if (outputStream != null) {
outputStream.close();
}
}
return success;
} finally {
if (ftp != null) {
ftp.logout();
ftp.disconnect();
}
}
}
Mise en œuvre:
private Boolean buttonDo(String atividade, int groupPosition, int childPosition) {
try {
downloadAndSaveFile(servidor, porta, user, password, filename, filelocation);
}catch (IOException e) {
Toast.makeText(_context,e.getMessage(),Toast.LENGTH_SHORT).show();
}
return false;
}
Log:
java.io.IOException: Connection is not open
at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:514)
at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:648)
at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:622)
at org.apache.commons.net.ftp.FTP.quit(FTP.java:904)
at org.apache.commons.net.ftp.FTPClient.logout(FTPClient.java:1148)
at com.joaobernardos.joaobernardo.gave.ExpandableListAdapter.downloadAndSaveFile(ExpandableListAdapter.java:124)
at com.joaobernardos.joaobernardo.gave.ExpandableListAdapter.buttonDo(ExpandableListAdapter.java:198)
at com.joaobernardos.joaobernardo.gave.ExpandableListAdapter.access$200(ExpandableListAdapter.java:34)
at com.joaobernardos.joaobernardo.gave.ExpandableListAdapter$1.onClick(ExpandableListAdapter.java:241)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19761)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5253)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Alors, je suis allé à explorer FTP.class et a découvert ce qui cause l'exception:
public int sendCommand(String command, String args) throws IOException {
if(this._controlOutput_ == null) {
throw new IOException("Connection is not open");
} else {
String message = this.__buildMessage(command, args);
this.__send(message);
this.fireCommandSent(command, message);
this.__getReply();
return this._replyCode;
}
}
Je Je n'utilise pas de thread de travail. Devrais-je? Comment puis-je l'implémenter?
'" La connexion n'est pas ouverte "'. C'est ton propre message. Votre propre exception. Cela n'a aucun sens de le mentionner. Mieux vaut nous dire ce qui se passe que vous le laissez jeter. – greenapps
Vous aurez aussi un NetworkOnMainThreadExeption. Vous devriez mettre tout le code ftp dans un thread ou asyncTask. – greenapps
@greenapps Ce n'est pas le propre message d'OP. C'est l'exception d'Apache Commons Net. –