I have referred many sites, none of them worked for me to check if download was completed 100%
Scenario- I am downloading a file, and I want my selenium/Java program to wait until the download is completed 100%.
(Downloading through HTTP would be the best, but I did not find anything appropriate that would help me out)
Thanks in Advance!!
The below code worked for me, There are 2 ways:
1st Method:(You need to download 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();
}
2nd Method :
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);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.