2012-04-13 17 views
0

私はJsoup Java HTMLパーサを使用して特定のURLから画像を取得しています。しかし、画像の中にはステータス502のエラーコードが表示されているため、マシンに保存されません。エラー502ステータスを修正するには

String url = "http://www.jabong.com"; 
String html = Jsoup.connect(url.toString()).get().html(); 
Document doc = Jsoup.parse(html, url); 
images = doc.select("img"); 

for (Element element : images) { 
     String imgSrc = element.attr("abs:src"); 
     log.info(imgSrc); 
     if (imgSrc != "") { 
      saveFromUrl(imgSrc, dirPath+"/" + nameCounter + ".jpg"); 
      try { 
       Thread.sleep(3000); 
      } catch (InterruptedException e) { 
       log.error("error in sleeping"); 
      } 
      nameCounter++; 
     } 
} 

そしてsaveFromURL機能は、次のようになります - - :ここで私が使用しているコードのスナップショットがある私は、ステータスコード502について、インターネット上で検索

public static void saveFromUrl(String Url, String destinationFile) { 
    try { 
     URL url = new URL(Url); 
     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(); 
    } catch (IOException e) { 
     log.error("Error in saving file from url:" + Url); 
     //e.printStackTrace(); 
    } 
} 

が、それは誤りが悪いゲートウェイによるものであると言います。私はこれを理解していない。可能性のあるものの一つは、私はこのエラーが私がループ内の画像にリクエストを送信しているためかもしれないと思っています。 Webサーバーはこの大量の負荷に対応できないため、以前の画像が送信されていないときに画像へのリクエストを拒否しています。私はすべての画像を取得した後に睡眠を入れようとしましたが、運はありません。

答えて

1

は、だから、プロキシなしで作業例です...あなたはあなたのコンソールに次の出力に含まを参照してください必要があります...

import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.net.Authenticator; 
import java.net.HttpURLConnection; 
import java.net.InetSocketAddress; 
import java.net.MalformedURLException; 
import java.net.Proxy; 
import java.net.SocketAddress; 
import java.net.URL; 

public class DownloadImage { 

    public static void main(String[] args) { 

     // URLs for Images we wish to download 
     String[] urls = { 
       "http://cdn.sstatic.net/stackoverflow/img/apple-touch-icon.png", 
       "http://www.google.co.uk/images/srpr/logo3w.png", 
       "http://i.microsoft.com/global/en-us/homepage/PublishingImages/sprites/microsoft_gray.png" 
       }; 

     for(int i = 0; i < urls.length; i++) { 
      downloadFromUrl(urls[i]); 
     } 

    } 

    /* 
    Extract the file name from the URL 
    */ 
    private static String getOutputFileName(URL url) { 

     String[] urlParts = url.getPath().split("/"); 

     return "c:/temp/" + urlParts[urlParts.length-1]; 
    } 

    /* 
    Assumes there is no Proxy server involved. 
    */ 
    private static void downloadFromUrl(String urlString) { 

     InputStream is = null; 
     FileOutputStream fos = null; 

     try { 
      URL url = new URL(urlString); 

      System.out.println("Reading..." + url); 

      HttpURLConnection conn = (HttpURLConnection)url.openConnection(proxy); 

      is = conn.getInputStream(); 

      String filename = getOutputFileName(url); 

      fos = new FileOutputStream(filename); 

      byte[] readData = new byte[1024]; 

      int i = is.read(readData); 

      while(i != -1) { 
       fos.write(readData, 0, i); 
       i = is.read(readData); 
      } 

      System.out.println("Created file: " + filename); 
     } 
     catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } 
     catch (IOException e) { 
      e.printStackTrace(); 
     } 
     finally { 
      if(is != null) { 
       try { 
        is.close(); 
       } catch (IOException e) { 
        System.out.println("Big problems if InputStream cannot be closed"); 
       } 
      }   
      if(fos != null) { 
       try { 
        fos.close(); 
       } catch (IOException e) { 
        System.out.println("Big problems if FileOutputSream cannot be closed"); 
       } 
      } 
     } 

     System.out.println("Completed"); 
    } 
} 

私の作品の完全なコード例です

Reading...http://cdn.sstatic.net/stackoverflow/img/apple-touch-icon.png 
Created file: c:/temp/apple-touch-icon.png 
Completed 
Reading...http://www.google.co.uk/images/srpr/logo3w.png 
Created file: c:/temp/logo3w.png 
Completed 
Reading...http://i.microsoft.com/global/en-us/homepage/PublishingImages/sprites/microsoft_gray.png 
Created file: c:/temp/microsoft_gray.png 
Completed 

関連するサーバー。プロキシサーバで認証が必要な場合にのみ

ここにあなたがこのOracle technote

import java.net.Authenticator; 
import java.net.PasswordAuthentication; 

public class ProxyAuthenticator extends Authenticator { 

    private String userName, password; 

    public ProxyAuthenticator(String userName, String password) { 
     this.userName = userName; 
     this.password = password; 
    } 

    protected PasswordAuthentication getPasswordAuthentication() { 
     return new PasswordAuthentication(userName, password.toCharArray()); 
    } 
} 

に基づく必要がありますそして、あなたの代わりに次のコードを使用することになり、この新しいクラスを使用するために、追加のクラスです上に示したopenConnection()の呼び出し

... 
try { 
    URL url = new URL(urlString); 

    System.out.println("Reading..." + url); 

    Authenticator.setDefault(new ProxyAuthenticator("username", "password"); 

    SocketAddress addr = new InetSocketAddress("proxy.server.com", 80); 
    Proxy proxy = new Proxy(Proxy.Type.HTTP, addr); 

    HttpURLConnection conn = (HttpURLConnection)url.openConnection(proxy); 

    ... 
1

問題HTTP通信の問題のように聞こえるかもしれませんので、通信の側面を処理するためにライブラリを使用しようとする方がよいでしょう。Apache Commons HttpClientを参照してください。

URLConnectionオブジェクトを使用していませんしたがって、Web/Proxyサーバーやクリーンなリソースに関する動作がどのようになるのかは明確ではありません。前述のHttpCommonライブラリはこの点で役立ちます。

また、J2ME libarariesを使用して欲しいものを行ういくつかの例があるようです。私は個人的に使用したものではありませんが、あなたを助けるかもしれません。ここで

+0

ありがとうたくさんのbrad。初心者はURLConnectionオブジェクトについて知りませんでした。リソースをきれいにクローズして何を言おうとしているのかまだ分かりません。説明してください。 – sachinjain024

+1

まだ502エラーが出ていますか? JSoupビジネスを使わずに試してみることのできる別のコード例を掲載しました。たぶん問題がどこにあるのかを特定するのに役立ちます。 – Brad

+0

ちょっとありがとうブラッド、問題を解決していただきありがとうございます。私はこの問題を抱えて以来、私はjsoupを使って相対URLを絶対URLに変更しました。それは私の目的を解決してくれて、私を助けてくれたことに感謝しています。 – sachinjain024

関連する問題