0

I am using below code, its not working.
when i use imageUrl on browser its redirect somewhere then its working.
But i have n number of facebook id only and every time redirected url is different.

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://graph.facebook.com/67563683055/picture?type=square";
    String destinationFile = "C:\\Users\\emtx\\Desktop\\Nxg-pic.png";

    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();
}

}

codePlayer
  • 15
  • 1
  • 3

2 Answers2

0

Address you are using redirects you to other location. Since URL class doesn't redirects you automatically what you get is

  • headers containing information about redirection (like Location which points you to new address),
  • and possibly body (but in this case it is empty).

You may want to create method like (based on: http://www.mkyong.com/java/java-httpurlconnection-follow-redirect-example/)

public static String getFinalLocation(String address) throws IOException{
    URL url = new URL(address);
    HttpURLConnection conn = (HttpURLConnection)url.openConnection();
    int status = conn.getResponseCode();
    if (status != HttpURLConnection.HTTP_OK) 
    {
        if (status == HttpURLConnection.HTTP_MOVED_TEMP
            || status == HttpURLConnection.HTTP_MOVED_PERM
            || status == HttpURLConnection.HTTP_SEE_OTHER)
        {
            String newLocation = conn.getHeaderField("Location");
            return getFinalLocation(newLocation);
        }
    }
    return address;
}

and change your

URL url = new URL(imageUrl);

to

URL url = new URL(getFinalLocation(imageUrl));
Pshemo
  • 122,468
  • 25
  • 185
  • 269
0

In addition to Phsemos answer, you can also disable the redirection with the redirect parameter and use the following API call to get the real URL as JSON:

https://graph.facebook.com/67563683055/picture?type=square&redirect=false

This is how the JSON looks like:

{
   "data": {
      "is_silhouette": false,
      "url": "https://scontent.xx.fbcdn.net/hprofile-xaf1/v/t1.0-1/c44.44.544.544/s50x50/316295_10151906553973056_2129080216_n.jpg?oh=04888b6ef5d631447227b42d82ebd35d&oe=57250EA4"
   }
}
andyrandy
  • 72,880
  • 8
  • 113
  • 130
  • how i can use this in my example. – codePlayer Feb 15 '16 at 09:20
  • since you just accepted my answer, i guess you found out on your own? either way, there are some stackoverflow threads about that, if you need more information: http://stackoverflow.com/questions/7467568/parsing-json-from-url – andyrandy Feb 15 '16 at 16:08
  • or that one: http://stackoverflow.com/questions/4308554/simplest-way-to-read-json-from-a-url-in-java – andyrandy Feb 15 '16 at 16:08