java - 我正在尝试使用 java 下载 facebook 个人资料图片。但是出现错误

标签 java facebook

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.net.URL;

public class SaveImageFromUrl {

    public static void main(String[] args) throws Exception {

        // proxy settings
        System.setProperty("http.proxyHost", "porxyHost");
        System.setProperty("http.proxyPort", "8080");
        Authenticator authenticator = new Authenticator() {
             public PasswordAuthentication getPasswordAuthentication() {
            return (new PasswordAuthentication("userxyz","password".toCharArray()));
            }
        };
        Authenticator.setDefault(authenticator);

        String imageUrl = "https://graph.facebook.com/10000012233xxxx/picture";
        String destinationFile = "D://image4.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();
    }

}

我的代码工作正常并下载其他 imageurl 路径的图像。但是当我使用 String imageUrl = "https://graph.facebook.com/10000012233xxxx/picture "; 时它不起作用

我收到以下错误:

Exception in thread "main" java.net.ConnectException: Connection timed out: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.connect(Unknown Source)
    at sun.security.ssl.BaseSSLSocketImpl.connect(Unknown Source)
    at sun.net.NetworkClient.doConnect(Unknown Source)
    at sun.net.www.http.HttpClient.openServer(Unknown Source)
    at sun.net.www.http.HttpClient.openServer(Unknown Source)
    at sun.net.www.protocol.https.HttpsClient.<init>(Unknown Source)
    at sun.net.www.protocol.https.HttpsClient.New(Unknown Source)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
    at java.net.URL.openStream(Unknown Source)
    at SaveImageFromUrl.saveImage(SaveImageFromUrl.java:33)
    at SaveImageFromUrl.main(SaveImageFromUrl.java:28)

最佳答案

您需要确保您的应用程序可以遵循重定向,因为如果您请求,Facebook 就会发送重定向

/{user_id}/picture

要实现这一点,请查看 http://www.mkyong.com/java/java-httpurlconnection-follow-redirect-example/

另外,尝试像这样设置代理身份验证:

System.setProperty( "http.proxyUserName", "username" );
System.setProperty( "http.proxyPassword", "password" );

我怀疑您的代理连接超时,但您应该能够自己测试这一点。

关于java - 我正在尝试使用 java 下载 facebook 个人资料图片。但是出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23468374/

相关文章:

java - OSGi 解决了什么问题?

java - 无法在 Android 中将 java 字符串转换为 JSON 数组

java - 如何检测用户是否通过应用程序设置从 Facebook 删除了该应用程序或更改了 Facebook 密码并重新访问该应用程序

iphone - 我正在使用 FBLogInView 来实现 FBLogin。 loginViewFetchedUserInfo 没有被调用

Facebook 隐私政策 URL : Bad Response Code: URL returned a bad HTTP response code

php - Facebook Graph API 对于较大的数据调用速度较慢

java - 找出一系列日期是否涵盖一个时间间隔

java - KeyListener源码

java - 使用java解析csv文件

ios - 在 iOS 中获取其他 Facebook 用户的个人资料图片