Sample code for 30+ languages & platforms
Java

Download Photo to a File

See more Facebook Examples

Assuming we have the ID of a Photo, this example demonstrates how to download the photo image data to a file.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.OAuth2;
import com.chilkatsoft.chilkat2.Rest;
import com.chilkatsoft.chilkat2.StringBuilder;

public class ChilkatExample {

    public static void main(String[] args) {
        // This example requires the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        // This example assumes a previously obtained an access token
        OAuth2 oauth2 = new OAuth2();
        oauth2.setAccessToken("FACEBOOK-ACCESS-TOKEN");

        Rest rest = new Rest();

        // Connect to Facebook...
        try {
            rest.connect("graph.facebook.com", 443, true, true);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // Provide the authentication credentials (i.e. the access key)
        rest.setAuthOAuth2(oauth2);

        // Assumes we've already obtained a Photo ID.
        String photoId = "10210199026347451";

        StringBuilder sbPath = new StringBuilder();
        sbPath.append("/v2.7/");
        sbPath.append(photoId);

        // First we're going to get the photo informaton so we can get the URL of the image file data.
        // Select the fields we want.
        // See https://developers.facebook.com/docs/graph-api/reference/photo/
        rest.addQueryParam("fields", "id,album,images");

        String responseJson;
        try {
            responseJson = rest.fullRequestNoBody("GET", sbPath.getAsString());
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        JsonObject json = new JsonObject();
        json.setEmitCompact(false);
        json.load(responseJson);

        // Show the JSON in human-readable format.
        System.out.println(json.emit());

        // Get the image URL.
        String imageUrl = json.stringOf("images[0].source");
        System.out.println("Downloading from " + imageUrl);

        StringBuilder sbImageUrl = new StringBuilder();
        sbImageUrl.append(imageUrl);

        // Build the output local file path.
        StringBuilder sbToPath = new StringBuilder();
        sbToPath.append("qa_output/fb");
        sbToPath.append(json.stringOf("id"));
        boolean bCaseSensitive = false;
        if (sbImageUrl.contains(".jpg", bCaseSensitive)) {
            sbToPath.append(".jpg");
        } else {
            sbToPath.append(".png");
        }

        System.out.println("Downloading to " + sbToPath.getAsString());

        // Download using Chilkat HTTP.
        Http http = new Http();
        try {
            http.download(imageUrl, sbToPath.getAsString());
            System.out.println("Downloaded.");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
        }
    }
}