JMeter中Foreach Controller引用文件路径失效问题排查
JMeter中Foreach Controller引用文件路径编译错误问题
后置处理器文件路径声明代码
def filePathListArrayList = vars.getObject("pathValue"); 1.upto(filePathListArrayList.size(), { vars.put('filePathUUIDA_' + it, filePathListArrayList.get(it - 1).toString())})
四种尝试方案及编译错误
方案1
def filePathOfImageDir = new File("${__V(filePathUUIDA_${__counter(,)})}") def InputStreamEntity entity = new InputStreamEntity(new FileInputStream(filePathOfImageDir ); requestBuilder.setEntity(entity); // 注:仅为代码片段
错误详情:
2023-07-16 13:27:17,492 ERROR o.a.j.p.j.s.JSR223Sampler: Problem in JSR223 script ImageUpload, message: javax.script.ScriptException: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script914.groovy: 32: Unexpected character: '"' @ line 32, column 35. def filePathOfImageDir = new File("C:\PerformanceTestFiles\555210_98168_98980_0.img") ^ 1 error
方案2
def filePathOfImageDir = new File(${__V(filePathUUIDA_${__counter(,)})}) def InputStreamEntity entity = new InputStreamEntity(new FileInputStream(filePathOfImageDir ); requestBuilder.setEntity(entity);
错误详情:
2023-07-16 13:29:47,507 ERROR o.a.j.p.j.s.JSR223Sampler: Problem in JSR223 script ImageUpload, message: javax.script.ScriptException: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script941.groovy: 32: Unexpected character: '\' @ line 32, column 37. def filePathOfImageDir = new File(C:\PerformanceTestFiles\555210_98168_98980_0.img) ^ 1 error
方案3
def imageDirectory = "${__V(filePathUUIDA_${__counter(,)})}"; def InputStreamEntity entity = new InputStreamEntity(new FileInputStream(imageDirectory); requestBuilder.setEntity(entity);
错误详情:
2023-07-16 13:43:44,027 ERROR o.a.j.p.j.s.JSR223Sampler: Problem in JSR223 script ImageUpload, message: javax.script.ScriptException: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script995.groovy: 34: Unexpected character: '"' @ line 34, column 22. def imageDirectory = "C:\PerformanceTestFiles\555210_98168_98980_0.img"; ^ 1 error
方案4
def imageDirectory = ${__V(filePathUUIDA_${__counter(,)})}; def InputStreamEntity entity = new InputStreamEntity(new FileInputStream(imageDirectory); requestBuilder.setEntity(entity);
错误详情:
Script1022.groovy: 34: Unexpected input: ':' @ line 34, column 23. def imageDirectory = C:\PerformanceTestFiles\555210_98168_98980_0.img; ^ 1 error javax.script.ScriptException: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
尝试过用以下代码替换反斜杠,但未生效:
String filePathOfImage = vars.get("${__V(filePathUUIDA_${__counter(,)})}").replaceAll("\\\\","\\\\\\\\");
解决方案
问题根源
- Windows路径中的反斜杠未正确转义,Groovy解析时会将
\视为转义符,导致路径字符串解析错误。 - 在JSR223 Sampler中直接嵌入JMeter函数(如
__V()、__counter()),会导致Groovy编译时提前解析路径中的特殊字符,引发语法错误。
正确实现方式
方式1:在JSR223 Sampler中直接通过Groovy获取变量
避免混合JMeter函数和Groovy代码,直接通过vars对象获取计数器值和对应路径:
// 获取当前计数器值(__counter默认从1开始计数) def counter = vars.get("__counter").toInteger() // 根据计数器值拼接变量名,获取文件路径 def filePath = vars.get("filePathUUIDA_" + counter) // 将路径中的反斜杠替换为正斜杠(或转义为双反斜杠),确保Groovy正确解析 filePath = filePath.replace("\\", "/") // 创建文件对象并构建请求实体 def file = new File(filePath) def InputStreamEntity entity = new InputStreamEntity(new FileInputStream(file)) requestBuilder.setEntity(entity)
方式2:在后置处理器中提前处理路径转义
修改后置处理器代码,存储路径时就处理好反斜杠,避免后续问题:
def filePathListArrayList = vars.getObject("pathValue"); 1.upto(filePathListArrayList.size(), { // 将路径中的反斜杠替换为正斜杠,或转义为双反斜杠 String processedPath = filePathListArrayList.get(it - 1).toString().replace("\\", "/") vars.put('filePathUUIDA_' + it, processedPath) })
之后在JSR223 Sampler中直接获取变量即可:
def filePath = vars.get("${__V(filePathUUIDA_${__counter(,)})}") def file = new File(filePath) def InputStreamEntity entity = new InputStreamEntity(new FileInputStream(file)) requestBuilder.setEntity(entity)
为什么之前的替换代码未生效
之前的替换代码中,vars.get("${__V(filePathUUIDA_${__counter(,)})}")是先通过JMeter函数解析变量,再传入vars.get(),此时路径中的反斜杠已经被JMeter解析过,导致replaceAll无法正确匹配原始的反斜杠。正确的做法是先通过Groovy获取变量名,再用vars.get()获取原始路径值后处理。
内容的提问来源于stack exchange,提问作者Jyoti Prakash Mallick
相关产品推荐
相关产品推荐

