2014-05-05 40 views
1
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 paths.But它不工作時,我用String IMAGEURL =「https://graph.facebook.com/10000012233xxxx/picture」;我試圖用java.But得到錯誤下載Facebook的個人資料圖片

我收到以下錯誤:

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) 

回答

2

你需要確保,如果您申請

/{user_id}/picture 

爲了實現這個應用程序可以跟蹤重定向,因爲Facebook正在發送一個,都看看http://www.mkyong.com/java/java-httpurlconnection-follow-redirect-example/

另外,試試設置這樣的代理服務器認證:

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

我懷疑你的代理連接超時,但你應該可以自己測試一下。

+0

如何做重定向? –

+0

谷歌是你的朋友,我的朋友......!我更新了我的答案。 – Tobi

+0

http://www.mkyong.com/java/java-httpurlconnection-follow-redirect-example/不工作 –

相關問題