2014-05-05 37 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/不工作 –

相关问题