如何等待文件下载或在所需文件夹中可用?

How to wait till file is getting downloaded or available in required folder?

单击下载按钮后,将在我的项目 dir 中下载一个文件,我想阅读该文件内容,这是 pdf

文件名是动态的。它以相同的名称开头,但中间的内容是动态的,并以 .pdf 扩展名结尾。

代码:

   public static String isFileDownloaded(String fileText, String fileExtension, int timeOut) {
String folderName = System.getProperty("user.dir")+"\src\test\resources\Download\";
File[] listOfFiles;
int waitTillSeconds = timeOut;
boolean fileDownloaded = false;
String filePath = null; 

long waitTillTime = Instant.now().getEpochSecond() + waitTillSeconds;
while (Instant.now().getEpochSecond() < waitTillTime) {
    listOfFiles = new File(folderName).listFiles();
    for (File file : listOfFiles) {
        String fileName = file.getName().toLowerCase();
        if (fileName.contains(fileText.toLowerCase()) && fileName.contains(fileExtension.toLowerCase())) {
            fileDownloaded = true;
            filePath = fileName.getAbsolutePath();
            break;
        }
    }
    if (fileDownloaded) {
        break;
    }
}
return filePath;

}

public static  String readPdfContent(String fileName) throws IOException {
    
    File file = new File(System.getProperty(fileName));
    PDDocument doc = PDDocument.load(file);
    int numberOfPages = getPageCount(doc);
    System.out.println("The total number of pages "+numberOfPages);
    String content = new PDFTextStripper().getText(doc);
    doc.close();    
return content;

}

try {
        JSclick(download);
        String filePath = isFileDownloaded("Personal Data", ".pdf", 30);
        if(filePath != null){
        String pdfContent = readPdfContent(filePath);
        Assert.assertTrue(pdfContent.contains("Test Kumar"));
        Assert.assertTrue(pdfContent.contains("XXXXX"));
        }else {
             System.out.println("Some issue with file downloading");
        }
       
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

它告诉我错误-:方法 getAbsolutePath() 未为类型 String 定义 你能帮忙解决这个问题吗?

你可以简单地列出文件夹中的所有文件并选择最新的

点赞:

File f = new File("download_folder");
String[] pathnames = f.list();

然后你可以使用for-loop来找到你需要的东西

  • 创建一个方法来检查文件是否已完全下载。
  • 在需要的地方调用该方法。

检查文件是否下载的方法:此方法是在给定时间内在下载文件夹中搜索包含部分或全文的特定文件,returns文件的绝对路径。

@param fileText - Partial or full file name
@param fileExtension - .pdf, .txt
@param timeOut - How many seconds you are expecting for file to be downloaded.
    public static String isFileDownloaded(String fileText, String fileExtension, int timeOut) {
        String folderName = "location of download folde";
        File[] listOfFiles;
        int waitTillSeconds = timeOut;
        boolean fileDownloaded = false;
        String filePath = null; 

        long waitTillTime = Instant.now().getEpochSecond() + waitTillSeconds;
        while (Instant.now().getEpochSecond() < waitTillTime) {
            listOfFiles = new File(folderName).listFiles();
            for (File file : listOfFiles) {
                String fileName = file.getName().toLowerCase();
                if (fileName.contains(fileText.toLowerCase()) && fileName.contains(fileExtension.toLowerCase())) {
                    fileDownloaded = true;
                    filePath = file.getAbsolutePath();
                    break;
                }
            }
            if (fileDownloaded) {
                break;
            }
        }
        return filePath;
    }

调用isFileDownloaded方法:

因为你知道部分文件名,所以你可以通过如下输入来获取文件路径。

String filePath = isFileDownloaded("Personal Data", ".pdf", 30);
System.out.println("Complete path of file:"+ filePath);

输出:

C:\Users\Download\Personal data...pdf

OP 逻辑的更新代码:

试块:

    try {
        JSclick(download);
        String filePath = isFileDownloaded("Personal Data", ".pdf", 30);
        if(filePath != null){
        String pdfContent = readPdfContent(filePath);
        Assert.assertTrue(pdfContent.contains("Test Kumar"));
        Assert.assertTrue(pdfContent.contains("XXXXX"));
        }
       }else{
         System.out.println("Some issue with file downloading");
       }

阅读pdf内容的方法:

public static  String readPdfContent(String fileName) throws IOException {
        
        File file = new File(System.getProperty(fileName);    
        PDDocument doc = PDDocument.load(file);
        int numberOfPages = getPageCount(doc);
        System.out.println("The total number of pages "+numberOfPages);
        String content = new PDFTextStripper().getText(doc);
        doc.close();
    return content;
}