Search code examples
javaandroidcookiesdownloadjsoup

JSoup BodyAsBytes connection to FileOutputStream to save temp file doesn't work?


I used JSoup to parse a website with cookies. I want to download a file from the website using JSoup and the cookies which were saved in a hashmap using this piece of code:

Connection.Response res = Jsoup.connect("http://www.webpage.com/downloadpage).execute();
Map<String, String> cookies = res.cookies();

So when I try to download the file, I use this:

downloadFile(Jsoup.connect("http://www.webpage.com/file.ext).cookies(cookies).ignoreContentType(true).execute().bodyAsBytes());

and

private void downloadFile(byte[] fileByteArray) {
    try {
        File temprFile = File.createTempFile("tempfile", "ext", getCacheDir());
        temprFile.deleteOnExit();
        FileOutputStream fos = new FileOutputStream(temprFile);
        fos.write(fileByteArray);
        fos.close();

        }  catch (MalformedURLException e){
            e.printStackTrace();
        } catch (IOException ex) {
        String s = ex.toString();
        ex.printStackTrace();
    }
}

The program runs without errors, but when I try to open the temporary file, it appears the file isn't complete. Each time, exactly 1.408.576 bytes are downloaded. For example when I download an mp3-file this way, the temporary file contains only 40 seconds of the original file. What am I missing here?

Help would be appreciated. Thanks.


Solution

  • Guess I came to soon here to ask my question. Found the solution myself in the GitHub docs for JSoup. Thanks anyway for the responses! https://github.com/jhy/jsoup/blob/master/src/main/java/org/jsoup/Connection.java

    /**
     * Set the maximum bytes to read from the (uncompressed) connection into the body, before the connection is closed,
     * and the input truncated. The default maximum is 1MB. A max size of zero is treated as an infinite amount (bounded
     * only by your patience and the memory available on your machine).
     * @param bytes number of bytes to read from the input before truncating
     * @return this Connection, for chaining
     */
    public Connection maxBodySize(int bytes);
    

    Thanks anyway!