package test; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.methods.FileRequestEntity; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.jmeter.config.Arguments; import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient; import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext; import org.apache.jmeter.samplers.SampleResult; import java.io.File; import java.io.IOException; import java.net.MalformedURLException; import java.util.Map; /** * Created by MacGang on 2015/12/27. */ public class HttpSam extends AbstractJavaSamplerClient { public static String label="short_video"; private SampleResult results; public void setupTest(JavaSamplerContext arg0) { results = new SampleResult(); } public SampleResult runTest(JavaSamplerContext javaSamplerContext) { results.setSampleLabel(label); results.sampleStart(); try { Test.execute(javaSamplerContext.getParameter("ip"),javaSamplerContext.getParameter("port"),javaSamplerContext.getParameter("Authorization"),javaSamplerContext.getParameter("path")); System.out.println(javaSamplerContext.getParameter("Authorization")); results.setSuccessful(true); } catch (MalformedURLException e) { results.setSamplerData(e.getMessage()); e.printStackTrace(); results.setSuccessful(false); }finally{ results.sampleEnd(); } return results; } public Arguments getDefaultParameters(){ Arguments args = new Arguments(); args.addArgument("ip", "192.168.1.11");//设置参数,并赋予默认值0 args.addArgument("port", "8080");//设置参数,并赋予默认值0 args.addArgument("path", "C:\Users\MacGang\Desktop\url.txt");//设置参数,并赋予默认值0 args.addArgument("Authorization", "0");//设置参数,并赋予默认值0 return args; } public static void main(String[] args) { // TODO Auto-generated method stub Arguments params = new Arguments(); params.addArgument("ip", "192.168.1.11");//设置参数,并赋予默认值0 params.addArgument("port", "8080");//设置参数,并赋予默认值0 params.addArgument("Authorization", "111");//设置参数,并赋予默认值0 params.addArgument("path", "C:\Users\MacGang\Desktop\url.txt");//设置参数,并赋予默认值0 JavaSamplerContext arg0 = new JavaSamplerContext(params); HttpSam test = new HttpSam(); test.setupTest(arg0); test.runTest(arg0); System.exit(1); } } class Test { public static void execute(String ip,String port,String auth,String path) throws MalformedURLException { String url = "http://"+ip+":"+port+"/multimedia/upload.json"; System.out.println(ip+"-->"+port+"-->"+auth); HttpClient httpClient = new HttpClient(); PostMethod post= new PostMethod(url); try { post.setRequestHeader("Authorization",auth); post.setRequestEntity(new FileRequestEntity(new File(path),"application/x-www-form-urlencoded")); } catch (Exception e) { e.printStackTrace(); } try { httpClient.executeMethod(post); } catch (IOException e) { e.printStackTrace(); } } }