Skip to content

Instantly share code, notes, and snippets.

@gjp0609
Created January 15, 2019 10:11
Show Gist options
  • Save gjp0609/e0d4a0234062f75f006dbee395fac68b to your computer and use it in GitHub Desktop.
Save gjp0609/e0d4a0234062f75f006dbee395fac68b to your computer and use it in GitHub Desktop.
download file from network to local use thread pool
package message;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;
public class ImgD {
private static final String SAVE_PATH = "D:/Files/Downloads/";
private static final ExecutorService THREAD_POOL = Executors.newFixedThreadPool(10);
public static void main(String[] args) throws Exception {
File file = new File("D:/Files/123.txt");
readUrlsFromFile(file).forEach(url -> THREAD_POOL.execute(new DownloadThread(url)));
}
private static List<String> readUrlsFromFile(File file) throws FileNotFoundException {
BufferedReader reader = new BufferedReader(new FileReader(file));
return reader.lines().collect(Collectors.toList());
}
static class DownloadThread implements Runnable {
private String webUrl;
DownloadThread(String webUrl) {
this.webUrl = webUrl;
}
@Override
public void run() {
System.out.println("url: " + webUrl);
byte[] bytes;
File save;
try {
HttpURLConnection connection = (HttpURLConnection) new URL(webUrl).openConnection();
connection.setConnectTimeout(1000 * 3);
connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
InputStream stream = connection.getInputStream();
bytes = readInputStream(stream);
// 太小的不要
if (bytes.length / 1024 > 10) {
// 文件保存位置
save = new File(SAVE_PATH + "/" + UUID.randomUUID().toString().replaceAll("-", "")
+ webUrl.substring(webUrl.lastIndexOf(".")));
new FileOutputStream(save).write(bytes);
System.out.println(" > Done");
} else {
System.out.println(" > Too Small " + bytes.length);
}
} catch (Exception e) {
System.out.println(" > ERR");
}
}
}
private static byte[] readInputStream(InputStream inputStream) throws IOException {
byte[] buffer = new byte[1024];
int len;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while ((len = inputStream.read(buffer)) != -1) bos.write(buffer, 0, len);
bos.close();
return bos.toByteArray();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment