mirror of
https://gitee.com/durcframework/SOP.git
synced 2025-08-11 12:56:28 +08:00
java-sdk添加文件下载示例
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
## 日常更新
|
||||
|
||||
- 2025-06-11:java-sdk添加文件下载示例
|
||||
- 2025-06-01:OpenContext添加charset字段
|
||||
- 2025-05-18:修复网关registerAddress配置
|
||||
- 2025-05-11:
|
||||
|
@@ -33,6 +33,11 @@
|
||||
<artifactId>commons-logging</artifactId>
|
||||
<version>1.3.3</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-io</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
<version>2.17.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
|
@@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.alibaba.fastjson.parser.Feature;
|
||||
import com.gitee.sop.sdk.common.DataNameBuilder;
|
||||
import com.gitee.sop.sdk.common.FileResult;
|
||||
import com.gitee.sop.sdk.common.OpenConfig;
|
||||
import com.gitee.sop.sdk.common.RequestForm;
|
||||
import com.gitee.sop.sdk.common.Result;
|
||||
@@ -11,15 +12,23 @@ import com.gitee.sop.sdk.common.SopSdkConstants;
|
||||
import com.gitee.sop.sdk.common.SopSdkErrors;
|
||||
import com.gitee.sop.sdk.exception.SdkException;
|
||||
import com.gitee.sop.sdk.request.BaseRequest;
|
||||
import com.gitee.sop.sdk.sign.SopSignException;
|
||||
import com.gitee.sop.sdk.request.DownloadAware;
|
||||
import com.gitee.sop.sdk.request.DownloadRequest;
|
||||
import com.gitee.sop.sdk.sign.SignUtil;
|
||||
import com.gitee.sop.sdk.sign.SopSignException;
|
||||
import com.gitee.sop.sdk.sign.StringUtils;
|
||||
import com.gitee.sop.sdk.util.FileUtil;
|
||||
import okhttp3.Headers;
|
||||
import okhttp3.Response;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
/**
|
||||
* 请求客户端,申明一个即可
|
||||
*
|
||||
@@ -138,6 +147,15 @@ public class OpenClient {
|
||||
return this.execute(request, null);
|
||||
}
|
||||
|
||||
public FileResult download(DownloadRequest request) {
|
||||
return download(request, null);
|
||||
}
|
||||
|
||||
public FileResult download(DownloadRequest request, String accessToken) {
|
||||
Result<FileResult> result = execute(request, accessToken);
|
||||
return result.getData();
|
||||
}
|
||||
|
||||
/**
|
||||
* 请求接口
|
||||
*
|
||||
@@ -165,17 +183,41 @@ public class OpenClient {
|
||||
|
||||
form.put(this.openConfig.getSignName(), sign);
|
||||
|
||||
String resp = doExecute(this.url, requestForm, Collections.emptyMap());
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("----------- 请求信息 -----------"
|
||||
+ "\n请求参数:" + SignUtil.getSignContent(form)
|
||||
+ "\n待签名内容:" + content
|
||||
+ "\n签名(sign):" + sign
|
||||
+ "\n----------- 返回结果 -----------"
|
||||
+ "\n" + resp
|
||||
);
|
||||
}
|
||||
return this.parseResponse(resp, request);
|
||||
|
||||
if (request instanceof DownloadAware) {
|
||||
try (Response response = openRequest.download(url, requestForm, Collections.emptyMap())) {
|
||||
Result result = new Result<>();
|
||||
FileResult fileResult = buildFileResult(response);
|
||||
result.setData(fileResult);
|
||||
return result;
|
||||
}
|
||||
} else {
|
||||
String resp = doExecute(this.url, requestForm, Collections.emptyMap());
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("----------- 返回结果 -----------" + "\n" + resp);
|
||||
}
|
||||
return this.parseResponse(resp, request);
|
||||
}
|
||||
}
|
||||
|
||||
protected FileResult buildFileResult(Response response) {
|
||||
FileResult fileResult = new FileResult();
|
||||
Headers headers = response.headers();
|
||||
InputStream inputStream = response.body().byteStream();
|
||||
try {
|
||||
fileResult.setFileData(FileUtil.toBytes(inputStream));
|
||||
} catch (IOException e) {
|
||||
log.error("文件不存在", e);
|
||||
}
|
||||
fileResult.setHeaders(headers);
|
||||
return fileResult;
|
||||
}
|
||||
|
||||
protected String doExecute(String url, RequestForm requestForm, Map<String, String> header) {
|
||||
|
@@ -81,6 +81,23 @@ public class OpenHttp {
|
||||
return response.body().string();
|
||||
}
|
||||
|
||||
/**
|
||||
* get请求
|
||||
*
|
||||
* @param url
|
||||
* @param header
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
public Response download(String url, Map<String, String> header) throws IOException {
|
||||
Request.Builder builder = new Request.Builder().url(url).get();
|
||||
// 添加header
|
||||
addHeader(builder, header);
|
||||
|
||||
Request request = builder.build();
|
||||
return httpClient.newCall(request).execute();
|
||||
}
|
||||
|
||||
/**
|
||||
* 请求json数据,contentType=application/json
|
||||
*
|
||||
@@ -135,6 +152,27 @@ public class OpenHttp {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 提交表单,下载文件
|
||||
*
|
||||
* @param url url
|
||||
* @param form 参数
|
||||
* @param header header
|
||||
* @param method 请求方式,post,get等
|
||||
* @return 返回结果
|
||||
* @throws IOException
|
||||
*/
|
||||
public Response download(String url, Map<String, String> form, Map<String, String> header, RequestMethod method) throws IOException {
|
||||
Request.Builder requestBuilder = buildRequestBuilder(url, form, method);
|
||||
// 添加header
|
||||
addHeader(requestBuilder, header);
|
||||
|
||||
Request request = requestBuilder.build();
|
||||
return httpClient
|
||||
.newCall(request)
|
||||
.execute();
|
||||
}
|
||||
|
||||
public static Request.Builder buildRequestBuilder(String url, Map<String, String> form, RequestMethod method) {
|
||||
switch (method) {
|
||||
case GET:
|
||||
@@ -188,6 +226,26 @@ public class OpenHttp {
|
||||
*/
|
||||
public String requestFile(String url, Map<String, String> form, Map<String, String> header, List<UploadFile> files)
|
||||
throws IOException {
|
||||
Response response = requestFileAndDownload(url, form, header, files);
|
||||
try {
|
||||
return response.body().string();
|
||||
} finally {
|
||||
response.close();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 提交表单,并且上传文件
|
||||
*
|
||||
* @param url
|
||||
* @param form
|
||||
* @param header
|
||||
* @param files
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
public Response requestFileAndDownload(String url, Map<String, String> form, Map<String, String> header, List<UploadFile> files)
|
||||
throws IOException {
|
||||
// 创建MultipartBody.Builder,用于添加请求的数据
|
||||
MultipartBody.Builder bodyBuilder = new MultipartBody.Builder();
|
||||
bodyBuilder.setType(MultipartBody.FORM);
|
||||
@@ -217,12 +275,7 @@ public class OpenHttp {
|
||||
addHeader(builder, header);
|
||||
|
||||
Request request = builder.build();
|
||||
Response response = httpClient.newCall(request).execute();
|
||||
try {
|
||||
return response.body().string();
|
||||
} finally {
|
||||
response.close();
|
||||
}
|
||||
return httpClient.newCall(request).execute();
|
||||
}
|
||||
|
||||
private void addHeader(Request.Builder builder, Map<String, String> header) {
|
||||
|
@@ -7,6 +7,7 @@ import com.gitee.sop.sdk.common.RequestMethod;
|
||||
import com.gitee.sop.sdk.common.Result;
|
||||
import com.gitee.sop.sdk.common.SopSdkErrors;
|
||||
import com.gitee.sop.sdk.common.UploadFile;
|
||||
import okhttp3.Response;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
@@ -50,6 +51,29 @@ public class OpenRequest {
|
||||
}
|
||||
}
|
||||
|
||||
public Response download(String url, RequestForm requestForm, Map<String, String> header) {
|
||||
try {
|
||||
Map<String, String> form = requestForm.getForm();
|
||||
List<UploadFile> files = requestForm.getFiles();
|
||||
if (files != null && files.size() > 0) {
|
||||
return openHttp.requestFileAndDownload(url, form, header, files);
|
||||
} else {
|
||||
RequestMethod requestMethod = requestForm.getRequestMethod();
|
||||
if (requestMethod == RequestMethod.GET) {
|
||||
String query = this.buildGetQueryString(form, requestForm.getCharset());
|
||||
if (query != null && query.length() > 0) {
|
||||
url = url + "?" + query;
|
||||
}
|
||||
return openHttp.download(url, header);
|
||||
} else {
|
||||
return openHttp.download(url, form, header, requestMethod);
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
protected String buildGetQueryString(Map<String, String> params, String charset) throws UnsupportedEncodingException {
|
||||
if (params == null || params.size() == 0) {
|
||||
return "";
|
||||
|
@@ -0,0 +1,17 @@
|
||||
package com.gitee.sop.sdk.common;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import okhttp3.Headers;
|
||||
|
||||
/**
|
||||
* @author 六如
|
||||
*/
|
||||
@Setter
|
||||
@Getter
|
||||
public class FileResult {
|
||||
private byte[] fileData;
|
||||
|
||||
private Headers headers;
|
||||
|
||||
}
|
@@ -50,7 +50,11 @@ public abstract class BaseRequest<T> {
|
||||
*/
|
||||
private List<UploadFile> files;
|
||||
|
||||
private Class<T> responseClass = (Class<T>) ClassUtil.getSuperClassGenricType(this.getClass(), 0);;
|
||||
private Class<T> responseClass = parseResponseClass();
|
||||
|
||||
protected Class<T> parseResponseClass() {
|
||||
return (Class<T>) ClassUtil.getSuperClassGenricType(this.getClass(), 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* 定义接口名称
|
||||
|
@@ -0,0 +1,7 @@
|
||||
package com.gitee.sop.sdk.request;
|
||||
|
||||
/**
|
||||
* @author 六如
|
||||
*/
|
||||
public interface DownloadAware {
|
||||
}
|
@@ -0,0 +1,14 @@
|
||||
package com.gitee.sop.sdk.request;
|
||||
|
||||
import com.gitee.sop.sdk.common.FileResult;
|
||||
|
||||
/**
|
||||
* @author 六如
|
||||
*/
|
||||
public abstract class DownloadRequest extends BaseRequest<FileResult> implements DownloadAware {
|
||||
|
||||
@Override
|
||||
protected Class<FileResult> parseResponseClass() {
|
||||
return FileResult.class;
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
package com.gitee.sop.sdk.request;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @author 六如
|
||||
*/
|
||||
@Data
|
||||
public class ProductDownloadModel {
|
||||
private Integer id;
|
||||
}
|
@@ -0,0 +1,15 @@
|
||||
package com.gitee.sop.sdk.request;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @author 六如
|
||||
*/
|
||||
@Data
|
||||
public class ProductDownloadRequest extends DownloadRequest {
|
||||
@Override
|
||||
protected String method() {
|
||||
return "product.download";
|
||||
}
|
||||
|
||||
}
|
@@ -1,14 +1,18 @@
|
||||
package com.gitee.sop.sdk.util;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
/**
|
||||
* 文件工具类
|
||||
*
|
||||
* @author 六如
|
||||
*/
|
||||
public class FileUtil {
|
||||
@@ -21,6 +25,7 @@ public class FileUtil {
|
||||
|
||||
/**
|
||||
* 将文件流转换成byte[]
|
||||
*
|
||||
* @param input
|
||||
* @return
|
||||
* @throws IOException
|
||||
@@ -38,6 +43,7 @@ public class FileUtil {
|
||||
|
||||
/**
|
||||
* 将文件转换成数据流
|
||||
*
|
||||
* @param file 文件
|
||||
* @return 返回数据流
|
||||
* @throws IOException
|
||||
@@ -66,4 +72,8 @@ public class FileUtil {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static String toString(InputStream inputStream, Charset charset) throws IOException {
|
||||
return IOUtils.toString(inputStream, charset);
|
||||
}
|
||||
}
|
||||
|
@@ -2,6 +2,7 @@ package com.gitee.sop.sdk;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.gitee.sop.sdk.client.OpenClient;
|
||||
import com.gitee.sop.sdk.common.FileResult;
|
||||
import com.gitee.sop.sdk.common.Result;
|
||||
import com.gitee.sop.sdk.common.UploadFile;
|
||||
import com.gitee.sop.sdk.model.DemoFileUploadModel;
|
||||
@@ -10,17 +11,22 @@ import com.gitee.sop.sdk.request.DemoFileUploadRequest;
|
||||
import com.gitee.sop.sdk.request.GetProductRequest;
|
||||
import com.gitee.sop.sdk.request.PayTradeWapPayModel;
|
||||
import com.gitee.sop.sdk.request.PayTradeWapPayRequest;
|
||||
import com.gitee.sop.sdk.request.ProductDownloadModel;
|
||||
import com.gitee.sop.sdk.request.ProductDownloadRequest;
|
||||
import com.gitee.sop.sdk.response.GetProductResponse;
|
||||
import com.gitee.sop.sdk.response.PayTradeWapPayResponse;
|
||||
import junit.framework.TestCase;
|
||||
import okhttp3.Headers;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.math.BigDecimal;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
import com.gitee.sop.sdk.response.PayTradeWapPayResponse;
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
|
||||
public class SdkTest extends TestCase {
|
||||
String url = "http://localhost:8081/api";
|
||||
@@ -87,7 +93,7 @@ public class SdkTest extends TestCase {
|
||||
|
||||
DemoFileUploadModel model = new DemoFileUploadModel();
|
||||
model.setProductName("上传文件参数");
|
||||
model.setAddTime( new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
|
||||
model.setAddTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
|
||||
request.setBizModel(model);
|
||||
|
||||
String root = System.getProperty("user.dir");
|
||||
@@ -108,4 +114,28 @@ public class SdkTest extends TestCase {
|
||||
System.out.println("--------------------");
|
||||
}
|
||||
|
||||
// 文件下载
|
||||
@Test
|
||||
public void testDownload() throws IOException {
|
||||
ProductDownloadRequest request = new ProductDownloadRequest();
|
||||
|
||||
ProductDownloadModel model = new ProductDownloadModel();
|
||||
model.setId(111);
|
||||
request.setBizModel(model);
|
||||
|
||||
FileResult result = client.download(request);
|
||||
// 文件流
|
||||
byte[] fileData = result.getFileData();
|
||||
String content = IOUtils.toString(fileData, "UTF-8");
|
||||
System.out.println("下载文件内容:" + content);
|
||||
Assert.assertEquals("abc,你好~!@#\n", content);
|
||||
|
||||
System.out.println("----- header -----");
|
||||
Headers headers = result.getHeaders();
|
||||
for (String name : headers.names()) {
|
||||
String value = headers.get(name);
|
||||
System.out.println(name + ":" + value);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user