我已经推荐了许多网站,但没有一个网站可以帮助我检查下载是否100%完成
场景-我正在下载文件,我希望我的selenium / Java程序等待下载完成100%。
(通过HTTP下载是最好的方法,但是我没有找到合适的方法来帮助我)
预先感谢!
答案 0 :(得分:0)
以下代码对我有用, 有两种方法:
第一种方法:(您需要下载AsyncHttpClient JAR)
try {
AsyncHttpClient client = Dsl.asyncHttpClient();
final FileOutputStream stream = new FileOutputStream(FILE_NAME);
client.prepareGet(FILE_URL).execute(new AsyncCompletionHandler<FileOutputStream>() {
@Override
public State onBodyPartReceived(HttpResponseBodyPart bodyPart)
throws Exception {
stream.getChannel().write(bodyPart.getBodyByteBuffer());
return State.CONTINUE;
}
@Override
public FileOutputStream onCompleted(Response response)
throws Exception {
return stream;
}
});
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
第二种方法:
private static void startDownload(String FILE_URL, String FILE_NAME)
{
try (BufferedInputStream in = new BufferedInputStream(new URL(FILE_URL).openStream());
FileOutputStream fileOutputStream = new FileOutputStream(FILE_NAME)) {
byte dataBuffer[] = new byte[1024];
int bytesRead;
while ((bytesRead = in.read(dataBuffer, 0, 1024)) != -1) {
fileOutputStream.write(dataBuffer, 0, bytesRead);
}
} catch (IOException e) {
System.out.println(e);
}
}