亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 Android 模擬器中從本地 FTP 服務器下載時出現錯誤

在 Android 模擬器中從本地 FTP 服務器下載時出現錯誤

繁花不似錦 2021-08-19 17:50:10
我正在嘗試FTPSClient使用在 Android 模擬器中運行的Java 從本地 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());    }...我已經嘗試使用enterLocalActivemode()而不是,enterLocalPassivmode()但它沒有幫助。FTP 服務器在我的本地機器上執行和運行 TLS。我通過 10.0.2.2(Android 環回)連接到它。我怎樣才能解決這個問題?
查看完整描述

1 回答

?
慕姐8265434

TA貢獻1813條經驗 獲得超2個贊

雖然我不熟悉 Android Emulator,但我假設您需要連接到 10.0.2.2 才能連接到模擬器主機。

在 FTP 被動模式下,服務器發回一個 IP 地址,FTP 客戶端需要連接到該地址來傳輸文件(或目錄列表)。當您的 FTP 服務器偵聽 127.0.0.1 時,它會發回該 IP 地址。但是 127.0.0.1 指的是(模擬的)Android 主機,在您的 Android 代碼上下文中。因此,“連接被拒絕”。

這與連接到 NAT 后的 FTP 服務器的常見問題非常相似。請參閱通過 NAT 在端口 2000 上運行的 FTP 服務器無法在被動模式下工作

因此解決方案是相同的:

  • 在 FileZilla 服務器界面中,轉到編輯 > 設置 > 被動模式設置 > IPv4 特定 > 被動模式傳輸的外部服務器 IP 地址。并輸入 10.0.2.2。

  • 也許您還需要取消選中“不要將外部 IP 用于本地連接”。

顯然,這反過來又使普通客戶端無法使用 FTP 服務器。

并且您已正確評論,此問題僅在從 Android 模擬器連接到在模擬器主機上運行的 FTP 服務器時出現。


另一種解決方案是使用FTPClient.setPassiveNatWorkaroundStrategy. 它接受HostnameResolver接口的實現。如果您以將 127.0.0.1 轉換為 10.0.2.2 的方式實現,即使服務器上沒有任何更改,它也將允許您的 Java 代碼連接。

public static class ServerResolverImpl implements HostnameResolver {

    private FTPClient client;


    public ServerResolverImpl(FTPClient client) {

        this.client = client;

    }


    @Override

    public String resolve(String hostname) throws UnknownHostException {

        return this.client.getRemoteAddress().getHostAddress();

    }

}


查看完整回答
反對 回復 2021-08-19
  • 1 回答
  • 0 關注
  • 327 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號