jmeter图片转base64
时间: 2023-10-10 14:06:25 浏览: 215
js 图片转base64的方式(两种)
5星 · 资源好评率100%
在jmeter中,可以使用org.apache.commons.codec.binary.Base64类将图片转换为base64编码。根据提供的引用内容,以下是两种将图片转换为base64的方法:
方法1:
import org.apache.commons.codec.binary.Base64;
import java.io.*;
String imagePath = vars.get("path");
String base64String = null;
try {
InputStream inputStream = new FileInputStream(imagePath);
byte[] data = new byte[inputStream.available()];
inputStream.read(data);
inputStream.close();
base64String = Base64.encodeBase64String(data);
} catch (IOException e) {
e.printStackTrace();
}
vars.put("base64", base64String);
方法2:
import sun.misc.BASE64Encoder;
import java.io.*;
String imagePath = vars.get("path");
String base64String = null;
try {
InputStream inputStream = new FileInputStream(imagePath);
byte[] data = new byte[inputStream.available()];
inputStream.read(data);
inputStream.close();
BASE64Encoder encoder = new BASE64Encoder();
base64String = encoder.encode(data);
base64String = base64String.replaceAll("\n", "").replaceAll("\r", "");
} catch (IOException e) {
throw new RuntimeException(e);
}
vars.put("base64str", base64String)
阅读全文