简体   繁体   English

如何在我的计算机磁盘中以Java下载Facebook个人资料图片

[英]how download facebook profile picture in java in my computer disk

I am using below code, its not working. 我正在使用下面的代码,它不起作用。
when i use imageUrl on browser its redirect somewhere then its working. 当我在浏览器上使用imageUrl时,其重定向到某处,然后工作。
But i have n number of facebook id only and every time redirected url is different. 但是我只有n个Facebook ID,每次重定向的URL都不一样。

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

} }

Address you are using redirects you to other location. 您使用的地址会将您重定向到其他位置。 Since URL class doesn't redirects you automatically what you get is 由于URL类不会自动重定向您,因此您得到的是

  • headers containing information about redirection (like Location which points you to new address), 标头,其中包含有关重定向的信息(例如,“ Location将您指向新地址),
  • 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/ ) 您可能想创建类似的方法(基于: 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));

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: 除了Phsemos答案,您还可以使用redirect参数禁用重定向,并使用以下API调用将真实的URL作为JSON获取:

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

This is how the JSON looks like: JSON如下所示:

{
   "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"
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM