Apache Commons FTP库 - retrieveFile函数下载失败

时间:2013-10-18 16:39:57

标签: java

你能不能给我一个暗示我做错了什么?调用retrieveFile后,此代码崩溃(异常e)。我正在尝试从IIS FTP(我可以成功连接)下载文件到SD卡(连接到模拟器的.iso文件)。文件'test.txt'存在于FTP的主目录中,printWorkingDirectory()显示为“/”。我确实将权限写入清单,并尝试没有.txt扩展名。

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。

0 个答案:

没有答案