java - 复制文件时 JMeter Bean Shell Sampler 错误 "...Static method get( java.lang.String ) not found in class' java.nio.file.Paths”

标签 java jmeter beanshell

我正在尝试使用 JMeter 3.0 (Java v1.8) 中的 Bean Shell Sampler 复制并重命名本地计算机 (Win 7) 上的文件。其想法是创建具有唯一名称的新文件,并将该名称保存为变量,该变量可用于代替 FTP PUT 请求中的文件名。

这是我用于复制和重命名的代码:

import java.text.*;
import java.nio.file.StandardCopyOption.*;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

String filename = new SimpleDateFormat("dd-MM-yyyy_hh:mm:ss").format(new Date())+".xlsx";
log.info(filename);

Path source = Paths.get("C:/dropfile/qatp/QATP_GuestRecords.xlsx");
Path target = Paths.get("C:/dropfile/qatp/"+filename);

Files.copy(source, target, REPLACE_EXISTING);

我在日志中收到的错误:

ERROR - jmeter.util.BeanShellInterpreter: Error invoking bsh method: eval Sourced file: inline evaluation of: ``import java.text.; import java.nio.file.StandardCopyOption.; import java.io.IO . . . '' : Typed variable declaration : Error in method invocation: Static method get( java.lang.String ) not found in class'java.nio.file.Paths'

我一直在寻找这个问题的答案,并发现了 solution where the suggestion was : “我的猜测是问题在于它没有填充 varargs 参数。尝试:

Path target = Paths.get(filename, new String[0]);"

我通过修改代码尝试了这个解决方案,如下所示:

import java.text.*;
import java.nio.file.StandardCopyOption.*;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

String filename = new SimpleDateFormat("dd-MM-yyyy_hh:mm:ss").format(new Date())+".xlsx";
log.info(filename);

Path source = Paths.get("C:/dropfile/qatp/QATP_GuestRecords.xlsx", new String[0]);
Path target = Paths.get("C:/dropfile/qatp/"+filename, new String[0]);

Files.copy(source, target, REPLACE_EXISTING);

并收到此错误:

ERROR - jmeter.util.BeanShellInterpreter: Error invoking bsh method: eval Sourced file: inline evaluation of: ``import java.text.; import java.nio.file.StandardCopyOption.; import java.io.IO . . . '' : Typed variable declaration : Method Invocation Paths.get

有谁知道我为什么会遇到此错误以及如何解决它?

最佳答案

即使在普通的旧 Java 中,这也是对 Paths.get 的误导性使用,它接受 URI 或字符串数​​组 (varargs)。 See javadoc .

在 Java 中,您尝试的方法有效,因为静态类型允许编译器确定您正在传递单个字符串的数组。显然 BeanShell 不这样做并且感到困惑。在我看来,另一个答案中建议的技巧并不是一个好的技巧:在Java中它再次可以工作,通过连接两个字符串(第二个是空的,所以结果是第一个字符串,这就是你想要的),但它令人困惑BeanShell 都一样,因为还有另一个带有 2 个参数的静态 get 方法。

如果您已经将路径作为单个字符串,请尝试以下操作:

Path source = new File("C:/dropfile/qatp/QATP_GuestRecords.xlsx").toPath();

或者,您可以像这样使用 Paths.get:

Path source = Paths.get("C:", "dropfile", "qatp", "QATP_GuestRecords.xlsx");

或者像这样(varargs 是帮助传递数组的语法糖):

Path source = Paths.get(new String [] { "C:/dropfile/qatp/QATP_GuestRecords.xlsx" });

将路径片段作为参数传递,或将整个路径字符串作为单个参数传递是完全有效的,但这似乎会使 BeanShell 出错,因此,最好避免在 BeanShell 中使用 Paths.get ,除非你传递一个明确的数组,如上一个示例所示。

关于java - 复制文件时 JMeter Bean Shell Sampler 错误 "...Static method get( java.lang.String ) not found in class' java.nio.file.Paths”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44209688/

相关文章:

java - 这是什么意思调用空对象的实例方法“

java - 将 commons-net jar 添加到 build.gradle 文件

java - java中的代码解释

java - Jmeter:- 线程组在 Unix 上执行两次

java - 如何将 Jmeter 变量保存到 csv 文件

java - 链接列表在尝试访问其数据时出现空点错误

JMeter:如何在请求 header 中发布cookie数据

python - 启动和停止服务器脚本

performance - JMeter - 在另一个变量中使用一个变量

java - 为什么 Beanshell 会阻塞源文件中的 '@' 字符?