|
20 | 20 | package com.cloud.utils.net; |
21 | 21 |
|
22 | 22 | import java.io.BufferedReader; |
23 | | -import java.io.FileOutputStream; |
24 | 23 | import java.io.IOException; |
25 | | -import java.io.InputStream; |
26 | 24 | import java.io.InputStreamReader; |
27 | 25 | import java.math.BigInteger; |
28 | | -import java.net.HttpURLConnection; |
29 | 26 | import java.net.Inet4Address; |
30 | 27 | import java.net.Inet6Address; |
31 | 28 | import java.net.InetAddress; |
|
34 | 31 | import java.net.SocketException; |
35 | 32 | import java.net.URI; |
36 | 33 | import java.net.URISyntaxException; |
37 | | -import java.net.URL; |
38 | 34 | import java.net.UnknownHostException; |
39 | 35 | import java.util.ArrayList; |
40 | 36 | import java.util.Arrays; |
|
55 | 51 | import org.apache.commons.net.util.SubnetUtils; |
56 | 52 | import org.apache.commons.validator.routines.InetAddressValidator; |
57 | 53 | import org.apache.commons.validator.routines.RegexValidator; |
58 | | -import org.apache.logging.log4j.LogManager; |
59 | 54 | import org.apache.logging.log4j.Logger; |
| 55 | +import org.apache.logging.log4j.LogManager; |
60 | 56 |
|
61 | 57 | import com.cloud.utils.IteratorUtil; |
62 | 58 | import com.cloud.utils.Pair; |
@@ -1878,51 +1874,4 @@ public static String transformCidr(final String cidr) { |
1878 | 1874 | final long start = (ip & startNetMask); |
1879 | 1875 | return String.format("%s/%s", long2Ip(start), size); |
1880 | 1876 | } |
1881 | | - |
1882 | | - public static boolean downloadFileWithProgress(final String fileURL, final String savePath, final Logger logger) { |
1883 | | - HttpURLConnection httpConn = null; |
1884 | | - try { |
1885 | | - URL url = new URL(fileURL); |
1886 | | - httpConn = (HttpURLConnection) url.openConnection(); |
1887 | | - int responseCode = httpConn.getResponseCode(); |
1888 | | - if (responseCode == HttpURLConnection.HTTP_OK) { |
1889 | | - int contentLength = httpConn.getContentLength(); |
1890 | | - if (contentLength < 0) { |
1891 | | - logger.warn("Content length not provided for {}, progress updates may not be accurate", |
1892 | | - fileURL); |
1893 | | - } |
1894 | | - try (InputStream inputStream = httpConn.getInputStream(); |
1895 | | - FileOutputStream outputStream = new FileOutputStream(savePath)) { |
1896 | | - byte[] buffer = new byte[4096]; |
1897 | | - int bytesRead; |
1898 | | - int downloaded = 0; |
1899 | | - int lastReportedPercent = 0; |
1900 | | - while ((bytesRead = inputStream.read(buffer)) != -1) { |
1901 | | - outputStream.write(buffer, 0, bytesRead); |
1902 | | - downloaded += bytesRead; |
1903 | | - if (contentLength > 0) { |
1904 | | - int percentDownloaded = (int) ((downloaded / (double) contentLength) * 100); |
1905 | | - // Update every 5 percent or on completion |
1906 | | - if (percentDownloaded - lastReportedPercent >= 5 || percentDownloaded == 100) { |
1907 | | - logger.debug("Downloaded {}% from {}", downloaded, fileURL); |
1908 | | - lastReportedPercent = percentDownloaded; |
1909 | | - } |
1910 | | - } |
1911 | | - } |
1912 | | - } |
1913 | | - logger.info("File {} downloaded successfully using {}.", fileURL, savePath); |
1914 | | - } else { |
1915 | | - logger.error("No file to download {}. Server replied with code: {}", fileURL, responseCode); |
1916 | | - return false; |
1917 | | - } |
1918 | | - } catch (IOException ex) { |
1919 | | - logger.error("Failed to download {} due to: {}", fileURL, ex.getMessage(), ex); |
1920 | | - return false; |
1921 | | - } finally { |
1922 | | - if (httpConn != null) { |
1923 | | - httpConn.disconnect(); |
1924 | | - } |
1925 | | - } |
1926 | | - return true; |
1927 | | - } |
1928 | 1877 | } |
0 commit comments