Untitled
unknown
plain_text
2 years ago
3.8 kB
6
Indexable
import org.apache.http.HttpEntity; import org.apache.http.HttpEntityWrapper; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import java.io.FilterInputStream; import java.io.IOException; import java.io.InputStream; import java.text.DecimalFormat; public class ProgressExample { private static final int UPDATE_INTERVAL = 1000; // Update interval in milliseconds public static void main(String[] args) throws IOException { CloseableHttpClient httpClient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet("https://example.com/file.txt"); HttpResponse response = httpClient.execute(httpGet); HttpEntity entity = response.getEntity(); ProgressHttpEntityWrapper entityWrapper = new ProgressHttpEntityWrapper(entity); InputStream inputStream = entityWrapper.getContent(); byte[] buffer = new byte[1024]; int bytesRead; long bytesReadTotal = 0; long contentLength = entity.getContentLength(); long startTime = System.currentTimeMillis(); long lastUpdateTime = startTime; while ((bytesRead = inputStream.read(buffer)) != -1) { // Update progress bytesReadTotal += bytesRead; long currentTime = System.currentTimeMillis(); long elapsedTime = currentTime - startTime; if (currentTime - lastUpdateTime >= UPDATE_INTERVAL) { double progress = (double) bytesReadTotal / contentLength * 100; double downloadSpeed = (double) bytesReadTotal / elapsedTime * 1000; // Bytes per second // Print progress, download size, current speed, and estimated time DecimalFormat decimalFormat = new DecimalFormat("#.##"); System.out.println("Progress: " + decimalFormat.format(progress) + "%"); System.out.println("Downloaded: " + bytesReadTotal + " bytes"); System.out.println("Download Speed: " + decimalFormat.format(downloadSpeed / 1024) + " KB/s"); if (downloadSpeed > 0) { double estimatedTime = (contentLength - bytesReadTotal) / downloadSpeed; System.out.println("Estimated Time: " + decimalFormat.format(estimatedTime) + " seconds"); } System.out.println(); lastUpdateTime = currentTime; } // Process the downloaded data // ... // You can also throttle the download speed if needed // Thread.sleep(100); } httpClient.close(); } static class ProgressHttpEntityWrapper extends HttpEntityWrapper { public ProgressHttpEntityWrapper(HttpEntity wrappedEntity) { super(wrappedEntity); } @Override public InputStream getContent() throws IOException { return new ProgressFilterInputStream(super.getContent()); } } static class ProgressFilterInputStream extends FilterInputStream { protected ProgressFilterInputStream(InputStream in) { super(in); } @Override public int read(byte[] b) throws IOException { int bytesRead = super.read(b); displayProgress(bytesRead); return bytesRead; } @Override public int read(byte[] b, int off, int len) throws IOException { int bytesRead = super.read(b, off, len); displayProgress(bytesRead); return bytesRead; } private void displayProgress(int bytesRead) { // You can update your progress here } } }
Editor is loading...