【问题标题】:Issue with an Android FTP ClientAndroid FTP 客户端的问题
【发布时间】:2017-07-28 09:33:01
【问题描述】:

我正在尝试通过 FTP 下载带有本地 Android 项目的文件
这是我的方法

@Override protected File doInBackground(Void... params) {

    File fileBar = new File(mLocalDir, mFileNameBar);

    boolean descargado = false;
    FTPClient ftpClient = null;
    try {
        ftpClient = new FTPClient();
        ftpClient.setConnectTimeout(10000);
        ftpClient.connect(SERVER, 21);
        ftpClient.enterLocalPassiveMode();
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
        if (ftpClient.login(mUser, mPass)) {

            ftpClient.changeWorkingDirectory("/Almacen");

            FileOutputStream fileOutputStreamBar;
            fileOutputStreamBar = new FileOutputStream(fileBar, false);
            OutputStream outputStreamBar = new BufferedOutputStream(fileOutputStreamBar);

            FileOutputStream fileOutputStreamPec;
            fileOutputStreamPec = new FileOutputStream(filePec, false);
            OutputStream outputStreamPec = new BufferedOutputStream(fileOutputStreamPec);


            descargado = ftpClient.retrieveFile(mFileNameBar, outputStreamBar);
        }
    } catch (IOException e) {
        System.out.println(e.toString());
        fileArt = null;

    } finally {
        if (ftpClient != null && ftpClient.isConnected()) try {
            ftpClient.logout();
        } catch (IOException ignored) {
        }
        if (ftpClient != null) try {
            ftpClient.disconnect();
        } catch (IOException ignored) {
        }
    }
    return (descargado ? fileArt : null);

}

该方法可以正常工作,或者至少我是这样认为的。它下载请求的文件,但不是占用 32Kb,而是占用 24Kb。 我不明白为什么会这样。如果有人能帮帮我,我将不胜感激

【问题讨论】:

    标签: java android ftp


    【解决方案1】:

    确保在函数返回之前优雅地关闭输出流。您可以尝试显式调用flush() 或close()。 您的方法似乎在保存所有数据之前返回。

    this answer

    【讨论】:

    • (我在努力,我不得不等待。)再次感谢^^
    猜你喜欢
    • 2011-11-13
    • 2013-02-15
    • 1970-01-01
    • 1970-01-01
    • 2012-03-20
    • 1970-01-01
    • 1970-01-01
    • 2018-03-02
    • 2015-02-27
    相关资源
    最近更新 更多