如何在Jmeter中编码附件到Base64?

问题描述 投票:3回答:4

我正在尝试将文件编码为Jmeter中的Base64,以使用以下脚本测试Web服务:

String file = FileUtils.readFileToString(new File("${filepath}"), "UTF-8");
vars.put("file", new String(Base64.encodeBase64(file.getBytes("UTF-8"))));

这适用于普通/文本文件,并不适用于其他文件类型。

我怎么能让它适用于其他文件类型?

web-services jmeter base64 automated-tests jmeter-plugins
4个回答
5
投票

Jmeter有很多药水可以将变量转换为“Base64”,下面是几个选项

  1. Bean shell预处理器
  2. BeanShell PostProcessor
  3. BeanShell采样器。

下面是“bean shell”代码,它在“Bean shell预处理器”中用于将变量转换为Base64

import org.apache.commons.codec.binary.Base64;

String emailIs= vars.get("session");

byte[] encryptedUid = Base64.encodeBase64(emailIs.getBytes());
vars.put("genStringValue",new String(encryptedUid));

示例:

在Base64之前:jdwqoeendwqqm12sdw

在Base64之后:amR3cW9lZW5kd3FxbTEyc2R3

使用Jmeter转换:

enter image description here

enter image description here

enter image description here转换使用base64站点:

enter image description here


0
投票

您的问题来自于您在阅读二进制文件时将其视为文本这是错误的。

用它来读取文件:

然后在Base64中编码字节数组


0
投票

试试这个

import org.apache.commons.codec.binary.Base64;
String forEncoding = vars.get("userName") + ":" + vars.get("passWord");
byte[] token = Base64.encodeBase64(forEncoding.getBytes());
vars.put("token", new String(token));

0
投票

由于Groovy现在是每个JMeter Sampler,Pre-PostProcessor,Listener,Assertion等首选的JSR223脚本语言,因此这项任务非常简单。

def fileAsBase64 = new File("${filepath}").bytes.encodeBase64().toString()
vars.put("file", fileAsBase64)

或者作为一个班轮:

vars.put("file", new File("${filepath}").bytes.encodeBase64().toString())

而已。


0
投票

使用函数${__base64Encode(nameofthestring)}对字符串值进行编码,使用${__base64Decode(nameofthestring)}对字符串值进行解码。

© www.soinside.com 2019 - 2024. All rights reserved.