注意:包名不能为Jmeter,天坑!!!
1、POM文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>Plguin.Jmeter</groupId> <artifactId>ImageBase64</artifactId> <version>0.0.1-SNAPSHOT</version> <!-- https://mvnrepository.com/artifact/org.apache.jmeter/ApacheJMeter_core --> <dependencies> <dependency> <groupId>org.apache.jmeter</groupId> <artifactId>ApacheJMeter_core</artifactId> <version>3.0</version> </dependency> <dependency> <groupId>org.apache.jmeter</groupId> <artifactId>ApacheJMeter_java</artifactId> <version>3.0</version> </dependency> </dependencies> </project>2、自定义函数:引入Image图片,进行BASE64加密
package org.apache.functions; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; 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 sun.misc.BASE64Encoder; public class ImageBase64 extends AbstractFunction { //显示的参数名字 private static final List<String> desc = new LinkedList(); //显示的函数名字 private static final String KEY = "__ImageBase64"; //参数值 private Object[] values; static { desc.add("String to calculate Base64 hash"); desc.add("Name of variable in which to store the result (optional)"); } public String execute(SampleResult paramSampleResult, Sampler paramSampler) throws InvalidVariableException { StringBuffer stBuffer = new StringBuffer(); try { String url = ((CompoundVariable)this.values[0]).execute(); <span style="white-space:pre"> </span>url = new String(url.getBytes("UTF-8"),"UTF-8"); <span style="white-space:pre"> </span>InputStream input = new FileInputStream(url); byte[] by = new byte[3072]; int status = -1; while((status = input.read(by))>-1) { stBuffer.append(new BASE64Encoder().encode(by)); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return stBuffer.toString(); } //获取参数值 public synchronized void setParameters(Collection<CompoundVariable> paramCollection) throws InvalidVariableException { //检查参数,我也搞不懂是毛线意思,NND checkMinParameterCount(paramCollection, 1); this.values = paramCollection.toArray(); } //返回函数名字 public String getReferenceKey() { return KEY; } //返回参数名字 public List<String> getArgumentDesc() { return desc; } }
3、自定义函数:加载image图片,保存byte 10进制数组
package org.apache.functions; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; 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; public class ImageByte extends AbstractFunction { private static final List<String> desc = new LinkedList(); private static final String KEY = "__ImageByte"; private Object[] values; static { desc.add("String to calculate Base64 hash"); desc.add("Name of variable in which to store the result (optional)"); } public String execute(SampleResult paramSampleResult, Sampler paramSampler) throws InvalidVariableException { StringBuffer stBuffer = new StringBuffer(); try { String url = ((CompoundVariable)this.values[0]).execute(); <span style="white-space:pre"> </span>url = new String(url.getBytes("UTF-8"),"UTF-8"); <span style="white-space:pre"> </span>InputStream input = new FileInputStream(url); byte[] by = new byte[3072]; int num = -1; stBuffer.append("["); while ((num = input.read()) > -1) { stBuffer.append(num + ","); } stBuffer.deleteCharAt(stBuffer.lastIndexOf(",")); stBuffer.append("]"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return stBuffer.toString(); } public synchronized void setParameters(Collection<CompoundVariable> paramCollection) throws InvalidVariableException { checkMinParameterCount(paramCollection, 1); this.values = paramCollection.toArray(); } public String getReferenceKey() { return KEY; } public List<String> getArgumentDesc() { return desc; } }