检查 URL 并下载图像

Inspect URL and download Image

我的目标是编写一个 java 应用程序来检查以下内容 URL:https://familysearch.org/pal:/MM9.3.1/TH-1971-28699-12927-58 能够保存图像(属于旧书的页面的副本)并导航到下一页,重复该过程。可以手动下载图像,但我想自动执行此任务。问题是我对网络了解不多,所以我很难过。

我使用浏览器的网络检查器查看了 URL 中的资源,并得出结论可以在此处找到图像:https://familysearch.org/pal:/MM9.3.1/TH-1971-28699-12927-58.jpg.

所以我尝试了以下代码片段:

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

public static void main(String args[]) throws Exception {

        String imageUrl = "https://familysearch.org/pal:/MM9.3.1/TH-1971-28699-12927-58.jpg";
        String destinationFile = "./image.jpg";

        saveImage(imageUrl, destinationFile);
}

这并没有真正奏效。我得到以下输出:

Exception in thread "main" java.io.IOException: Server returned HTTP response code: 500 for URL: https://familysearch.org/pal:/MM9.3.1/TH-1971-28699-12927-58.jpg
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1626)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
at java.net.URL.openStream(URL.java:1037)
at mainpackage.Main.saveImage(Main.java:25)
at mainpackage.Main.main(Main.java:44)

所以我有两个问题:第一个是如何继续下载图像,第二个是如何找到下一张图像的 URL,如 URLs似乎不遵循某种模式(比如计数)。

这是一个工作示例:

import javax.net.ssl.HttpsURLConnection;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;

public class WhosebugTest {

    public static void saveImage(final String imageUrl, final String destinationFile) throws IOException {
        final URL url = new URL(imageUrl);
        final HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();

        urlConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36");
        urlConnection.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
        urlConnection.setInstanceFollowRedirects(true);

        final InputStream is = urlConnection.getInputStream();
        final 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();
    }

    public static void main(final String args[]) throws Exception {

        final String imageUrl = "https://familysearch.org/pal:/MM9.3.1/TH-1971-28699-12927-58.jpg";
        final String destinationFile = "./image.jpg";

        saveImage(imageUrl, destinationFile);
    }
}

问题是 Web 服务器需要 Accept header,但由于找不到它而失败,返回 500 响应。 (此外,图像 URL 执行重定向。)

至于找到下一张图片:这是一个更复杂的任务。如果没有一种简单的方法来识别下一张图像,您可能需要查看 XML/HTML Java 的解析器。一个又好又快的是 Jsoup (http://jsoup.org/)。