[英]JSoup BodyAsBytes connection to FileOutputStream to save temp file doesn't work?
I used JSoup to parse a website with cookies. 我用JSoup解析了一个包含cookie的网站。 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:
我想使用JSoup从网站下载文件,并使用以下代码将Cookie保存在哈希图中:
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.
每次精确下载1.408.576字节。 For example when I download an mp3-file this way, the temporary file contains only 40 seconds of the original file.
例如,当我以这种方式下载mp3文件时,临时文件仅包含原始文件的40秒。 What am I missing here?
我在这里想念什么?
Help would be appreciated. 帮助将不胜感激。 Thanks.
谢谢。
Guess I came to soon here to ask my question. 猜猜我很快来这里问我的问题。 Found the solution myself in the GitHub docs for JSoup.
我自己在JSoup的GitHub文档中找到了解决方案。 Thanks anyway for the responses!
无论如何,谢谢您的答复! https://github.com/jhy/jsoup/blob/master/src/main/java/org/jsoup/Connection.java
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! 不管怎么说,还是要谢谢你!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.