【问题标题】:I am trying to download facebook profile picture using java.But getting error我正在尝试使用 java 下载 facebook 个人资料图片。但出现错误
【发布时间】:2014-05-05 08:45:54
【问题描述】:
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)

【问题讨论】:

    标签: java facebook


    【解决方案1】:

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

    /{user_id}/picture
    

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

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

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

    我怀疑你的代理连接超时了,但你应该可以自己测试一下。

    【讨论】:

    猜你喜欢
    • 2011-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-30
    • 1970-01-01
    • 2017-03-26
    • 2021-10-25
    • 1970-01-01
    相关资源
    最近更新 更多