我尝试从免费的ftp上的特定目录下载文件。虽然使用Filezilla很快,但是我的java程序很慢,不知道为什么。我试图增加Buffersize没有结果。我真的很无能,有没有人有想法?
非常感谢
private void DLAll() {
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(server, port);
boolean login=ftpClient.login(user, pass);
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.setBufferSize(1024000);
ftpClient.changeWorkingDirectory(remoteDIR);
FTPFile[] files=ftpClient.listFiles();
if(files != null && files.length >0)
{
number_file=files.length;
loop_index=0;
for(FTPFile fl:files)
{
if(loop_index<pLimit)
{
if(!fl.isFile())
{
continue;
}
System.out.println("Accessing:" + fl.getName());
//Download only zip files
if (fl.getName().endsWith("zip"))
{
// APPROACH #2: using InputStream retrieveFileStream(String)
String remoteFile2 = remoteDIR+"/"+fl.getName();
File downloadFile2 = new File(localDIR+fl.getName());
OutputStream outputStream2 = new BufferedOutputStream(new FileOutputStream(downloadFile2));
ftpClient.setBufferSize(1024*1024);
InputStream inputStream = ftpClient.retrieveFileStream(remoteFile2);
byte[] bytesArray = new byte[1024*1024];
int bytesRead = 0;
while ((bytesRead = inputStream.read(bytesArray)) != -1) {
outputStream2.write(bytesArray, 0, bytesRead);
}
boolean success = ftpClient.completePendingCommand();
if (success) {
System.out.println("File "+fl.getName()+" has been downloaded successfully." + ftpClient.getBufferSize());
}
outputStream2.close();
inputStream.close();
loop_index++;
}
}
else {
System.out.println("skipping:" + fl.getName());
}
}
}
}
} catch (IOException ex) {
System.out.println("Error: " + ex.getMessage());
ex.printStackTrace();
} finally {
try {
if (ftpClient.isConnected()) {
ftpClient.logout();
ftpClient.disconnect();}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
答案 0 :(得分:0)
好的,所以解决方案很奇怪地反转Outputstream.Close()和。首先关闭,然后让命令运行。我不知道为什么。我在这里找到了解决方案:https://coderanch.com/t/422797/java/InputStream-org-apache-commons-net
outputStream2.close();
inputStream.close();
loop_index++;
boolean success = ftpClient.completePendingCommand();
if (success) {
long estimatedTime = System.currentTimeMillis() - startTime;
System.out.println("File "+fl.getName()+" has been downloaded successfully in " + estimatedTime + "; Buffer:" + buffer_size);
}
答案 1 :(得分:-1)
您可以检查下载单个文件的各种缓冲区大小的延迟吗?如果它是相同的,那么你可能需要使用多线程实现多次下载(filezilla肯定会这样做)。尝试在FtpEntries上使用java 8并行流来快速实现。