How to show downloading progress by FTPClient(Apache commons-net component)
By:Roy.LiuLast updated:2024-04-28
When we use apache commons-net FTPClient component to download file from FTP server, How to show the progress?
we can use this method:
ftpClient.retrieveFile(source, outputStream);
But this method does't show the progress, so we should use another way as below:
OutputStream os = null; CountingOutputStream cos = null; try { os = Files.newOutputStream(Paths.get(destination)); cos = new CountingOutputStream(os) { protected void beforeWrite(int n){ super.beforeWrite(n); int downloadedSize = getCount(); double d = (double) downloadedSize / fileSize; int currentPercent = (int) (d * 100); if (currentPercent > previousPercent.get()) { previousPercent.set(currentPercent); log.info("\nDownloaded {}:" + getCount() + "/" + fileSize + " percent: {}%", source, currentPercent); } } }; ftpClient.enterLocalPassiveMode(); ftpClient.retrieveFile(source, cos); }catch (IOException e) { log.error("download file {} error", source, e); throw new RuntimeException(e); } finally { IOUtils.closeQuietly(os, cos); }
From:Is Everything OK
COMMENTS