Java /Selenium- 如何在下载文件夹中检查是否下载完成

Java /Selenium- How to check if the download was completed in the download folder

我推荐了很多网站,其中 none 为我检查下载是否完成 100%

场景 - 我正在下载一个文件,我希望我的 selenium/Java 程序等到下载完成 100%。

(通过 HTTP 下载最好,但我没有找到合适的方法来帮助我)

提前致谢!!

下面的代码对我有用, 有两种方式:

第一种方法:(您需要下载 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);
            }
 }