【问题标题】:Verify the file is exist on a location in jmeter using beanshell使用 beanshell 验证文件是否存在于 jmeter 中的某个位置
【发布时间】:2019-08-04 05:12:34
【问题描述】:

我正在尝试使用 jmeter 中的 BeanShell 采样器验证特定文件是否存在于文件夹中。

import org.apache.commons.io.FileUtils;

String filename;
String tempFile;

if(vars.get("LogFile") != null) 
    {
    filename = vars.get("LogFile");
    log.info("File exist");
    }
else {
    log.info("File exist");
    }

我在用户定义的变量中定义了 LogFile。它被定义为 日志文件:D:\Jmeter\Jmeter.log, 目前,它总是返回 true,因为它没有验证文件是否真的存在。

【问题讨论】:

    标签: java jmeter beanshell file-exists


    【解决方案1】:

    Since JMeter 3.1 you should be using JSR223 Sampler 和 Groovy language 所以我建议忘记 Beanshell。

    检查文件是否存在以及如果不存在的相关代码 - 创建它会是这样的:

    File file = new File(vars.get("LogFile"));
    
    if (file.exists()) {
        log.info("File exists");
    } else {
        log.info("File doesn't exist, creating...")
        file.createNewFile()
    } 
    

    【讨论】:

      【解决方案2】:

      要检查你需要添加一个文件是否存在检查

      if (new File(filename).exists()) { 
          log.info("File exist");
      } else {
          log.info("File not exist");
      }
      

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-21
      • 1970-01-01
      • 2021-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多