/**
* Program: spider - Downloader
* Description: Downloader下载工具类
* Author: Nya
* CreateAt: 2024-02-15 00:29
**/
public class Downloader {
private static final int BUFFER_CAPACITY = 1 << 10;
private static final Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 7890));
public static void downloadFile(String uri, String path) throws IOException {
URL url = new URL(uri);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(proxy);
BufferedInputStream br = new BufferedInputStream(urlConnection.getInputStream());
int length;
byte[] buffer = new byte[BUFFER_CAPACITY];
try (FileOutputStream fileOutputStream = new FileOutputStream(path)) {
while ((length = br.read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, length);
}
}
}
}
07-29
3万+
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交