JSoup BodyAsBytes连接到FileOutputStream以保存临时文件不起作用?

Bra*_*ker 2 java cookies android download jsoup

我使用JSoup来解析一个带有cookie的网站.我想使用JSoup和使用这段代码保存在hashmap中的cookie从网站下载文件:

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

所以当我尝试下载文件时,我使用这个:

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

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

程序运行没有错误,但是当我尝试打开临时文件时,看起来文件不完整.每次下载正好1.408.576字节.例如,当我以这种方式下载mp3文件时,临时文件仅包含原始文件的40秒.我在这里错过了什么?

帮助将不胜感激.谢谢.

Bra*_*ker 5

我猜我很快来到这里问我的问题.我自己在JSoup的GitHub文档中找到了解决方案.无论如何,谢谢你的回复! 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);
Run Code Online (Sandbox Code Playgroud)

不管怎么说,还是要谢谢你!