在 JMeter 中,我在 Response 中获得了一个使用 RegEx Extractor 提取的 base64 编码的 PDF。这一切都很好。
然后我需要解码该 base64 编码文档并将其写入一个文件,我在 BeanShell 后处理器中使用以下代码执行此操作:
import org.apache.commons.io.FileUtils;
import org.apache.commons.codec.binary.Base64;
// Set the response variable
String response = vars.get("documentText");
// Remove the carriage return hex code and condense to single string
String encodedFile = response.replace("
","").replaceAll("[\n]+","");
// Decode the encoded string
vars.put("decodedFile",new String(Base64.decodeBase64(encodedFile)));
// Write out the decoded file
Output = vars.get("decodedFile");
f = new FileOutputStream("C:\\Users\\user\\Desktop\\decodedFile.pdf");
p = new PrintStream(f);
this.interpreter.setOut(p);
print(Output);
p.flush();
f.close();
我的问题是,解码并写出的文件打开时为空白 PDF。
在解决此问题时,我用 JMeter 中的编码字符串写出了一个文件,然后使用 this 手动对其进行解码。 Base64 工具。当我手动解码文件时,它按预期打开。
然后,我将 JMeter 生成的文件的文本与我使用该工具解码的文件进行比较,发现 JMeter 生成的文件全文包含随机的 ?
我假设这一定是罪魁祸首,但是,我不知道是什么导致这些出现或如何修复它。
最佳答案
JMeter 无法正确解码 Base64,因为 JMeter 无法解码 Base64。如果您使用一些自定义代码来执行此操作,我建议您首先查看此代码。
鉴于您需要施展这个魔法:
String encodedFile = response.replace("
","").replaceAll("[\n]+","");
我的期望是你的正则表达式或服务器响应很糟糕
- 考虑到您使用基于脚本的后处理器,您不需要这个“正则表达式”临时步骤,您应该能够通过
data
简写从 Beanshell PostProcessor 访问父采样器响应
因此,您的出色脚本可以优化为:
FileUtils.writeByteArrayToFile(new File("C:\\Users\\user\\Desktop\\decodedFile.pdf"), Base64.decodeBase64(data));
作为后备选项,您可以使用 OS Process Sampler 执行此 decb64.exe
程序。 。
关于java - JMeter 未正确解码 base64 - 导致空白 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43070577/