我正在尝试使用在Android模拟器中运行的Java FTPSClient
从本地FileZilla服务器下载文件。
我已经编写了此帮助程序代码来下载一个文件:
public boolean downloadSingleFile(FTPSClient ftpClient,
String remoteFilePath, File downloadFile) {
OutputStream outputStream;
Log.i("t1", remoteFilePath + " - " + downloadFile.getAbsolutePath());
try {
outputStream = new BufferedOutputStream(new FileOutputStream(
downloadFile));
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
boolean retval = ftpClient.retrieveFile(remoteFilePath, outputStream);
outputStream.flush();
return retval;
} catch (Exception e) {
Log.e("dwFile", e.toString());
Log.e("dwFile", ftpClient.getReplyString());
} return false;
}
我这样称呼这个函数:
FTPSClient dwClient = new FTPSClient();
dwClient.addProtocolCommandListener(
new PrintCommandListener(
new PrintWriter(new OutputStreamWriter(System.out, "UTF-8")), true));
dwClient.setConnectTimeout(30 * 1000);
dwClient.connect(OmsSettingsFunctions.getFTPServer());
Log.i("dwDB", dwClient.getReplyString());
if (dwClient.login(FPTuser, FTPpass)) {
Log.i("dwDB", dwClient.getReplyString());
dwClient.enterLocalPassiveMode();
File dwFile = new File(externalPath + "/Android/data/com.myapp/files/Documents/db.temp");
if(!downloadSingleFile(dwClient, "/DBs/db.txt", dwFile)) {
Log.e("dwDB", "Download could not finish (DB)");
Log.e("dwDB", dwClient.getReplyString());
}...
但是我一直收到此错误:
I/System.out: 220-FileZilla Server version 0.9.41 beta
I/System.out: 220-written by Tim Kosse (Tim.Kosse@gmx.de)
220 Please visit http://sourceforge.net/projects/filezilla/
I/System.out: AUTH TLS
D/EGL_emulation: eglMakeCurrent: 0xa209dd60: ver 3 0 (tinfo 0x9f652ff0)
D/EGL_emulation: eglMakeCurrent: 0xa209dd60: ver 3 0 (tinfo 0x9f652ff0)
I/System.out: 234 Using authentication type TLS
I/dwDB: 234 Using authentication type TLS
I/Permission: Readingpermission is granted
I/Permission: Writingpermission is granted
I/System.out: USER *******
I/System.out: 331 Password required for omstest
I/System.out: PASS *******
I/System.out: 230 Logged on
I/dwDB: 230 Logged on
I/t1: /DBs/db.txt - /storage/0FF0-280B/Android/data/com.myapp/files/Documents/db.temp
I/System.out: TYPE I
I/System.out: 200 Type set to I
I/System.out: PASV
I/System.out: 227 Entering Passive Mode (127,0,0,1,199,113)
E/dwFile: java.net.ConnectException: Connection refused
227 Entering Passive Mode (127,0,0,1,199,113)
E/dwDB: Download could not finish (DB)
227 Entering Passive Mode (127,0,0,1,199,113)
我已经尝试使用enterLocalActivemode()
代替enterLocalPassivmode()
,但没有帮助。 FTP服务器是TLS强制的,并且在我的本地计算机上运行。我正在通过10.0.2.2(Android回送)连接到它。我该如何解决?
答案 0 :(得分:1)
虽然我对Android Emulator不熟悉,但我认为您需要连接到10.0.2.2才能连接到模拟器主机。
在FTP被动模式下,服务器发回FTP客户端需要连接到的IP地址以传输文件(或目录列表)。当您的FTP服务器侦听127.0.0.1时,它会发回该IP地址。但是在您的Android代码的上下文中,127.0.0.1指的是(模拟的)Android主机。因此,“连接被拒绝”。
这与连接到NAT后面的FTP服务器的常见问题非常相似。参见FTP server running on Port 2000 over NAT not working on Passive Mode
因此解决方案是相同的:
显然,这又使FTP服务器无法用于普通客户端。
您已正确评论,仅当从Android模拟器连接到在模拟器主机上运行的FTP服务器连接时,才会出现此问题。