Showing posts with label save image from url. Show all posts
Showing posts with label save image from url. Show all posts

Thursday, 20 November 2014

Save image from ulr using JAVA

package com.cakes;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
public class SaveImageFromUrl {
 public static void main(String[] args) throws Exception {
  String imageUrl = "http://www.avajava.com/images/avajavalogo.jpg";
  String destinationFile = "image.jpg";
  saveImage(imageUrl, destinationFile);
 }
 public static void saveImage(String imageUrl, String destinationFile) throws IOException {
  URL url = new URL(imageUrl);
  InputStream is = url.openStream();
  OutputStream os = new FileOutputStream(destinationFile);
  byte[] b = new byte[2048];
  int length;
  while ((length = is.read(b)) != -1) {
   os.write(b, 0, length);
  }
  is.close();
  os.close();
 }
}

Save image from url using php

<?php
$url = "http://www.technotrigger.com/wp-content/uploads/2014/01/house-in-green-field.jpg";
$content = file_get_contents($url);
file_put_contents('flower.jpg', $content);
?>
<img src="flower.jpg">