Java FTPClient不会传输整个文件
我编写了一个java类,用于从FTP服务器下载文件。一切似乎都很好,但当我检查文件的大小时,它们比原始文件小一点 知道为什么会这样吗Java FTPClient不会传输整个文件,java,ftp,ftp-client,Java,Ftp,Ftp Client,我编写了一个java类,用于从FTP服务器下载文件。一切似乎都很好,但当我检查文件的大小时,它们比原始文件小一点 知道为什么会这样吗 import java.io.FileOutputStream; import java.io.BufferedOutputStream; import java.io.OutputStream; import java.io.IOException; import org.apache.commons.net.ftp.FTP; import org.apach
import java.io.FileOutputStream;
import java.io.BufferedOutputStream;
import java.io.OutputStream;
import java.io.IOException;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import java.io.IOException;
public class Downloader {
public static void main(String[] args) {
String server = "ftp.example.gov";
int port = 21;
String user = "anonymous";
String pass = "anonymous";
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(server, port);
ftpClient.login(user, pass);
ftpClient.cwd("dir1/dir2/gz");
FTPFile[] files = ftpClient.listFiles();
for (FTPFile file : files) {
String downloadFile = "/home/andrej/Documents/" + file.getName();
OutputStream output = new BufferedOutputStream(new FileOutputStream(downloadFile));
boolean success = ftpClient.retrieveFile(file.getName(), output);
output.close();
if (success) {
System.out.println(file.getName());
}
}
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
e.printStackTrace();
} finally {
try {
if (ftpClient.isConnected()) {
ftpClient.logout();
ftpClient.disconnect();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
尝试以二进制模式下载,您的差异可能是由于两台服务器之间的回车差异造成的。如果您的服务器是Windows,您将有CRLF,而Linux将只有新的行。ASCII模式下的FTP将自动为您进行转换
比较两个文件时,它们在文本方面是否相同?如果是这样,我认为您不必担心。在输出之前尝试output.flush()。close()将最后缓冲的字节写入文件。为我工作!FTP.BINARY\u文件类型没有。要小多少?可以同时包含两个文件大小吗?原始文件(gzip)的大小是20MB,而下载文件的大小是16MB。好吧,它可能没有4MB的回车符…ftpClient.setFileType(FTP.BINARY_file_TYPE)代码>行解决了这个问题。现在很有魅力。