code is as follows:
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.commons.codec.binary.Base64; public class ImageLocalBase64 extends AbstractFunction { // displayed parameter name private static final List<String> desc = new LinkedList(); // display function name private static final String KEY = "__ImageLocalBase64"; // parameter value private Object[] values; static { desc.add("local path for image"); desc.add("Name of variable in which to store the result (optional)"); } public String execute(SampleResult paramSampleResult, Sampler paramSampler) throws InvalidVariableException { String s = null; try { String url = ((CompoundVariable) this.values[0]).execute(); url = new String(url.getBytes("UTF-8"), "UTF-8"); InputStream inStream = new FileInputStream(url); byte[] b = ImageUtil.readInputStream(inStream); byte[] bs = Base64.encodeBase64(b); s = new String(bs); return s; } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace (); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace (); } catch (Exception e) { // TODO auto-generated catch block e.printStackTrace (); } return s; } // get parameter value public synchronized void setParameters(Collection<CompoundVariable> paramCollection) throws InvalidVariableException { // Check the parameters, I don't understand the meaning of wool, NND checkMinParameterCount(paramCollection, 1); this.values = paramCollection.toArray(); } // return function name public String getReferenceKey () { return KEY; } // return parameter name public List<String> getArgumentDesc() { return desc; } }
package org.apache.functions; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; 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.commons.codec.binary.Base64; public class ImageRemoteBase64 extends AbstractFunction { //display parameter name private static final List<String> desc = new LinkedList(); //display function name private static final String KEY = "__ImageRemoteBase64"; //parameter value private Object[] values; static { desc.add("URL for remote Image"); desc.add("Name of variable in which to store the result (optional)"); } public String execute(SampleResult paramSampleResult, Sampler paramSampler) throws InvalidVariableException { String s = null; try { String url = ((CompoundVariable)this.values[0]).execute(); s = ImageUtil.getImageFromNetByURL1(url); return s; } catch (Exception e) { // TODO auto-generated catch block e.printStackTrace (); } return s; } //get parameter value public synchronized void setParameters(Collection<CompoundVariable> paramCollection) throws InvalidVariableException { //Check the parameters, I don't understand the meaning of wool, NND checkMinParameterCount(paramCollection, 1); this.values = paramCollection.toArray(); } //return function name public String getReferenceKey () { return KEY; } //return parameter name public List<String> getArgumentDesc() { return desc; } /** * Get data from the input stream * * @param inStream * input stream * @return * @throws Exception */ public static byte[] readInputStream(InputStream inStream) throws Exception { ByteArrayOutputStream outStream = new ByteArrayOutputStream(); byte[] buffer = new byte[inStream.available()]; int len = 0; while ((len = inStream.read(buffer)) != -1) { outStream.write(buffer, 0, len); } inStream.close(); return outStream.toByteArray(); } }
package org.apache.functions; import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import org.apache.commons.codec.binary.Base64; import org.apache.commons.codec.binary.StringUtils; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; public class ImageUtil { /** * Get the byte stream of data according to the address * * @param strUrl * Internet connection address * @return */ public static String getImageFromNetByURL1(String strUrl) { HttpClient httpclient = new DefaultHttpClient(); HttpGet httpget = new HttpGet(strUrl); HttpResponse response; String s = null; try { response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); if (entity != null) { InputStream instream = entity.getContent(); byte[] b = readInputStream(instream); byte[] bs = Base64.encodeBase64(b); s = new String(bs); return s; } } catch (ClientProtocolException e) { // TODO auto-generated catch block e.printStackTrace (); } catch (IOException e) { // TODO auto-generated catch block e.printStackTrace (); } catch (Exception e) { // TODO auto-generated catch block e.printStackTrace (); } return s; } /** * Get the byte stream of data according to the address * * @param strUrl * local connection address * @return */ public static byte[] getImageFromLocalByUrl(String strUrl) { try { File imageFile = new File(strUrl); InputStream inStream = new FileInputStream(imageFile); byte[] btImg = readInputStream(inStream);// Get the binary data of the picture return btImg; } catch (Exception e) { e.printStackTrace (); } return null; } /** * Get data from the input stream * * @param inStream * input stream * @return * @throws Exception */ public static byte[] readInputStream(InputStream inStream) throws Exception { ByteArrayOutputStream outStream = new ByteArrayOutputStream(); byte[] buffer = new byte[inStream.available()]; int len = 0; while ((len = inStream.read(buffer)) != -1) { outStream.write(buffer, 0, len); } inStream.close(); return outStream.toByteArray(); } /** * Write the image to disk * * @param img * Image data stream * @param fileName * The name of the file when it was saved */ public static void writeImageToDisk(byte[] img, String zipImageUrl) { try { File file = new File(zipImageUrl); FileOutputStream fops = new FileOutputStream(file); fops.write(img); fops.flush(); fops.close(); System.out.println("The picture has been written" + zipImageUrl); } catch (Exception e) { e.printStackTrace (); } } }
Place the generated jar package in the lib\ext directory of Jmeter. Restart Jmeter and run it again -- the function helper module can see the two customized functions.
When used, it can be referenced in the following way:
${__ImageRemoteBase64(${photoPath},)
where ${photoPath} is the remote image path obtained by parameterizing the CSV Data Set Config.
Experience:
1. httpclient can automatically transcode the Chinese code in the URL string, which reduces the problem that the picture cannot be obtained because the URL contains Chinese.
2. The package name of the Jmeter custom function must contain "functions", otherwise Jmeter cannot find the custom function. Suggested package name: package org.apache.functions