2017-04-20 203 views
2

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

我正在使用apache-common-net库。

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

实现:

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

登录:

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) 

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

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

我m不使用工作线程。我是不是该?我怎样才能实现它?

+0

''“连接未打开”'。这是你自己的信息。你自己的例外。没有意义提到这一点。最好告诉我们你发生了什么事。 – greenapps

+0

您也将拥有一个NetworkOnMainThreadExeption。你应该把所有的ftp代码放在一个线程或AsyncTask中。 – greenapps

+0

@greenapps这不是OP自己的信息。它是Apache Commons Net的例外。 –

回答

4

你没有得到例外,当你“试图连接”。当你注销时,你会得到它。

我猜想,实际情况是try块中的代码会引发异常,但实际上没有连接。然后,您尝试在finally块,什么抛出注销,因为你永远不登录。

取出ftp.logout()ftp.disconnect()避免吞咽主要的例外,看的时候是你的实际问题。

正如你在GUI线程上做网络的东西,很可能这是真正的问题。主要的例外可以是NetworkOnMainThreadExeption
请参阅How do I fix android.os.NetworkOnMainThreadException?

+0

这是我的问题。谢谢 – JotaB

-1

确保您发送正确的端口号。请检查它与服务器管理。这是没有必要,端口是22一段时间不同的端口号可能设置为FTP

+1

没有证据表明这是问题。 –