线程下载百度瓦片

package util;

import java.net.*;
import java.io.*;
import java.util.Date;
import java.util.concurrent.*;
import java.util.logging.Logger; 

public class FileDownConnManager {

    private static final FileDownConnManager connManager = new FileDownConnManager();

    private static ExecutorService executorService = Executors.newFixedThreadPool(32); 

    public static FileDownConnManager getDefaultManager() {
        return connManager;
    }

    public static void fileDown(int xmin, int xmax,int ymin, int ymax,Integer z) throws ExecutionException, InterruptedException {
        executorService.submit(new Callable() {
            @Override
            public Integer call() throws Exception {

                for (int i = xmin; i <= xmax; i++) {
                    for (int j = ymin; j <= ymax; j++) {
                        try {
                            String link =
                                    "http://online2.map.bdimg.com/tile/?qt=tile&x={x}&y={y}&z={z}&styles=pl&udt=20181012&scaler=1";
                            try {
                                URL url = new URL(link.replace("{x}", i + "").replace("{y}", j + "").replace("{z}", z + ""));
                                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                                conn.setConnectTimeout(100);
                                conn.connect();
                                InputStream in = conn.getInputStream();
                                File dir = new File("D:\\英雄时刻\\" + z + "/" + i);
                                if (!dir.exists()) {
                                    dir.mkdirs();
                                }
                                File file = new File("D:\\英雄时刻\\" + z + "/" + i + "/" + j + ".png");
                                if (!file.exists()) {
                                    file.createNewFile();
                                }
                                OutputStream out = new FileOutputStream(file);
                                byte[] bytes = new byte[1024 * 20];
                                int len = 0;
                                while ((len = in.read(bytes)) != -1) {
                                    out.write(bytes, 0, len);
                                }
                                out.close();
                                in.close();
                            } catch (Exception e) {
                            }
                        } catch (Exception e) {
                            e.getMessage();
                        }
                    }
                }
                return 1;
            }
        });
    }


    public static void main(String[] args) throws ExecutionException, InterruptedException {
//        int xmin = 24979;//x最小值
//        int xmax = 26016;//x最大值
//        int ymin = 6651;//y最小值
//        int ymax = 7994;//y最大值
//        int z = 17;//层级

//        int xmin = 12489;//x最小值
//        int xmax = 13008;//x最大值
//        int ymin = 3325;//y最小值
//        int ymax = 3997;//y最大值
//        int z = 16;//层级

//        int xmin = 6244;//x最小值
//        int xmax = 6504;//x最大值
//        int ymin = 1662;//y最小值
//        int ymax = 1999;//y最大值
//        int z = 15;//层级

//        int xmin = 3122;//x最小值
//        int xmax = 3252;//x最大值
//        int ymin = 813;//y最小值
//        int ymax = 1000;//y最大值
//        int z = 14;//层级

//        int xmin = 1561;//x最小值
//        int xmax = 1626;//x最大值
//        int ymin = 415;//y最小值
//        int ymax = 500;//y最大值
//        int z = 13;//层级

//        int xmin = 780;//x最小值
//        int xmax = 813;//x最大值
//        int ymin = 207;//y最小值
//        int ymax = 255;//y最大值
//        int z = 12;//层级

//        int xmin = 390;//x最小值
//        int xmax = 407;//x最大值
//        int ymin = 103;//y最小值
//        int ymax = 125;//y最大值
//        int z = 11;//层级

//        int xmin = 195;//x最小值
//        int xmax = 204;//x最大值
//        int ymin = 51;//y最小值
//        int ymax = 63;//y最大值
//        int z = 10;//层级

//        int xmin = 97;//x最小值
//        int xmax = 102;//x最大值
//        int ymin = 25;//y最小值
//        int ymax = 32;//y最大值
//        int z = 9;//层级

        int xmin = 48;//x最小值
        int xmax = 51;//x最大值
        int ymin = 12;//y最小值
        int ymax = 16;//y最大值
        int z = 8;//层级

//        int xmin = 24;//x最小值
//        int xmax = 26;//x最大值
//        int ymin = 6;//y最小值
//        int ymax = 8;//y最大值
//        int z = 7;//层级

//        int xmin = 5;//x最小值
//        int xmax = 13;//x最大值
//        int ymin = 1;//y最小值
//        int ymax = 7;//y最大值
//        int z = 6;//层级

//        int xmin = 4;//x最小值
//        int xmax = 6;//x最大值
//        int ymin = 0;//y最小值
//        int ymax = 2;//y最大值
//        int z = 5;//层级

//        int xmin = 6;//x最小值
//        int xmax = 6;//x最大值
//        int ymin = 4;//y最小值
//        int ymax = 4;//y最大值
//        int z = 4;//层级

//        int xmin = 3;//x最小值
//        int xmax = 3;//x最大值
//        int ymin = 2;//y最小值
//        int ymax = 2;//y最大值
//        int z = 3;//层级

        int count2 = (xmax-xmin)%10;
        int count = ( (xmax-xmin)/10 == 0 ) ? 1 : ( (xmax-xmin)/10 );

        if(count2>=1) {
            while (xmin < (xmax - count)) {
                FileDownConnManager.fileDown(xmin, (xmin + count), ymin, ymax, z);
                System.out.println("线程数:" + ((ThreadPoolExecutor) executorService).getActiveCount());
                xmin += count;
            }
        }
        FileDownConnManager.fileDown(xmin, xmax, ymin, ymax,z);

        executorService.shutdown();

    }


}

你可能感兴趣的:(线程下载百度瓦片)