如何将接收字节数组作为base64转换为字符串并写入Jmeter中的文件 [英] How to convert receiving bytes array as base64 to string and write to file in Jmeter
问题描述
{"结果":{"成功":真," ERRMSG":"误差"},"字节":" PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4NCjxCdXNpbmVzc1J1bGUgeG1sbnM6eHNpPSJodHRwOi8vd3d3LnczLm9yZy8yMDAxL1hNTFNjaGVtYS1pbnN0YW5jZSIgeG1sbnM6eHNkPSJodHRwOi8vd3d3LnczLm9yZy8yMDAxL1hNTFNjaGVtYSI + DQogICAgPEN"}
{"result":{"success":true,"errMsg":"error"},"bytes":"PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4NCjxCdXNpbmVzc1J1bGUgeG1sbnM6eHNpPSJodHRwOi8vd3d3LnczLm9yZy8yMDAxL1hNTFNjaGVtYS1pbnN0YW5jZSIgeG1sbnM6eHNkPSJodHRwOi8vd3d3LnczLm9yZy8yMDAxL1hNTFNjaGVtYSI+DQogICAgPEN"}
我收到如上所述的响应,我需要将字节转换为字符串并使用Jmeter写入文件,这怎么可能,我能够使用JSON提取器提取值来写入文件并写入文件
I am receiving a response like above, I need to convert the bytes to a string and write to file using Jmeter , How is this possible, I am able to write to file using extracting the value using JSON extractor and write to file
receivedbytes = vars.get("recvBytes");
log.info(receivedbytes);
FileWriter fstream = new FileWriter("C:\\tmp\\getSoftwarePackage.out", true);
BufferedWriter out = new BufferedWriter(fstream);
out.write(receivedbytes);
out.close();
fstream.close();
其中recvBytes是响应的字节
where recvBytes is bytes in response
推荐答案
- 从JMeter 3.1开始,您应该使用JSR223测试元素和Groovy语言,而不是Beanshell
- 您收到的不是字节数组,而是 Base64 字符串
- 如果您要使用多个线程编写同一文件,则会遇到种族条件导致数据丢失
- Since JMeter 3.1 you should be using JSR223 Test Elements and Groovy language, not Beanshell
- You're receiving not bytes array but a Base64 String
- If you will be writing the same file with multiple threads you will run into a race condition resulting in data loss
假设以上所有条件
-
修改您的代码,使其如下所示:
Amend your code to look like as follows:
vars.put('decodedBytes', new String(vars.get('recvBytes').decodeBase64()))
将下一行添加到 user.properties文件:
sample_variables=decodedBytes
使用灵活文件编写器将变量写入文件
Use Flexible File Writer to write the variable into the file
如果您不害怕"竞赛条件或使用JMeter进行功能测试(以1个虚拟用户运行它),您可以修改代码,如下所示:
If you don't "afraid" of the race condition or using JMeter for functional testing running it with 1 virtual user you can amend the code as follows:
new File('C:\\tmp\\getSoftwarePackage.out').text = new String(vars.get('recvBytes').decodeBase64())
这篇关于如何将接收字节数组作为base64转换为字符串并写入Jmeter中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!