【问题标题】:Java FTP Download speedJava FTP 下载速度
【发布时间】:2018-01-26 22:30:31
【问题描述】:

我尝试从免费 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();
        }
    }
}

【问题讨论】:

  • 你做过分析吗?
  • 不,我该怎么做?

标签: java performance ftp


【解决方案1】:

好的,奇怪的是,解决方案是反转 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);
                               }

【讨论】:

    【解决方案2】:

    您能否检查下载不同缓冲区大小的单个文件的延迟?如果相同,那么您可能需要通过使用多线程(filezilla 肯定会这样做)来实现多个下载。尝试在 FtpEntries 上使用 java 8 并行流以快速实现。

    【讨论】:

    • 我该怎么做?我真的是java新手
    • Stream.of(files).parallel().forEach(file->{ //在这里添加你的下载代码})
    猜你喜欢
    • 1970-01-01
    • 2013-01-15
    • 2011-09-13
    • 1970-01-01
    • 2016-04-20
    • 2011-06-01
    • 2012-08-28
    • 1970-01-01
    • 2018-11-08
    相关资源
    最近更新 更多