Home  >  Q&A  >  body text

java - Problem with crawling images?

Just modified POM, you can download the source code again

This is the project address of the crawler I wrote
The project does not report an error, but the problem is that after downloading the image to the local area, the image is often incomplete, as follows:

##This is the core code for downloading pictures, as follows:

@Override
    public void run() {
        Response res = null;
        try {
            res = Jsoup.connect(src).ignoreContentType(true).timeout(30000).execute();
            byte[] bytes = res.bodyAsBytes();
            File file = new File(path + name);
            if (!file.exists()) {
                RandomAccessFile raf = new RandomAccessFile(file, "rw");
                raf.write(bytes);
                raf.close();
            }
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }

After data query, I feel that it is a problem with the range request

Range or a problem that I did not find? I hope you can take a look at it, thank you

天蓬老师天蓬老师2675 days ago815

reply all(1)I'll reply

  • typecho

    typecho2017-06-23 09:14:30

    1. It seems that the response data is not complete. You can debug it to see if there is any discrepancy between the actual data size of the response and the data size you saved.

    2. Or maybe because there is still some data in the buffer and there is no time to write to the file, the process exits at this time, resulting in incomplete data. Perform a flush operation before closing the file stream.

    reply
    0
  • Cancelreply