JMeter 没有正确解码 base64 - 结果为空白 PDF
JMeter not decoding base64 correctly - Results in blank PDF
在 JMeter 中,我在使用 RegEx Extractor 提取的 Response 中获得了一个 base64 编码的 PDF。这一切都很好。
然后我需要解码 base64 编码的文档并将其写入文件,我在 BeanShell Post 处理器中执行以下操作:
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]+","");
我的预期是你的正则表达式或服务器响应很糟糕
- 如果您使用基于脚本的 post-处理器,您将不需要这个 "regex" 中间步骤,您应该能够通过 [=12= 从 Beanshell PostProcessor 访问父采样器响应] shorthand
所以您的优秀脚本可以优化成如下内容:
FileUtils.writeByteArrayToFile(new File("C:\Users\user\Desktop\decodedFile.pdf"), Base64.decodeBase64(data));
作为备用选项,您可以使用 OS Process Sampler 执行此 decb64.exe
程序。
在 JMeter 中,我在使用 RegEx Extractor 提取的 Response 中获得了一个 base64 编码的 PDF。这一切都很好。
然后我需要解码 base64 编码的文档并将其写入文件,我在 BeanShell Post 处理器中执行以下操作:
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]+","");
我的预期是你的正则表达式或服务器响应很糟糕
- 如果您使用基于脚本的 post-处理器,您将不需要这个 "regex" 中间步骤,您应该能够通过 [=12= 从 Beanshell PostProcessor 访问父采样器响应] shorthand
所以您的优秀脚本可以优化成如下内容:
FileUtils.writeByteArrayToFile(new File("C:\Users\user\Desktop\decodedFile.pdf"), Base64.decodeBase64(data));
作为备用选项,您可以使用 OS Process Sampler 执行此 decb64.exe
程序。