简体   繁体   中英

I am trying to download facebook profile picture using java.But getting error

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

}

My code works fine and downloads image for other imageurl paths.But it is not working when I use String imageUrl = " https://graph.facebook.com/10000012233xxxx/picture ";

I am getting the following error :

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)

You need to make sure that your application can follow redirects, because Facebook is sending one if you request

/{user_id}/picture

To implement this, have a look at http://www.mkyong.com/java/java-httpurlconnection-follow-redirect-example/

Also, try setting the Proxy authentication like this:

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

I suspect you get the timeout from your proxy connection, but you should be able to test this yourself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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