2014年2月15日土曜日

web上の画像を連続して保存する

・SaveImage.txtに書かれているアドレスの画像を一行ずつgetする
・SaveImageフォルダに保存される


SaveImage.java

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.FileOutputStream;
import java.net.URL;
import java.net.HttpURLConnection;
import java.net.URLConnection;
import java.io.File;
import java.io.FileReader;

import java.nio.charset.StandardCharsets;

public class SaveImage {
    public static void main(String[] args) {
        executeGet();
        //executePost();
    }

    private static void executeGet() {
        System.out.println("===== HTTP GET Start =====");
        try {
        File file = new File("SaveImage.txt");
  BufferedReader br = new BufferedReader(new FileReader(file));
  String str = br.readLine();
  int count =0;
  while(str != null){
 
  System.out.println(str);
  URL url = new URL(str);
  URLConnection conn = url.openConnection();
  InputStream in = conn.getInputStream();
  File file2 = new File(String.format("SaveImage\\save%1$03d.jpg", count)); // 保存先
  FileOutputStream out = new FileOutputStream(file2, false);
  int b;
  while((b = in.read()) != -1){
  out.write(b);
  }
  out.close();
  in.close();
  count++;
  str = br.readLine();
  }
  br.close();
        //URL url = new URL("http://image3.photochoice.net/r/tn_248435_678_2906926_319902230_745de7ffca57cd3501eec381e27960be_1392302311/pc_watermark_6_h/0/1280x921/0x20_1280x921/logoh_00170684/"); // ダウンロードする URL

            /*URL url = new URL("http://yahoo.co.jp");

            HttpURLConnection connection = null;

            try {
                connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("GET");

                if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                    try (InputStreamReader isr = new InputStreamReader(connection.getInputStream(),
                                                                       StandardCharsets.UTF_8);
                         BufferedReader reader = new BufferedReader(isr)) {
                        String line;
                        while ((line = reader.readLine()) != null) {
                            System.out.println(line);
                        }
                    }
                }
            } finally {
                if (connection != null) {
                    connection.disconnect();
                }
            }*/
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println("===== HTTP GET End =====");
    }

    private static void executePost() {
        System.out.println("===== HTTP POST Start =====");
        try {
            URL url = new URL("http://localhost:8080/post");

            HttpURLConnection connection = null;

            try {
                connection = (HttpURLConnection) url.openConnection();
                connection.setDoOutput(true);
                connection.setRequestMethod("POST");

                BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream(),
                                                                                  StandardCharsets.UTF_8));
                writer.write("POST Body");
                writer.write("\r\n");
                writer.write("Hello Http Server!!");
                writer.write("\r\n");
                writer.flush();

                if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                    try (InputStreamReader isr = new InputStreamReader(connection.getInputStream(),
                                                                       StandardCharsets.UTF_8);
                         BufferedReader reader = new BufferedReader(isr)) {
                        String line;
                        while ((line = reader.readLine()) != null) {
                            System.out.println(line);
                        }
                    }
                }
            } finally {
                if (connection != null) {
                    connection.disconnect();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println("===== HTTP POST End =====");
    }
}

0 件のコメント:

コメントを投稿