Advertisement
evgenko

Untitled

Sep 11th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.57 KB | None | 0 0
  1. package com.company;
  2.  
  3.  
  4. import java.io.*;
  5. import java.net.URL;
  6. import java.nio.charset.Charset;
  7. import java.util.ArrayList;
  8.  
  9. public class Main {
  10.  
  11.     public static void main(String[] args) throws IOException {
  12.  
  13.         ArrayList<String> arrayOfUrl = new ArrayList();
  14.         int indexOfElementArray = 0;
  15.         try (
  16.                 InputStream fileInput = new FileInputStream("empty.txt");
  17.                 InputStreamReader inputStreamRead = new InputStreamReader(fileInput, Charset.forName("UTF-8"));
  18.                 BufferedReader bufferRead = new BufferedReader(inputStreamRead);) {
  19.             String url;
  20.             while ((url = bufferRead.readLine()) != null) {
  21.                 arrayOfUrl.add(indexOfElementArray, url);
  22.                 indexOfElementArray++;
  23.             }
  24.         } catch (FileNotFoundException e) {
  25.             e.printStackTrace();
  26.         } catch (IOException e) {
  27.             e.printStackTrace();
  28.         }
  29.  
  30.         while (indexOfElementArray > 0) {
  31.             indexOfElementArray--;
  32.             saveImage(arrayOfUrl.get(indexOfElementArray), indexOfElementArray + ".jpg");
  33.         }
  34.  
  35.     }
  36.  
  37.     public static void saveImage(String imageUrl, String destinationFile) throws IOException {
  38.         URL url = new URL(imageUrl);
  39.         InputStream is = url.openStream();
  40.         OutputStream os = new FileOutputStream(destinationFile);
  41.  
  42.         byte[] b = new byte[2048];
  43.         int length;
  44.  
  45.         while ((length = is.read(b)) != -1) {
  46.             os.write(b, 0, length);
  47.         }
  48.  
  49.         is.close();
  50.         os.close();
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement