下载

package gmap;

import java.awt.List;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.concurrent.CountDownLatch;


public class reDownload  implements Runnable{
    private final static String ossep ="\\";
    private ArrayList<String> lines = new ArrayList<String>() ;
    
    public static void main(String[] args) throws Exception {

        int threads = 5;
        CountDownLatch cdl = new CountDownLatch(threads);
        BufferedReader br = new BufferedReader(new InputStreamReader(
                new FileInputStream("D:\\lwomap\\error.log")));
        String tempString = "";

        ArrayList<reDownload> downloaders = new ArrayList<reDownload>();
        for (int ii = 0; ii < threads; ii++) {
            reDownload r = new reDownload();
            downloaders.add(r);
        }
        int line = 0;
        while ((tempString = br.readLine()) != null) {
            System.out.println("line " + line + ": " + tempString);
            line++;
            downloaders.get(line % threads).addLine(tempString);
        }
        br.close();
        for (reDownload r : downloaders) {
            new Thread(r).start();
        }
        System.out.println("end-----------");
        
    }
    
    @Override
    public void run() {
        Thread current = Thread.currentThread();  
        System.out.println("start run:"+current.getId());
        for(String line:lines){
            try {
                downloadLine(line);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        System.out.println("end");
    }
    public void addLine(String line){
        lines.add(line);
    }
    void downloadLine(String line) throws Exception{
          String[] strs = line.split(",");
          int x =Integer.parseInt(strs[2]);
          int y =Integer.parseInt(strs[1]);
          int z =Integer.parseInt(strs[0]);
          downloadOneImg(x,y,z);
    }

    //String localFile = ossep + z + ossep+y+ ossep +"Map_x=" + x + "y="+y+"zoom="+z+".png";
    public  void downloadOneImg(int x,int y,int z) throws Exception{
        int port =(int)(Math.random()* 3);
        String remotePathString = "/vt/v=w2.115&hl=en&x="+x+"&s=&y="+y+"&z="+z;
        String remoteUrl = "http://mt"+port+".google.cn"+remotePathString;
        String localFile ="Map_x=" + x + "y="+y+"zoom="+z+".png";
        String wFile =ossep + z + ossep+y+ ossep+localFile;
        String ip = getRadomIP("mt"+port+".google.cn");
        System.out.println(remoteUrl);
        System.out.println(localFile);
        /*
        try {
            Socket socket = new Socket(ip, 80);
            OutputStream outputStream = socket.getOutputStream(); // 获取一个输出流,向服务端发送信息
            PrintWriter printWriter = new PrintWriter(outputStream);// 将输出流包装成打印流
            String sendString = "GET "+remotePathString +"HTTP/1.1\r\n"
                    + "Host: "+"mt"+port+".google.cn"+"\r\n" + "\r\n";
            printWriter.print(sendString);
            printWriter.flush();
            socket.shutdownOutput();
            InputStream inputStream = socket.getInputStream();

            byte[] img = readInputStream(inputStream);
            writeImageToDisk(img,localFile);

            inputStream.close();
            printWriter.close();
            outputStream.close();
            socket.close();

        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }*/
    }

    public  String getRadomIP(String host) throws UnknownHostException{
        InetAddress[] addresses = InetAddress.getAllByName(host);
        int number = (int)(Math.random()* addresses.length);
        return addresses[number].getHostAddress();
    }
    
    public  void writeImageToDisk(byte[] img, String fileName) {
        try {

            File file = new File(fileName);
            FileOutputStream fops = new FileOutputStream(file);
            fops.write(img);
            fops.flush();
            fops.close();
            System.out.println("图片已经写入");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public  byte[] readInputStream(InputStream inStream) throws Exception {
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = inStream.read(buffer)) != -1) {
            outStream.write(buffer, 0, len);
        }
        inStream.close();
        byte[] img = outStream.toByteArray();
        int i = 0;
        for (; i < img.length - 3; i++) {
            if (img[i] == '\r' && img[i + 1] == '\n' & img[i + 2] == '\r'
                    && img[i + 3] == '\n') {
                break;
            }
        }
        byte[] newimg = new byte[img.length - i - 4];
        for (int k = 0; k < img.length - i - 4; k++)
            newimg[k] = img[k + i + 4];        
        return newimg;
    }


    
}

 

package gmap;
import java.awt.List;import java.io.BufferedReader;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.PrintWriter;import java.net.InetAddress;import java.net.Socket;import java.net.UnknownHostException;import java.util.ArrayDeque;import java.util.ArrayList;import java.util.concurrent.CountDownLatch;

public class reDownload  implements Runnable{private final static String ossep ="\\";private ArrayList<String> lines = new ArrayList<String>() ;public static void main(String[] args) throws Exception {
int threads = 5;CountDownLatch cdl = new CountDownLatch(threads);BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("D:\\lwomap\\error.log")));String tempString = "";
ArrayList<reDownload> downloaders = new ArrayList<reDownload>();for (int ii = 0; ii < threads; ii++) {reDownload r = new reDownload();downloaders.add(r);}int line = 0;while ((tempString = br.readLine()) != null) {System.out.println("line " + line + ": " + tempString);line++;downloaders.get(line % threads).addLine(tempString);}br.close();for (reDownload r : downloaders) {new Thread(r).start();}System.out.println("end-----------");}@Overridepublic void run() {Thread current = Thread.currentThread();  System.out.println("start run:"+current.getId());for(String line:lines){try {downloadLine(line);} catch (Exception e) {e.printStackTrace();}}System.out.println("end");}public void addLine(String line){lines.add(line);}void downloadLine(String line) throws Exception{  String[] strs = line.split(",");          int x =Integer.parseInt(strs[2]);          int y =Integer.parseInt(strs[1]);          int z =Integer.parseInt(strs[0]);          downloadOneImg(x,y,z);}
//String localFile = ossep + z + ossep+y+ ossep +"Map_x=" + x + "y="+y+"zoom="+z+".png";public  void downloadOneImg(int x,int y,int z) throws Exception{int port =(int)(Math.random()* 3);String remotePathString = "/vt/v=w2.115&hl=en&x="+x+"&s=&y="+y+"&z="+z;String remoteUrl = "http://mt"+port+".google.cn"+remotePathString;String localFile ="Map_x=" + x + "y="+y+"zoom="+z+".png";String wFile =ossep + z + ossep+y+ ossep+localFile;String ip = getRadomIP("mt"+port+".google.cn");System.out.println(remoteUrl);System.out.println(localFile);/*try {Socket socket = new Socket(ip, 80);OutputStream outputStream = socket.getOutputStream(); // 获取一个输出流,向服务端发送信息PrintWriter printWriter = new PrintWriter(outputStream);// 将输出流包装成打印流String sendString = "GET "+remotePathString +"HTTP/1.1\r\n"+ "Host: "+"mt"+port+".google.cn"+"\r\n" + "\r\n";printWriter.print(sendString);printWriter.flush();socket.shutdownOutput();InputStream inputStream = socket.getInputStream();
byte[] img = readInputStream(inputStream);writeImageToDisk(img,localFile);
inputStream.close();printWriter.close();outputStream.close();socket.close();
} catch (UnknownHostException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}*/}
public  String getRadomIP(String host) throws UnknownHostException{InetAddress[] addresses = InetAddress.getAllByName(host);int number = (int)(Math.random()* addresses.length);return addresses[number].getHostAddress();}public  void writeImageToDisk(byte[] img, String fileName) {try {
File file = new File(fileName);FileOutputStream fops = new FileOutputStream(file);fops.write(img);fops.flush();fops.close();System.out.println("图片已经写入");} catch (Exception e) {e.printStackTrace();}}
public  byte[] readInputStream(InputStream inStream) throws Exception {ByteArrayOutputStream outStream = new ByteArrayOutputStream();byte[] buffer = new byte[1024];int len = 0;while ((len = inStream.read(buffer)) != -1) {outStream.write(buffer, 0, len);}inStream.close();byte[] img = outStream.toByteArray();int i = 0;for (; i < img.length - 3; i++) {if (img[i] == '\r' && img[i + 1] == '\n' & img[i + 2] == '\r'&& img[i + 3] == '\n') {break;}}byte[] newimg = new byte[img.length - i - 4];for (int k = 0; k < img.length - i - 4; k++)newimg[k] = img[k + i + 4];return newimg;}

}

 

posted @ 2017-03-20 17:24  cnchengv  阅读(89)  评论(0)    收藏  举报