java - 检查 URL 并下载图像

标签 java image web web-crawler

我的目标是编写一个检查以下 URL 的 Java 应用程序: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,因为 URL 似乎不遵循某种模式(比如计数)。

最佳答案

这是一个工作示例:

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 StackOverflowTest {

    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 执行重定向。)

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

关于java - 检查 URL 并下载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28139967/

相关文章:

java - 由 : java. lang.ClassNotFoundException : org. apache.commons.io.FileUtils、maven pom.xml 引起

java - Java 中字符串的排列。请解释?

image - 在 play 2 中处理动态创建的文件

javascript - AngularJS 和外部 API - 让它像 PostMan 一样工作

java - 编译时由: android. database.sqlite.SQLiteException : near "": syntax error (code 1): ,引起:

java - 多个 Apache mod_jk 服务器指向同一个 Tomcat worker?

java - ListView 和图像 : I'm going crazy

html - IE9 中的图像模糊、内容移动、无法重现的错误

java - 即使返回404错误,tomcat是否创建线程

对象属性的 C# Web 服务序列化