我试图使用java.But下载facebook个人资料图片获取错误

时间:2014-05-05 08:45:55

标签: 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)

1 个答案:

答案 0 :(得分:2)

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

/{user_id}/picture

要实现此目的,请查看http://www.mkyong.com/java/java-httpurlconnection-follow-redirect-example/

另外,尝试设置代理身份验证,如下所示:

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

我怀疑你从代理连接中获得了超时,但你应该能够自己测试一下。