1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| package com.example.login;
import com.google.common.collect.Lists; import com.google.common.io.ByteStreams; import com.google.common.io.Closer; import com.google.common.io.Files;
import java.io.File; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.concurrent.*; import java.util.stream.IntStream;
public class Test { public static void main(String[] args) throws ExecutionException, InterruptedException { ExecutorService executorService = Executors.newFixedThreadPool(10); System.out.println("[开始]:"+new Date().toLocaleString()); IntStream.rangeClosed(1,10).parallel().forEach(e->{ Future<String> submit = executorService.submit(new downloadThread(e)); }); } public static String downloadThreadDo(Integer count){ Closer closer = Closer.create(); try { String url = "http://xxxxxx/file/tomcatWebPackage.zip"; File imageDir = new File("d://Temp//"); File imageFile = new File(imageDir, "tomcatWebPackage"+count+".zip"); InputStream in = closer.register(new URL(url).openStream()); Files.write(ByteStreams.toByteArray(in), imageFile); } catch(Exception ex) { throw closer.rethrow(e); } finally { closer.close(); } System.out.println("["+count+"][结束]:"+new Date().toLocaleString()); return "success:"+count; } static class downloadThread implements Callable<String> { Integer count; public downloadThread(Integer count) { this.count=count; } @Override public String call() { return downloadThreadDo(count); } } }
|