mirror of
https://gitee.com/durcframework/SOP.git
synced 2025-08-11 21:57:56 +08:00
2.0
This commit is contained in:
@@ -5,11 +5,15 @@ import com.gitee.sop.test.alipay.AlipayApiException;
|
||||
import com.gitee.sop.test.alipay.AlipaySignature;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
@@ -74,6 +78,7 @@ public class Client {
|
||||
|
||||
/**
|
||||
* 发送请求
|
||||
*
|
||||
* @param requestBuilder 请求信息
|
||||
* @return 返回结果
|
||||
*/
|
||||
@@ -84,14 +89,21 @@ public class Client {
|
||||
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();
|
||||
String responseData = null;
|
||||
// 发送请求
|
||||
try {
|
||||
// 发送请求
|
||||
if (httpMethod == HttpTool.HTTPMethod.POST && postJson) {
|
||||
responseData = httpTool.requestJson(requestUrl, JSON.toJSONString(form), header);
|
||||
// 如果有上传文件
|
||||
if (uploadFileList != null && uploadFileList.size() > 0) {
|
||||
responseData = httpTool.requestFile(url, form, header, uploadFileList);
|
||||
} else {
|
||||
responseData = httpTool.request(requestUrl, form, header, httpMethod);
|
||||
if (httpMethod == HttpTool.HTTPMethod.POST && postJson) {
|
||||
responseData = httpTool.requestJson(requestUrl, JSON.toJSONString(form), header);
|
||||
} else {
|
||||
responseData = httpTool.request(requestUrl, form, header, httpMethod);
|
||||
}
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
@@ -120,6 +132,7 @@ public class Client {
|
||||
private Map<String, String> header;
|
||||
private boolean ignoreSign;
|
||||
private boolean postJson;
|
||||
private List<HttpTool.UploadFile> uploadFileList;
|
||||
private Callback callback;
|
||||
|
||||
/**
|
||||
@@ -210,6 +223,62 @@ public class Client {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加文件
|
||||
*
|
||||
* @param paramName 表单名称
|
||||
* @param file 文件
|
||||
* @return 返回RequestBuilder
|
||||
*/
|
||||
public RequestBuilder addFile(String paramName, File file) {
|
||||
try {
|
||||
HttpTool.UploadFile uploadFile = new HttpTool.UploadFile(paramName, file);
|
||||
getUploadFileList().add(uploadFile);
|
||||
} catch (IOException e) {
|
||||
throw new IllegalArgumentException("上传文件错误", e);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加文件
|
||||
*
|
||||
* @param paramName 表单名称
|
||||
* @param fileName 文件名称
|
||||
* @param fileInputStream 文件流
|
||||
* @return 返回RequestBuilder
|
||||
*/
|
||||
public RequestBuilder addFile(String paramName, String fileName, InputStream fileInputStream) {
|
||||
try {
|
||||
HttpTool.UploadFile uploadFile = new HttpTool.UploadFile(paramName, fileName, fileInputStream);
|
||||
getUploadFileList().add(uploadFile);
|
||||
} catch (IOException e) {
|
||||
throw new IllegalArgumentException("上传文件错误", e);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加文件
|
||||
*
|
||||
* @param paramName 表单名称
|
||||
* @param fileName 文件名称
|
||||
* @param fileData 文件数据
|
||||
* @return 返回RequestBuilder
|
||||
*/
|
||||
public RequestBuilder addFile(String paramName, String fileName, byte[] fileData) {
|
||||
HttpTool.UploadFile uploadFile = new HttpTool.UploadFile(paramName, fileName, fileData);
|
||||
getUploadFileList().add(uploadFile);
|
||||
return this;
|
||||
}
|
||||
|
||||
private List<HttpTool.UploadFile> getUploadFileList() {
|
||||
if (uploadFileList == null) {
|
||||
uploadFileList = new ArrayList<>(16);
|
||||
}
|
||||
return uploadFileList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置请求成功处理
|
||||
*
|
||||
|
@@ -204,7 +204,7 @@ public class HttpTool {
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
public String requestFile(String url, Map<String, String> form, Map<String, String> header, List<UploadFile> files)
|
||||
public String requestFile(String url, Map<String, ?> form, Map<String, String> header, List<UploadFile> files)
|
||||
throws IOException {
|
||||
// 创建MultipartBody.Builder,用于添加请求的数据
|
||||
MultipartBody.Builder bodyBuilder = new MultipartBody.Builder();
|
||||
@@ -217,9 +217,8 @@ public class HttpTool {
|
||||
);
|
||||
}
|
||||
|
||||
Set<Map.Entry<String, String>> entrySet = form.entrySet();
|
||||
for (Map.Entry<String, String> entry : entrySet) {
|
||||
bodyBuilder.addFormDataPart(entry.getKey(), entry.getValue());
|
||||
for (Map.Entry<String, ?> entry : form.entrySet()) {
|
||||
bodyBuilder.addFormDataPart(entry.getKey(), String.valueOf(entry.getValue()));
|
||||
}
|
||||
|
||||
RequestBody requestBody = bodyBuilder.build();
|
||||
|
1
sop-test/src/main/resources/file1.txt
Normal file
1
sop-test/src/main/resources/file1.txt
Normal file
@@ -0,0 +1 @@
|
||||
file1 content,内容1
|
1
sop-test/src/main/resources/file2.txt
Normal file
1
sop-test/src/main/resources/file2.txt
Normal file
@@ -0,0 +1 @@
|
||||
file2 content,内容2
|
@@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import org.junit.Assert;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.HashMap;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
@@ -142,6 +143,23 @@ public class AllInOneTest extends TestBase {
|
||||
client.execute(requestBuilder);
|
||||
}
|
||||
|
||||
/**
|
||||
* 演示文件上传
|
||||
*/
|
||||
public void testFile() {
|
||||
String root = System.getProperty("user.dir");
|
||||
Client.RequestBuilder requestBuilder = new Client.RequestBuilder()
|
||||
.method("demo.file.upload")
|
||||
.version("1.0")
|
||||
.bizContent(new BizContent().add("remark", "test file upload"))
|
||||
// 添加文件
|
||||
.addFile("file1", new File(root + "/src/main/resources/file1.txt"))
|
||||
.addFile("file2", new File(root + "/src/main/resources/file2.txt"))
|
||||
;
|
||||
|
||||
client.execute(requestBuilder);
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证中文乱码问题
|
||||
*/
|
||||
|
Reference in New Issue
Block a user