“连接未打开” FTP Android [英] "Connection is not open" FTP Android

查看:164
本文介绍了“连接未打开” FTP Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次尝试连接时,我都会遇到连接未打开异常。当我尝试访问FileZilla或浏览器时,服务器运行良好。



我使用apache-common-net库。

  private布尔值downloadAndSaveFile(字符串服务器,int端口号,
字符串用户,字符串密码,字符串文件名,文件localFile)
抛出IOException { b $ b FTPClient ftp = null;

尝试{
ftp = new FTPClient();
ftp.connect(server,portNumber);

ftp.login(user,password);

ftp.setFileType(FTP.BINARY_FILE_TYPE);

ftp.enterLocalPassiveMode();

OutputStream outputStream = null;
布尔成功= false;
try {
outputStream = new BufferedOutputStream(new FileOutputStream(
localFile));
success = ftp.retrieveFile(filename,outputStream);
} finally {
if(outputStream!= null){
outputStream.close();
}
}

返回成功;
} finally {
if(ftp!= null){
ftp.logout();
ftp.disconnect();



code
$ b

执行:

 私有布尔buttonDo(字符串atividade,INT groupPosition,INT childPosition){
尝试{
downloadAndSaveFile(servidor,PORTA,用户,密码,文件名,文件位置);

} catch(IOException e){
Toast.makeText(_context,e.getMessage(),Toast.LENGTH_SHORT).show();

}
返回false;

记录:

  java.io.IOException:连接未打开
,位于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)
处com.joaobernardos com.joaobernardos.joaobernardo.gave.ExpandableListAdapter.buttonDo(ExpandableListAdapter.java:198)
。 joaobernardo.gave.ExpandableListAdapter.access $ 200(ExpandableListAdapter.java:34)处android.view.View com.joaobernardos.joaobernardo.gave.ExpandableListAdapter $ 1.onClick(ExpandableListAdapter.java:241)

。 performClick(View.java:4756)
at android.view.View $ PerformClick.run(View.ja va: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)
在android.app.ActivityThread.main(ActivityThread.java:5253)
在java.lang.reflect.Method.invoke(本机方法)
。在java.lang.reflect.Method.invoke(Method.java:372)
在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:899)
。在COM .android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

所以,我去了探索FTP.class,并发现是什么导致异常:

  public int sendCommand(String command,String args)throws IOException { b $ b if(this._controlOutput_ == null){
抛出新的IOException(Connection is not open);
} else {
String message = this .__ buildMessage(command,args);
this .__ send(message);
this.fireCommandSent(command,message);
this .__ getReply();
返回this._replyCode;
}
}

我没有使用工作线程。我是不是该?如何实现它?

解决方案

当您尝试连接时, EM>。当你注销时,你会收到它。



我猜测,实际情况是中的代码尝试块抛出一个异常,而不实际连接。然后尝试注销最后块,抛出什么,因为您从未登录过。



删除 ftp.logout() ftp.disconnect(),以避免吞噬主要异常,以查看您的实际问题。

当你在GUI线程上做网络的时候,很有可能这是真正的问题。主要异常可能是 NetworkOnMainThreadExeption

请参阅如何我修复了android.os.NetworkOnMainThreadException?


Everytime I try to connect, I get the exception "Connection is not open". The server is working well when I try to access by FileZilla or browser.

I'm 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:

 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)

So, I went to explore FTP.class and discovered what's causing 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'm not using an Worker Thread. Should I? How can I implement it?

解决方案

You are not getting the exception, when you "try to connect". You are getting it, when you are logging out.

I'd guess that, what actually happens is that the code in try block throws an exception, without actually connecting. You then try to logout in the finally block, what throws, because you never logged in.

Remove the ftp.logout() and ftp.disconnect() to avoid swallowing a primary exception, to see when's your actual problem.

As you are doing network stuff on GUI thread, it's quite likely, that it's the actual problem. The primary exception can be NetworkOnMainThreadExeption.
See How do I fix android.os.NetworkOnMainThreadException?

这篇关于“连接未打开” FTP Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆