Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void downloadImage(InetAddress hostname, String host, String path) throws IOException{
- String localFile = path;
- Socket socket = new Socket(hostname,80);
- ByteArrayOutputStream output = new ByteArrayOutputStream();
- InputStream input = socket.getInputStream();
- byte[] buf = new byte[1024];
- int n = 0;
- //System.out.println("Path: " + path);
- //System.out.println("Host: " + host);
- OutputStreamWriter outWriter = new OutputStreamWriter(socket.getOutputStream());
- PrintWriter outw = new PrintWriter(outWriter, false);
- outw.print("GET /" + path + " HTTP/1.0\r\n");
- outw.print("Host: "+ host +"\r\n");
- outw.print("Accept: image/gif, image/jpeg\r\n");
- outw.print("\r\n");
- outw.flush();
- localFile = localFile.substring(localFile.lastIndexOf('/') + 1);
- while (-1 != (n = input.read(buf))){
- output.write(buf, 0, n);
- }
- byte[] response = output.toByteArray();
- FileOutputStream out1 = new FileOutputStream(localFile);
- out1.write(response);
- out1.flush();
- out1.close();
- socket.close();
- }
Add Comment
Please, Sign In to add comment