Java – “connection not open” FTP Android

Every time I try to connect, there will be an exception of "connection not opened". When I try to access through FileZilla or browser, the server runs well

I am using the Apache common net library

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();
        }
    }
}

Implementation method:

 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;
}

journal:

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)

Therefore, I explored ftp.class and found the cause of the 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;
    }
}

I don't use worker threads. Should I? How should I implement it?

resolvent:

When you "try to connect", you get no exception. When you log out, you get it

I guess what actually happens is that the code in the try block throws an exception without an actual connection. Then, you try to log out of the finally block, which will throw an exception because you have never logged in

Remove FTP. Logout() and FTP. Disconnect() to avoid swallowing the main exception to see when it is your actual problem

When you do network work on GUI threads, it is likely to be a practical problem. The main exception can be networkonmainthreadexception. Check how do I fix android.os.networkonmainthreadexception?

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>