繁体   English   中英

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

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

我用JSoup解析了一个包含cookie的网站。 我想使用JSoup从网站下载文件,并使用以下代码将Cookie保存在哈希图中:

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

因此,当我尝试下载文件时,请使用以下命令:

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

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

该程序运行没有错误,但是当我尝试打开临时文件时,似乎文件不完整。 每次精确下载1.408.576字节。 例如,当我以这种方式下载mp3文件时,临时文件仅包含原始文件的40秒。 我在这里想念什么?

帮助将不胜感激。 谢谢。

猜猜我很快来这里问我的问题。 我自己在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);

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM