圖片轉碼 base64
致謝參考博客:
https://www.cnblogs.com/qiaoyeye/p/7218770.html
https://www.cnblogs.com/lasdaybg/p/9803988.html
https://www.cnblogs.com/jyiqing/p/10256178.html
http://www.manongjc.com/article/59838.html
一、環境
jmeter5.2.1
jdk1.8
eclipse
二、具體步驟(總體是參考的第一條博客)
1、打開eclipse建立maven工程
2、修改 pom.xml 文件
修改完 pom.xml 文件后,等一會兒(正在下載 jmeter 的相關東西)
<!-- https://mvnrepository.com/artifact/org.apache.jmeter/ApacheJMeter_core --> <dependency> <groupId>org.apache.jmeter</groupId> <artifactId>ApacheJMeter_core</artifactId> <version>5.2.1</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.jmeter/ApacheJMeter_functions --> <dependency> <groupId>org.apache.jmeter</groupId> <artifactId>ApacheJMeter_functions</artifactId> <version>5.2.1</version> </dependency>
3、新建 com.mytest.functions 包
4、新建 MyBase64 類(jmeter 函數名為 __Base64Img)
我這里和原作者不同,用的是 java.util.Base64 的包(具體原因看第二第三條博客)
base64 碼報錯,看第四條博客
package com.mytest.functions; import java.io.FileInputStream; import java.io.InputStream; import java.util.Collection; import java.util.LinkedList; import java.util.List; import org.apache.jmeter.engine.util.CompoundVariable; import org.apache.jmeter.functions.AbstractFunction; import org.apache.jmeter.functions.InvalidVariableException; import org.apache.jmeter.samplers.SampleResult; import org.apache.jmeter.samplers.Sampler; import org.apache.jmeter.threads.JMeterVariables; import java.util.Base64; public class MyBase64 extends AbstractFunction{ //自定義function的描述 private static final List<String> desc = new LinkedList<String>(); static { desc.add("圖片路徑"); } static { desc.add("圖片base64后存放變量"); } private static final String KEY = "__Base64Img"; //存放傳入參數的值的變量 private Object[] values; //描述參數 public List<String> getArgumentDesc() { // TODO Auto-generated method stub return desc; } @Override //函數的執行 public synchronized String execute(SampleResult arg0, Sampler arg1) throws InvalidVariableException { // TODO Auto-generated method stub JMeterVariables localJMeterVariables = getVariables(); String str1 = ((CompoundVariable)this.values[0]).execute(); String str2 = getImgBase64(str1); if ((localJMeterVariables != null) && (this.values.length > 1)) { String str3 = ((CompoundVariable)this.values[1]).execute().trim(); localJMeterVariables.put(str3, str2); } return str2; } @Override public String getReferenceKey() { // TODO Auto-generated method stub //提供jmeter函數助手顯示的名稱 return KEY; } @Override public synchronized void setParameters(Collection<CompoundVariable> arg0) throws InvalidVariableException { // TODO Auto-generated method stub //檢查參數的個數,支持的方法有2個,具體用法參加api: /** * protected void checkParameterCount(Collection<CompoundVariable> parameters, int count) throws InvalidVariableException Utility method to check parameter counts. Parameters: parameters - collection of parameters count - number of parameters expected * */ //----------------- /** * * protected void checkParameterCount(Collection<CompoundVariable> parameters, int min, int max) throws InvalidVariableException Utility method to check parameter counts. Parameters: parameters - collection of parameters min - minimum number of parameters allowed max - maximum number of parameters allowed * */ //checkParameterCount(arg0, 1); checkParameterCount(arg0, 1, 2); //將參數值存入變量中 this.values = arg0.toArray(); } public String getImgBase64(String filePath) { InputStream in = null; byte[] data = null; String result = null; try { in = new FileInputStream(filePath); data = new byte[in.available()]; in.read(data); in.close(); // BASE64Encoder encoder = new BASE64Encoder(); // result = encoder.encode(data); result = Base64.getEncoder().encodeToString(data); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return result; } }
5、把 MyBase64 類導出為 jar 包至 apache-jmeter-5.2.1\lib\ext 目錄下
6、打開 jmeter5.2.1 去試一試 函數能不能用
7、注意事項
有些接口可能需要 “data:img/jpg;base64,” 這種前綴部分添加上去才能正確讀取 Base64 編碼
那就在 jmeter 參數填寫的時候添加上 “data:img/jpg;base64, ” 這種前綴
好,完成。