我使用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秒.我在这里错过了什么?
帮助将不胜感激.谢谢.