简体   繁体   English

Apache Commons FTPClient无法检索某些文件

[英]Apache Commons FTPClient fails to retrieve some files

I'm using Apache Commons FTPClient to fetch files from FTP server. 我正在使用Apache Commons FTPClient从FTP服务器获取文件。 This is the setup: 这是设置:

 ftpClient.setDefaultPort(port);
            ftpClient.connect(server);
            ftpClient.login(user, password);
            ftpClient.enterLocalPassiveMode();
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
            ftpClient.changeWorkingDirectory(path);

This is the transfer code: 这是转移代码:

final FileOutputStream fileOutputStream = new FileOutputStream(tempFile);
                    final boolean result = ftpClient.retrieveFile(dirToList + aFile.getName(), fileOutputStream);
                    Log.i(TAG, "[" + (result ? "+" : "-") + "]");

And what I see in the logs: 我在日志中看到的内容:

I/SyncService( 4412): /Users/user1/Downloads/FtpSync/.idea/copyrightprofiles_settings.xml
I/SyncService( 4412): [-]
<...>
I/SyncService( 4412): /Users/user1/Downloads/FtpSync/footer.php
I/SyncService( 4412): [+]

All php files are synced, and all xml files are failed to sync. 所有php文件都已同步,并且所有xml文件都无法同步。 The FTP server is on my local notebook (Mac OS X default ftp server, tnftpd 20100324+GSSAPI) FTP服务器在我的本地笔记本上(Mac OS X默认ftp服务器,tnftpd 20100324 + GSSAPI)

Why it does not work? 为什么它不起作用?

For first, you should always close the output stream after the retrieveFile method. 首先,您应该始终在retrieveFile方法之后关闭输出流。 Have you tried to change the FTP.{filetype} when downloading XML files (although this shouldnt be the case)? 您是否尝试在下载XML文件时更改FTP。{filetype}(虽然不一定如此)?

I have had trouble downloading some large files with the retrieveFile method, where it would crash without throwing an exception. 我在使用retrieveFile方法下载一些大文件时遇到了麻烦,它会在不抛出异常的情况下崩溃。 In the end I used the retrieveFileStream method, which solved it for me. 最后我使用了retrieveFileStream方法,它为我解决了这个问题。

Replace 更换

status = mFTPClient.retrieveFile(srcFilePath, desFileStream);

With

// import org.apache.commons.io.IOUtils;

InputStream inputStream = mFTPClient.retrieveFileStream(srcFilePath);
IOUtils.copy(inputStream, desFileStream);
outputStream.flush();
IOUtils.closeQuietly(desFileStream);
IOUtils.closeQuietly(inputStream);

//status = mFTPClient.completePendingCommand();
status = true;

completePendingCommand crashed without throwing an exception for me, hence why it is commented out, but I think it is supposed to be called after completing a command. completePendingCommand崩溃而没有为我抛出异常,因此它被注释掉了,但我认为应该在完成一个命令后调用它。

use this code to download files. 使用此代码下载文件。

public boolean ftpDownload(String srcFilePath, String desFilePath)
    {
        boolean status = false;
        try {
            FileOutputStream desFileStream = new FileOutputStream(desFilePath);; //desfilepath where the file is to be stored
            status = mFTPClient.retrieveFile(srcFilePath, desFileStream);

            desFileStream.close();

            return status;
        } catch (Exception e) {
            Log.d(TAG, "download failed");
        }

        return status;
    } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM