2

このリンクで、高解像度の製品画像をダウンロードしようとしています

http://www.hookerfurniture.com/index.cfm/furniture/furniture-catalog.show-product/American-furniture/3005-75310/spindle-back-side-chair---ebony.cfm

[高解像度の写真をダウンロード] をクリックすると、簡単にダウンロードできますが、画像の URL をコピーして別のタブからダウンロードしようとすると、3005_75310.jpg が存在しません。

だから私は最初のリクエストからリクエストヘッダーを見て、それらをURL Javaオブジェクトに設定しようとしましたが、作成されたファイルは空です、誰か考えがありますか?

public static void saveImage(String imageUrl, String destinationFile) {
    URL url;
    try {
        url = new URL(imageUrl);
        URLConnection uc = url.openConnection();

        uc.setRequestProperty("Accept",
                "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        uc.setRequestProperty("Accept-Charset",
                "ISO-8859-1,utf-8;q=0.7,*;q=0.3");
        uc.setRequestProperty("Accept-Encoding", "gzip,deflate,sdch");
        uc.setRequestProperty("Accept-Language", "en-US,en;q=0.8");
        uc.setRequestProperty("Connection", "keep-alive");

        uc.setRequestProperty(
                "Referer",
                "http://www.hookerfurniture.com/index.cfm/furniture/furniture-catalog.show-product/American-furniture/3005-75310/spindle-back-side-chair---ebony.cfm");

        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 (MalformedURLException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}
4

2 に答える 2

0

提供されたリファラーは、実行中のスクレイピングを防止する方法として Web サイトのコーダーが期待しているものではありません。作業リクエストの例:

$ wget \
  --referer=http://www.hookerfurniture.com/index.cfm/furniture/furniture-catalog.show-product/American-furniture/3005-75310/spindle-back-side-chair---ebony.cfm \
  http://www.hookerfurniture.com/index.cfm/furniture/furniture-catalog.photo-download/photo/3005_75310.jpg


Length: unspecified [image/jpeg]
Saving to: `3005_75310.jpg'

    [  <=>                                                                                ] 346,125      949K/s   in 0.4s

2013-01-29 13:24:02 (949 KB/s) - `3005_75310.jpg' saved [346125]
于 2013-01-29T18:26:52.240 に答える
0

価値があるのは、唯一の重要なヘッダーが "Referer" ヘッダーのように見えることです。

これは失敗します:

curl "http://www.hookerfurniture.com/index.cfm/furniture/furniture-catalog.photo-download/photo/3005_75310.jpg" > /test/3005_75310.jpg

これは機能します:

curl -H "Referer: http://www.hookerfurniture.com/index.cfm/furniture/furniture-catalog.show-product/American-furniture/3005-75310/spindle-back-side-chair---ebony.cfm" "http://www.hookerfurniture.com/index.cfm/furniture/furniture-catalog.photo-download/photo/3005_75310.jpg" > /test/3005_75310.jpg

Java で画像データをプルする場合、DataInputStream の readFully() メソッドを使用するのが最も成功しました。

于 2013-01-29T18:28:00.997 に答える