"Connection is not open" FTP Android
"Connection is not open" FTP Android
每次尝试连接时,我都会遇到异常 "Connection is not open"。当我尝试通过 FileZilla 或浏览器访问时,服务器运行良好。
我正在使用 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.access0(ExpandableListAdapter.java:34)
at com.joaobernardos.joaobernardo.gave.ExpandableListAdapter.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;
}
}
我没有使用工作线程。我是不是该?我该如何实施?
确保发送正确的端口号。请与服务器管理员核实。端口不一定是 22,有时可能会为 FTP
设置不同的端口号
当您“尝试连接” 时,您没有遇到异常。当您注销时,您会得到它。
我猜想,实际发生的是,try
块中的代码抛出异常,而没有实际连接。然后您尝试在 finally
块中注销,抛出什么,因为您从未登录过。
删除 ftp.logout()
和 ftp.disconnect()
以避免吞下主要异常,看看什么时候是您的实际问题。
当您在 GUI 线程上执行网络操作时,很可能这是实际问题所在。主要例外可以是 NetworkOnMainThreadExeption
.
参见 How to fix 'android.os.NetworkOnMainThreadException'?
先注销 ftp.logout()
,然后 ftp.disconnect()
,对我有用:)
public boolean disconnectFromFtpServer() {
try {
ftpClient.logout();
ftpClient.disconnect();
return true;
} catch (IOException ioException) {
ioException.printStackTrace();
}
return false;
}
每次尝试连接时,我都会遇到异常 "Connection is not open"。当我尝试通过 FileZilla 或浏览器访问时,服务器运行良好。
我正在使用 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.access0(ExpandableListAdapter.java:34)
at com.joaobernardos.joaobernardo.gave.ExpandableListAdapter.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;
}
}
我没有使用工作线程。我是不是该?我该如何实施?
确保发送正确的端口号。请与服务器管理员核实。端口不一定是 22,有时可能会为 FTP
设置不同的端口号当您“尝试连接” 时,您没有遇到异常。当您注销时,您会得到它。
我猜想,实际发生的是,try
块中的代码抛出异常,而没有实际连接。然后您尝试在 finally
块中注销,抛出什么,因为您从未登录过。
删除 ftp.logout()
和 ftp.disconnect()
以避免吞下主要异常,看看什么时候是您的实际问题。
当您在 GUI 线程上执行网络操作时,很可能这是实际问题所在。主要例外可以是 NetworkOnMainThreadExeption
.
参见 How to fix 'android.os.NetworkOnMainThreadException'?
先注销 ftp.logout()
,然后 ftp.disconnect()
,对我有用:)
public boolean disconnectFromFtpServer() {
try {
ftpClient.logout();
ftpClient.disconnect();
return true;
} catch (IOException ioException) {
ioException.printStackTrace();
}
return false;
}