woonie

downloadImage

Sep 25th, 2012
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.05 KB | None | 0 0
  1. public void downloadImage(InetAddress hostname, String host, String path) throws IOException{
  2.         String localFile = path;
  3.         Socket socket = new Socket(hostname,80);
  4.         ByteArrayOutputStream output = new ByteArrayOutputStream();
  5.         InputStream input = socket.getInputStream();
  6.         byte[] buf = new byte[1024];
  7.         int n = 0;
  8.         //System.out.println("Path: " + path);
  9.         //System.out.println("Host: " + host);
  10.         OutputStreamWriter outWriter = new OutputStreamWriter(socket.getOutputStream());
  11.         PrintWriter outw = new PrintWriter(outWriter, false);
  12.         outw.print("GET /" + path + " HTTP/1.0\r\n");
  13.         outw.print("Host: "+ host +"\r\n");
  14.         outw.print("Accept: image/gif, image/jpeg\r\n");
  15.         outw.print("\r\n");
  16.         outw.flush();
  17.         localFile = localFile.substring(localFile.lastIndexOf('/') + 1);
  18.         while (-1 != (n = input.read(buf))){
  19.             output.write(buf, 0, n);
  20.         }
  21.         byte[] response = output.toByteArray();
  22.         FileOutputStream out1 = new FileOutputStream(localFile);
  23.         out1.write(response);
  24.         out1.flush();
  25.         out1.close();
  26.         socket.close();
  27.     }
Add Comment
Please, Sign In to add comment