软件测试学习之路

HttpClient模拟文件上传、下载

2018-01-06  本文已影响171人  乘风破浪的姐姐
1、httpclient模拟上传文件操作

(1)、传头部信息doUpload方法

public static String doUpload(String url, File file,Map<String,Object> params,Map<String,Object> header){
    String ret ="";
    HttpPost post = new HttpPost(url);
    post.setConfig(config);
    post.addHeader(HTTP.CONTENT_ENCODING,"UTF-8");
    CloseableHttpResponse response = null;
    try {
        MultipartEntityBuilder entityBuilder =  MultipartEntityBuilder.create();
        entityBuilder.addBinaryBody("file",file);
        if(params!=null){
            for(Map.Entry<String,Object> entry:params.entrySet()){
                entityBuilder.addTextBody(entry.getKey(),entry.getValue().toString());
            }
        }
        post.setEntity(entityBuilder.build());
        if(header!=null){
            for(Map.Entry<String,Object> entry:header.entrySet()){
                post.addHeader(entry.getKey(),entry.getValue().toString());
            }
        }

        response = httpclient.execute(post);
        if(response.getStatusLine().getStatusCode()==200){
            ret = EntityUtils.toString(response.getEntity(),"utf-8");
        }else{
            throw new HttpClientException("System level error, Code=[" + response.getStatusLine().getStatusCode() + "].");
        }
    } catch (IOException e) {
        e.printStackTrace();
    } catch (HttpClientException e) {
        e.printStackTrace();
    }finally {
        if(response!=null){
            try {
                response.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return  ret;
}

(2)、不传头部信息doUpload方法

public static String doUpload(String url,File file){
    return doUpload(url,file,null,null);
}

(3)、测试文件上传:

String url="http://123.58.251.183:8080/FileSever/upload.do";
String filePath = System.getProperty("user.dir")+ File.separator+"testdata"+File.separator;
File file = new File(filePath+"test1.xml");
String result = HttpUtils.doUpload(url,file);
System.out.println(result);
2、httpclient模拟下载文件操作

(1)、传头部信息 doDownload方法

public static void doDownload(String url, File descfile,Map<String,Object> header){
     HttpPost post = new HttpPost(url);
    post.setConfig(config);
    post.addHeader(HTTP.CONTENT_ENCODING,"UTF-8");
    CloseableHttpResponse response = null;
    try {
        if(header!=null){
            for(Map.Entry<String,Object> entry:header.entrySet()){
                post.addHeader(entry.getKey(),entry.getValue().toString());
            }
        }
        response = httpclient.execute(post);
        if(response.getStatusLine().getStatusCode()==200){
            FileUtils.copyToFile(response.getEntity().getContent(),descfile);
        }else{
            throw new HttpClientException("System level error, Code=[" + response.getStatusLine().getStatusCode() + "].");
        }
    } catch (IOException e) {
        e.printStackTrace();
    } catch (HttpClientException e) {
        e.printStackTrace();
    }finally {
        if(response!=null){
            try {
                response.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

(2)、不传头部信息doDownload方法

public static void doDownload(String url,File descfile){
    doDownload(url,descfile,null);
}

(3)、测试文件下载:

String url="http:// 59.110.139.20:8080/FileSever/upload.do";
String filePath = System.getProperty("user.dir")+ File.separator+"testdata"+File.separator;
File file = new File(filePath+"test1.xml");
String result = HttpUtils.doUpload(url,file);
System.out.println(result);

JSONObject  object =JSON.parseObject(result);
String fileId = object.getString("fileId");
try {
  String result2 =  HttpUtils.doGet("http://59.110.139.20:8080/FileSever/url.do?fileId="+fileId);
  JSONObject object1 = JSON.parseObject(result2);
  String downloadUrl =  object1.getString("url");
  File file1 = new File(filePath+"test2.xml");
  HttpUtils.doDownload(downloadUrl,file1);
} catch (HttpClientException e) {
    e.printStackTrace();
}
上一篇下一篇

猜你喜欢

热点阅读