ftp doesn't download the file properly in java?

ぐ巨炮叔叔 提交于 2019-12-05 22:18:45

问题


When i download file with following code it just write the file to destination on local but file size are all zero. Can anybody say Why this happen and how to fix it?

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import java.io.FileOutputStream;
import java.io.IOException;

public class FtpDownload {
public static void main(String[] args) {
    FTPClient client = new FTPClient();
    FileOutputStream fos = null;
    String filename = "config.zip";
    try {
        client.connect("ftpsrv");
        client.login("user", "user");

        for (FTPFile file : client.listFiles()) {
            filename = "C:\\tmp\\user\\" + file.getName();
            if (file.isFile()) {
                fos = new FileOutputStream(filename);
                client.retrieveFile(filename, fos);
                System.out.println(file.getName());
            } else if (file.isDirectory()) {
                System.out.println("directory: " + file.getName());
            }
        }

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (fos != null) {
                fos.close();
            }
            client.disconnect();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
}

回答1:


after searching in the Apache documentation i come to conclusion that BINARY_FILE_TYPE is not yet settled. i have add following code and everything is OK. thanks to all of you that did help.

try {
    client.connect(ftpServer);
    client.login(username, password);
    // following line fixed my zip file corruption issue.
    client.setFileType(FTP.BINARY_FILE_TYPE);



回答2:


You're using the local path variable to point to the remote file:

filename = "C:\\tmp\\user\\" + file.getName();
...
client.retrieveFile(filename, fos);

You want to point to the remote file, not the local destination, like this:

client.retrieveFile(file.getName(), fos);

Also, make sure you close your fos stream after reading each file, rather than right at the end. You can use IOUtils.closeQuietly(fos); for this if you get the commons.io library, which avoids the nested try-catch blocks.

You should also use BINARY file type and BLOCK transfer mode for transferring zip files:

client.setFileTransferMode(FTPClient.BLOCK_TRANSFER_MODE);
client.setFileType(FTPClient.BINARY_FILE_TYPE);



回答3:


try closing the stream on every iteration in the loop

fos.close();

Your code is not doing it correctly because you have a for loop that creates new stream every iteration but only close the last one



来源:https://stackoverflow.com/questions/9501681/ftp-doesnt-download-the-file-properly-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!