簡體   English   中英

Java / Selenium-如何檢查下載文件夾中的下載是否完成

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

我推薦了許多網站,但沒有一個網站可以幫助我檢查下載是否已完成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);
            }
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM