pull/1/MERGE
tanghc 5 years ago
parent 58add8be82
commit f6ec1a6654
  1. 44
      sop-test/src/main/java/com/gitee/sop/test/Client.java
  2. 89
      sop-test/src/main/java/com/gitee/sop/test/HttpTool.java
  3. 20
      sop-test/src/test/java/com/gitee/sop/test/AllInOneTest.java

@ -117,6 +117,40 @@ public class Client {
return responseData;
}
/**
* 发送请求
*
* @param requestBuilder 请求信息
* @return 返回结果
*/
public InputStream download(RequestBuilder requestBuilder) {
RequestInfo requestInfo = requestBuilder.build(appId, privateKey);
HttpTool.HTTPMethod httpMethod = requestInfo.getHttpMethod();
boolean postJson = requestInfo.isPostJson();
Map<String, ?> form = requestInfo.getForm();
Map<String, String> header = requestInfo.getHeader();
String requestUrl = requestInfo.getUrl() != null ? requestInfo.getUrl() : url;
List<HttpTool.UploadFile> uploadFileList = requestBuilder.getUploadFileList();
InputStream responseData = null;
// 发送请求
try {
// 如果有上传文件
if (uploadFileList != null && uploadFileList.size() > 0) {
responseData = httpTool.downloadByRequestFile(url, form, header, uploadFileList);
} else {
if (httpMethod == HttpTool.HTTPMethod.POST && postJson) {
responseData = httpTool.downloadJson(requestUrl, JSON.toJSONString(form), header);
} else {
responseData = httpTool.download(requestUrl, form, header, httpMethod);
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}
return responseData;
}
public interface Callback {
/**
* 请求成功后回调
@ -127,6 +161,16 @@ public class Client {
void callback(RequestInfo requestInfo, String responseData);
}
public interface DownloadCallback {
/**
* 请求成功后回调
*
* @param requestInfo 请求信息
* @param responseData 返回结果
*/
void callback(RequestInfo requestInfo, InputStream responseData);
}
public static class RequestBuilder {
private static final String DEFAULT_VERSION = "1.0";

@ -130,6 +130,28 @@ public class HttpTool {
}
}
/**
* 下载文件
*
* @param url url
* @param form 参数
* @param header header
* @param method 请求方式postget等
* @return
* @throws IOException
*/
public InputStream download(String url, Map<String, ?> form, Map<String, String> header, HTTPMethod method) throws IOException {
Request.Builder requestBuilder = buildRequestBuilder(url, form, method);
// 添加header
addHeader(requestBuilder, header);
Request request = requestBuilder.build();
Response response = httpClient
.newCall(request)
.execute();
return response.body().byteStream();
}
/**
* 请求json数据contentType=application/json
* @param url 请求路径
@ -157,6 +179,29 @@ public class HttpTool {
}
}
/**
* 请求json数据contentType=application/json
* @param url 请求路径
* @param json json数据
* @param header header
* @return 返回响应结果
* @throws IOException
*/
public InputStream downloadJson(String url, String json, Map<String, String> header) throws IOException {
RequestBody body = RequestBody.create(MEDIA_TYPE_JSON, json);
Request.Builder requestBuilder = new Request.Builder()
.url(url)
.post(body);
// 添加header
addHeader(requestBuilder, header);
Request request = requestBuilder.build();
Response response = httpClient
.newCall(request)
.execute();
return response.body().byteStream();
}
public static Request.Builder buildRequestBuilder(String url, Map<String, ?> form, HTTPMethod method) {
switch (method) {
case GET:
@ -244,6 +289,48 @@ public class HttpTool {
}
}
/**
* 上传文件然后下载文件
*
* @param url
* @param form
* @param header
* @param files
* @return
* @throws IOException
*/
public InputStream downloadByRequestFile(String url, Map<String, ?> form, Map<String, String> header, List<UploadFile> files)
throws IOException {
// 创建MultipartBody.Builder,用于添加请求的数据
MultipartBody.Builder bodyBuilder = new MultipartBody.Builder();
bodyBuilder.setType(MultipartBody.FORM);
for (UploadFile uploadFile : files) {
// 请求的名字
bodyBuilder.addFormDataPart(uploadFile.getName(),
// 文件的文字,服务器端用来解析的
uploadFile.getFileName(),
// 创建RequestBody,把上传的文件放入
RequestBody.create(null, uploadFile.getFileData())
);
}
for (Map.Entry<String, ?> entry : form.entrySet()) {
bodyBuilder.addFormDataPart(entry.getKey(), String.valueOf(entry.getValue()));
}
RequestBody requestBody = bodyBuilder.build();
Request.Builder builder = new Request.Builder().url(url).post(requestBody);
// 添加header
addHeader(builder, header);
Request request = builder.build();
Response response = httpClient.newCall(request).execute();
return response.body().byteStream();
}
/**
* 请求数据
*
@ -318,7 +405,7 @@ public class HttpTool {
/** http DELETE */
DELETE;
private HTTPMethod() {
HTTPMethod() {
}
public String value() {

@ -2,9 +2,12 @@ package com.gitee.sop.test;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.IOUtils;
import org.junit.Assert;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Optional;
import java.util.concurrent.CountDownLatch;
@ -247,6 +250,23 @@ public class AllInOneTest extends TestBase {
client.execute(requestBuilder);
}
/**
* 下载文件
*/
public void testDownloadFile() throws IOException {
Client.RequestBuilder requestBuilder = new Client.RequestBuilder()
.method("story.download")
.version("1.0")
.bizContent(new BizContent().add("id",1).add("name","Jim"))
.httpMethod(HttpTool.HTTPMethod.GET);
// 文件流
InputStream download = client.download(requestBuilder);
String content = IOUtils.toString(download, "UTF-8");
System.out.println("下载文件内容:" + content);
Assert.assertEquals(content, "spring.profiles.active=dev");
}
/**
* 验证中文乱码问题
*/

Loading…
Cancel
Save