try {
File file = new File(Environment.getExternalStorageDirectory(), "test2.txt");
Log.e(TAG, "1 " + Environment.getExternalStorageDirectory());
file.mkdir();
FileOutputStream desFileStream = new FileOutputStream(file);
Log.e(TAG, "2 " + Environment.getExternalStorageDirectory());
status = mFTPClient.retrieveFile("/"+"test.txt", desFileStream);
Log.e(TAG, "3");
desFileStream.close();
return status;
} catch (Exception e) {
Log.d(TAG, "download failed");
}
10-20 18:02:03.498:E /(1380):1 / storage / sdcard
10-20 18:02:03.507:E /(1380):2 / storage / sdcard
10-20 18:02:03.777:W / System.err(1380):java.net.ConnectException:无法连接到/127.0.0.1(端口63564):连接失败:ECONNREFUSED(连接被拒绝)
10-20 18:02:03.816:W / System.err(1380):at libcore.io.IoBridge.connect(IoBridge.java:114)
10-20 18:02:03.816:W / System.err(1380):at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
10-20 18:02:03.847:W / System.err(1380):at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
10-20 18:02:03.847:W / System.err(1380):at java.net.Socket.startupSocket(Socket.java:566)
10-20 18:02:03.867:W / System.err(1380):at java.net.Socket.tryAllAddresses(Socket.java:127)
10-20 18:02:03.887:W / System.err(1380):at java.net.Socket。(Socket.java:177)
10-20 18:02:03.917:W / System.err(1380):at java.net.Socket。(Socket.java:149)
10-20 18:02:03.967:W / System.err(1380):at javax.net.DefaultSocketFactory.createSocket(DefaultSocketFactory.java:40)
10-20 18:02:03.988:W / System.err(1380):at org.apache.commons.net.ftp.FTPClient。 openDataConnection (FTPClient.java:502)
10-20 18:02:04.029:W / System.err(1380):at org.apache.commons.net.ftp.FTPClient.retrieveFile(FTPClient.java:1276)
10-20 18:02:04.083:W / System.err(1380):at com.prgguru.android.FTP.ftpDownload(FTP.java:170)
10-20 18:02:04.156:W / System.err(1380):at com.prgguru.android.Caller5.run(Caller5.java:28)
10-20 18:02:04.156:W / System.err(1380):引起:libcore.io.ErrnoException:连接失败:ECONNREFUSED(连接被拒绝)
10-20 18:02:04.177:W / System.err(1380):at libcore.io.Posix.connect(Native Method)
10-20 18:02:04.177:W / System.err(1380):at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
10-20 18:02:04.197:W / System.err(1380):at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
10-20 18:02:04.197:W / System.err(1380):at libcore.io.IoBridge.connect(IoBridge.java:112)
10-20 18:02:04.217:W / System.err(1380):... 11更多
10-20 18:02:04.217:E /(1380):下载失败
这里我如何在调用retrieveFile之前连接到FTP:
public static boolean ftpConnect(String host, String username,
String password, int port) {
try {
mFTPClient = new FTPClient();
mFTPClient.connect(host, port);
if (FTPReply.isPositiveCompletion(mFTPClient.getReplyCode())) {
boolean status = mFTPClient.login(username, password);
mFTPClient.enterLocalPassiveMode();
mFTPClient.setFileTransferMode(org.apache.commons.net.ftp.FTP.BINARY_FILE_TYPE);
return status;
}
} catch (Exception e) {
Log.d(TAG, "Error: could not connect to host " + host);
}
return false;
}
status为数据返回TRUE:
private static String host = "10.0.2.2";
private static String user = "MyCorrectNickname";
private static String password = "CorrectPassword";
尝试了错误的密码 - 返回false。