ftp doesn#39;t download the file properly in java?(ftp 不能在 java 中正确下载文件?)
问题描述
当我使用以下代码下载文件时,它只是将文件写入本地目标,但文件大小全部为零.谁能说出为什么会发生这种情况以及如何解决?
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();
}
}
}
}
推荐答案
在 Apache 文档中搜索后,我得出结论 BINARY_FILE_TYPE 尚未解决.我添加了以下代码,一切正常.感谢所有提供帮助的人.
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);
这篇关于ftp 不能在 java 中正确下载文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:ftp 不能在 java 中正确下载文件?
基础教程推荐
- 修改 void 函数的输入参数,然后读取 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- 如何对 Java Hashmap 中的值求和 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- Struts2 URL 无法访问 2022-01-01
