java 模拟网络上传文件测试文件上传压力测试

Posted 洛阳泰山

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 模拟网络上传文件测试文件上传压力测试相关的知识,希望对你有一定的参考价值。

 单类实现代码如下


import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;

public class TestUpload 


    public static String FilePost(String url, File value) throws  Exception 
        String BOUNDARY = java.util.UUID.randomUUID().toString();
        MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, "--------------------"+BOUNDARY, Charset.defaultCharset());
        multipartEntity.addPart("categoryId",new StringBody("1",Charset.forName("UTF-8")));
        multipartEntity.addPart("storageType",new StringBody("1",Charset.forName("UTF-8")));
        multipartEntity.addPart("file", new FileBody(value));
        HttpPost request = new HttpPost(url);
        request.setEntity(multipartEntity);
        request.addHeader("Authorization","Basic c2FiZXI6c2FiZXJfc2VjcmV0");
        request.addHeader("Content-Type", "multipart/form-data; boundary=--------------------"+BOUNDARY);
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpResponse response = httpClient.execute(request);
        InputStream is = response.getEntity().getContent();
        BufferedReader in = new BufferedReader(new InputStreamReader(is));
        StringBuffer buffer = new StringBuffer();
        String line = "";
        while ((line = in.readLine()) != null) 
            buffer.append(line);
        
        System.out.println("发送消息收到的返回:"+buffer);
        return buffer.toString();
    

    public static void main(String[] args) throws Exception 
        ExecutorService es = Executors.newFixedThreadPool(20);
        while(true)
            Thread thread= new Thread(() ->
                try 
                    FilePost("http://172.16.10.201:20010/unify-resource/oss/endpoint/put-file-attach",new File("C:\\\\Users\\\\liuya\\\\Desktop\\\\图书馆.bmz"));
                 catch (Exception e) 
                    e.printStackTrace();
                
            );
            es.execute(thread);
        
    

以上是关于java 模拟网络上传文件测试文件上传压力测试的主要内容,如果未能解决你的问题,请参考以下文章

如何对 Web 表单文件上传进行压力测试?

如何对Web表单文件上传进行压力测试?

压力 - 负载测试Web应用程序

java用上传一个文件测试网速怎么写代码

Loadrunner上传文件脚本回放错误解决方法

Web自动化测试——代码篇八常用方法——上传/下载文件