mirror of
https://gitee.com/durcframework/SOP.git
synced 2025-08-12 07:02:14 +08:00
新增下载demo
This commit is contained in:
@@ -4,12 +4,16 @@ import com.gitee.sop.gatewaycommon.bean.ApiConfig;
|
|||||||
import com.gitee.sop.gatewaycommon.bean.ApiContext;
|
import com.gitee.sop.gatewaycommon.bean.ApiContext;
|
||||||
import com.gitee.sop.gatewaycommon.bean.SopConstants;
|
import com.gitee.sop.gatewaycommon.bean.SopConstants;
|
||||||
import com.gitee.sop.gatewaycommon.result.ResultExecutor;
|
import com.gitee.sop.gatewaycommon.result.ResultExecutor;
|
||||||
|
import com.netflix.util.Pair;
|
||||||
import com.netflix.zuul.context.RequestContext;
|
import com.netflix.zuul.context.RequestContext;
|
||||||
import com.netflix.zuul.exception.ZuulException;
|
import com.netflix.zuul.exception.ZuulException;
|
||||||
import org.apache.commons.io.IOUtils;
|
import org.apache.commons.io.IOUtils;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
|
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 合并微服务结果,统一返回格式
|
* 合并微服务结果,统一返回格式
|
||||||
@@ -33,6 +37,14 @@ public class PostResultFilter extends BaseZuulFilter {
|
|||||||
if (requestContext.getResponse().isCommitted()) {
|
if (requestContext.getResponse().isCommitted()) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
List<Pair<String, String>> zuulResponseHeaders = requestContext.getZuulResponseHeaders();
|
||||||
|
boolean isDownloadRequest = zuulResponseHeaders
|
||||||
|
.stream()
|
||||||
|
.anyMatch(pair -> StringUtils.contains(pair.second(), MediaType.APPLICATION_OCTET_STREAM_VALUE));
|
||||||
|
// 如果是文件下载直接返回
|
||||||
|
if (isDownloadRequest) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
InputStream responseDataStream = requestContext.getResponseDataStream();
|
InputStream responseDataStream = requestContext.getResponseDataStream();
|
||||||
ApiConfig apiConfig = ApiContext.getApiConfig();
|
ApiConfig apiConfig = ApiContext.getApiConfig();
|
||||||
ResultExecutor<RequestContext, String> resultExecutor = apiConfig.getZuulResultExecutor();
|
ResultExecutor<RequestContext, String> resultExecutor = apiConfig.getZuulResultExecutor();
|
||||||
|
@@ -0,0 +1,37 @@
|
|||||||
|
package com.gitee.sop.storyweb.controller;
|
||||||
|
|
||||||
|
import com.gitee.sop.servercommon.annotation.ApiMapping;
|
||||||
|
import com.gitee.sop.storyweb.controller.param.StoryParam;
|
||||||
|
import org.apache.commons.io.FileUtils;
|
||||||
|
import org.springframework.core.io.ClassPathResource;
|
||||||
|
import org.springframework.http.HttpHeaders;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 演示文件下载
|
||||||
|
*
|
||||||
|
* @author tanghc
|
||||||
|
*/
|
||||||
|
@Controller
|
||||||
|
public class DownloadController {
|
||||||
|
|
||||||
|
@ApiMapping(value = "story.download")
|
||||||
|
public ResponseEntity<byte[]> export(StoryParam param) throws IOException {
|
||||||
|
|
||||||
|
HttpHeaders headers = new HttpHeaders();
|
||||||
|
// 假设下载classpath下的application.properties文件
|
||||||
|
ClassPathResource resource = new ClassPathResource("/application.properties");
|
||||||
|
File file = resource.getFile();
|
||||||
|
|
||||||
|
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
|
||||||
|
headers.setContentDispositionFormData("attachment", file.getName());
|
||||||
|
|
||||||
|
return new ResponseEntity<>(FileUtils.readFileToByteArray(file), headers, HttpStatus.OK);
|
||||||
|
}
|
||||||
|
}
|
@@ -20,46 +20,108 @@ namespace SDKCSharp.Client
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class OpenClient
|
public class OpenClient
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 默认配置
|
||||||
|
/// </summary>
|
||||||
private static OpenConfig DEFAULT_CONFIG = new OpenConfig();
|
private static OpenConfig DEFAULT_CONFIG = new OpenConfig();
|
||||||
|
|
||||||
private Dictionary<string, string> header = new Dictionary<string, string>();
|
private Dictionary<string, string> header = new Dictionary<string, string>();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 接口请求url
|
||||||
|
/// </summary>
|
||||||
private string url;
|
private string url;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 平台提供的appId
|
||||||
|
/// </summary>
|
||||||
private string appId;
|
private string appId;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 开放平台提供的私钥
|
||||||
|
/// </summary>
|
||||||
private string privateKey;
|
private string privateKey;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 开放平台提供的公钥
|
||||||
|
/// </summary>
|
||||||
private string publicKeyPlatform;
|
private string publicKeyPlatform;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 配置项
|
||||||
|
/// </summary>
|
||||||
private OpenConfig openConfig;
|
private OpenConfig openConfig;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 请求对象
|
||||||
|
/// </summary>
|
||||||
private OpenRequest openRequest;
|
private OpenRequest openRequest;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 节点处理
|
||||||
|
/// </summary>
|
||||||
private DataNameBuilder dataNameBuilder;
|
private DataNameBuilder dataNameBuilder;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 构建请求客户端
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="url">接口url</param>
|
||||||
|
/// <param name="appId">平台分配的appId</param>
|
||||||
|
/// <param name="privateKey">平台分配的私钥</param>
|
||||||
public OpenClient(string url, string appId, string privateKey)
|
public OpenClient(string url, string appId, string privateKey)
|
||||||
: this(url, appId, privateKey,false, DEFAULT_CONFIG)
|
: this(url, appId, privateKey,false, DEFAULT_CONFIG)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 构建请求客户端
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="url">接口url</param>
|
||||||
|
/// <param name="appId">平台分配的appId</param>
|
||||||
|
/// <param name="privateKey">平台分配的私钥</param>
|
||||||
|
/// <param name="publicKeyPlatform">平台分配的公钥</param>
|
||||||
public OpenClient(string url, string appId, string privateKey, string publicKeyPlatform)
|
public OpenClient(string url, string appId, string privateKey, string publicKeyPlatform)
|
||||||
: this(url, appId, privateKey)
|
: this(url, appId, privateKey)
|
||||||
{
|
{
|
||||||
this.publicKeyPlatform = publicKeyPlatform;
|
this.publicKeyPlatform = publicKeyPlatform;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 构建请求客户端
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="url">接口url</param>
|
||||||
|
/// <param name="appId">平台分配的appId</param>
|
||||||
|
/// <param name="privateKey">平台分配的私钥</param>
|
||||||
|
/// <param name="priKeyFromFile">如果设置 <c>true</c> 从文件中加载私钥</param>
|
||||||
public OpenClient(string url, string appId, string privateKey, bool priKeyFromFile)
|
public OpenClient(string url, string appId, string privateKey, bool priKeyFromFile)
|
||||||
: this(url, appId, privateKey, priKeyFromFile, DEFAULT_CONFIG)
|
: this(url, appId, privateKey, priKeyFromFile, DEFAULT_CONFIG)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 构建请求客户端
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="url">接口url</param>
|
||||||
|
/// <param name="appId">平台分配的appId</param>
|
||||||
|
/// <param name="privateKey">平台分配的私钥</param>
|
||||||
|
/// <param name="priKeyFromFile">如果设置 <c>true</c> 从文件中加载私钥</param>
|
||||||
|
/// <param name="publicKeyPlatform">平台分配的公钥</param>
|
||||||
public OpenClient(string url, string appId, string privateKey, bool priKeyFromFile, string publicKeyPlatform)
|
public OpenClient(string url, string appId, string privateKey, bool priKeyFromFile, string publicKeyPlatform)
|
||||||
: this(url, appId, privateKey, priKeyFromFile)
|
: this(url, appId, privateKey, priKeyFromFile)
|
||||||
{
|
{
|
||||||
this.publicKeyPlatform = publicKeyPlatform;
|
this.publicKeyPlatform = publicKeyPlatform;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 构建请求客户端
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="url">接口url</param>
|
||||||
|
/// <param name="appId">平台分配的appId</param>
|
||||||
|
/// <param name="privateKey">平台分配的私钥</param>
|
||||||
|
/// <param name="priKeyFromFile">如果设置 <c>true</c> 从文件中加载私钥</param>
|
||||||
|
/// <param name="openConfig">配置项</param>
|
||||||
public OpenClient(string url, string appId, string privateKey,bool priKeyFromFile, OpenConfig openConfig)
|
public OpenClient(string url, string appId, string privateKey,bool priKeyFromFile, OpenConfig openConfig)
|
||||||
{
|
{
|
||||||
this.url = url;
|
this.url = url;
|
||||||
@@ -75,6 +137,15 @@ namespace SDKCSharp.Client
|
|||||||
this.dataNameBuilder = openConfig.DataNameBuilder;
|
this.dataNameBuilder = openConfig.DataNameBuilder;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 构建请求客户端
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="url">接口url</param>
|
||||||
|
/// <param name="appId">平台分配的appId</param>
|
||||||
|
/// <param name="privateKey">平台分配的私钥</param>
|
||||||
|
/// <param name="priKeyFromFile">如果设置 <c>true</c> 从文件中加载私钥</param>
|
||||||
|
/// <param name="publicKeyPlatform">平台分配的公钥</param>
|
||||||
|
/// <param name="openConfig">配置项</param>
|
||||||
public OpenClient(string url, string appId, string privateKey, bool priKeyFromFile, string publicKeyPlatform, OpenConfig openConfig)
|
public OpenClient(string url, string appId, string privateKey, bool priKeyFromFile, string publicKeyPlatform, OpenConfig openConfig)
|
||||||
: this(url, appId, privateKey, priKeyFromFile, openConfig)
|
: this(url, appId, privateKey, priKeyFromFile, openConfig)
|
||||||
{
|
{
|
||||||
@@ -203,6 +274,14 @@ namespace SDKCSharp.Client
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 构建业务json内容。
|
||||||
|
/// 假设返回的结果是:{"alipay_story_get_response":{"msg":"Success","code":"10000","name":"海底小纵队","id":1},"sign":"xxx"}
|
||||||
|
/// 将解析得到:{"msg":"Success","code":"10000","name":"海底小纵队","id":1}
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>The biz json.</returns>
|
||||||
|
/// <param name="rootNodeName">根节点名称.</param>
|
||||||
|
/// <param name="body">返回内容.</param>
|
||||||
protected virtual string BuildBizJson(string rootNodeName, string body)
|
protected virtual string BuildBizJson(string rootNodeName, string body)
|
||||||
{
|
{
|
||||||
int indexOfRootNode = body.IndexOf(rootNodeName);
|
int indexOfRootNode = body.IndexOf(rootNodeName);
|
||||||
@@ -219,6 +298,14 @@ namespace SDKCSharp.Client
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取业务结果,如下结果:{"alipay_story_get_response":{"msg":"Success","code":"10000","name":"海底小纵队","id":1},"sign":"xxx"}
|
||||||
|
/// 将返回:{"msg":"Success","code":"10000","name":"海底小纵队","id":1}
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>The json node data.</returns>
|
||||||
|
/// <param name="body">Body.</param>
|
||||||
|
/// <param name="rootNode">Root node.</param>
|
||||||
|
/// <param name="indexOfRootNode">Index of root node.</param>
|
||||||
protected virtual string BuildJsonNodeData(string body, string rootNode, int indexOfRootNode)
|
protected virtual string BuildJsonNodeData(string body, string rootNode, int indexOfRootNode)
|
||||||
{
|
{
|
||||||
int signDataStartIndex = indexOfRootNode + rootNode.Length + 2;
|
int signDataStartIndex = indexOfRootNode + rootNode.Length + 2;
|
||||||
|
@@ -13,6 +13,7 @@ import okhttp3.OkHttpClient;
|
|||||||
import okhttp3.Request;
|
import okhttp3.Request;
|
||||||
import okhttp3.RequestBody;
|
import okhttp3.RequestBody;
|
||||||
import okhttp3.Response;
|
import okhttp3.Response;
|
||||||
|
import okhttp3.ResponseBody;
|
||||||
import org.apache.commons.codec.digest.DigestUtils;
|
import org.apache.commons.codec.digest.DigestUtils;
|
||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
@@ -51,14 +52,17 @@ public class HttpTool {
|
|||||||
|
|
||||||
protected void initHttpClient(HttpToolConfig httpToolConfig) {
|
protected void initHttpClient(HttpToolConfig httpToolConfig) {
|
||||||
httpClient = new OkHttpClient.Builder()
|
httpClient = new OkHttpClient.Builder()
|
||||||
.connectTimeout(httpToolConfig.connectTimeoutSeconds, TimeUnit.SECONDS) // 设置链接超时时间,默认10秒
|
// 设置链接超时时间,默认10秒
|
||||||
|
.connectTimeout(httpToolConfig.connectTimeoutSeconds, TimeUnit.SECONDS)
|
||||||
.readTimeout(httpToolConfig.readTimeoutSeconds, TimeUnit.SECONDS)
|
.readTimeout(httpToolConfig.readTimeoutSeconds, TimeUnit.SECONDS)
|
||||||
.writeTimeout(httpToolConfig.writeTimeoutSeconds, TimeUnit.SECONDS)
|
.writeTimeout(httpToolConfig.writeTimeoutSeconds, TimeUnit.SECONDS)
|
||||||
.cookieJar(new CookieJar() {
|
.cookieJar(new CookieJar() {
|
||||||
|
@Override
|
||||||
public void saveFromResponse(HttpUrl httpUrl, List<Cookie> list) {
|
public void saveFromResponse(HttpUrl httpUrl, List<Cookie> list) {
|
||||||
cookieStore.put(httpUrl.host(), list);
|
cookieStore.put(httpUrl.host(), list);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public List<Cookie> loadForRequest(HttpUrl httpUrl) {
|
public List<Cookie> loadForRequest(HttpUrl httpUrl) {
|
||||||
List<Cookie> cookies = cookieStore.get(httpUrl.host());
|
List<Cookie> cookies = cookieStore.get(httpUrl.host());
|
||||||
return cookies != null ? cookies : new ArrayList<Cookie>();
|
return cookies != null ? cookies : new ArrayList<Cookie>();
|
||||||
@@ -204,22 +208,24 @@ public class HttpTool {
|
|||||||
* @return
|
* @return
|
||||||
* @throws IOException
|
* @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 {
|
throws IOException {
|
||||||
// 创建MultipartBody.Builder,用于添加请求的数据
|
// 创建MultipartBody.Builder,用于添加请求的数据
|
||||||
MultipartBody.Builder bodyBuilder = new MultipartBody.Builder();
|
MultipartBody.Builder bodyBuilder = new MultipartBody.Builder();
|
||||||
bodyBuilder.setType(MultipartBody.FORM);
|
bodyBuilder.setType(MultipartBody.FORM);
|
||||||
|
|
||||||
for (UploadFile uploadFile : files) {
|
for (UploadFile uploadFile : files) {
|
||||||
bodyBuilder.addFormDataPart(uploadFile.getName(), // 请求的名字
|
// 请求的名字
|
||||||
uploadFile.getFileName(), // 文件的文字,服务器端用来解析的
|
bodyBuilder.addFormDataPart(uploadFile.getName(),
|
||||||
RequestBody.create(null, uploadFile.getFileData()) // 创建RequestBody,把上传的文件放入
|
// 文件的文字,服务器端用来解析的
|
||||||
|
uploadFile.getFileName(),
|
||||||
|
// 创建RequestBody,把上传的文件放入
|
||||||
|
RequestBody.create(null, uploadFile.getFileData())
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
Set<Map.Entry<String, String>> entrySet = form.entrySet();
|
for (Map.Entry<String, ?> entry : form.entrySet()) {
|
||||||
for (Map.Entry<String, String> entry : entrySet) {
|
bodyBuilder.addFormDataPart(entry.getKey(), String.valueOf(entry.getValue()));
|
||||||
bodyBuilder.addFormDataPart(entry.getKey(), entry.getValue());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
RequestBody requestBody = bodyBuilder.build();
|
RequestBody requestBody = bodyBuilder.build();
|
||||||
@@ -238,6 +244,31 @@ public class HttpTool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 下载文件
|
||||||
|
*
|
||||||
|
* @param url 请求url
|
||||||
|
* @param form 请求数据
|
||||||
|
* @param header header
|
||||||
|
* @return 返回文件流
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
public InputStream downloadFile(String url, Map<String, ?> form, Map<String, String> header) throws IOException {
|
||||||
|
Request.Builder requestBuilder = buildRequestBuilder(url, form, HTTPMethod.GET);
|
||||||
|
// 添加header
|
||||||
|
addHeader(requestBuilder, header);
|
||||||
|
|
||||||
|
Request request = requestBuilder.build();
|
||||||
|
Response response = httpClient
|
||||||
|
.newCall(request)
|
||||||
|
.execute();
|
||||||
|
if (response.isSuccessful()) {
|
||||||
|
ResponseBody body = response.body();
|
||||||
|
return body == null ? null : body.byteStream();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
private void addHeader(Request.Builder builder, Map<String, String> header) {
|
private void addHeader(Request.Builder builder, Map<String, String> header) {
|
||||||
if (header != null) {
|
if (header != null) {
|
||||||
Set<Map.Entry<String, String>> entrySet = header.entrySet();
|
Set<Map.Entry<String, String>> entrySet = header.entrySet();
|
||||||
@@ -256,10 +287,15 @@ public class HttpTool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public enum HTTPMethod {
|
public enum HTTPMethod {
|
||||||
|
/** http GET */
|
||||||
GET,
|
GET,
|
||||||
|
/** http POST */
|
||||||
POST,
|
POST,
|
||||||
|
/** http PUT */
|
||||||
PUT,
|
PUT,
|
||||||
|
/** http HEAD */
|
||||||
HEAD,
|
HEAD,
|
||||||
|
/** http DELETE */
|
||||||
DELETE;
|
DELETE;
|
||||||
|
|
||||||
private HTTPMethod() {
|
private HTTPMethod() {
|
||||||
|
67
sop-test/src/test/java/com/gitee/sop/test/DownloadTest.java
Normal file
67
sop-test/src/test/java/com/gitee/sop/test/DownloadTest.java
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
package com.gitee.sop.test;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSON;
|
||||||
|
import com.gitee.sop.test.alipay.AlipayApiException;
|
||||||
|
import com.gitee.sop.test.alipay.AlipaySignature;
|
||||||
|
import org.apache.commons.io.IOUtils;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件下载
|
||||||
|
* @author tanghc
|
||||||
|
*/
|
||||||
|
public class DownloadTest extends TestBase{
|
||||||
|
|
||||||
|
String url = "http://localhost:8081/api"; // zuul
|
||||||
|
String appId = "2019032617262200001";
|
||||||
|
// 支付宝私钥
|
||||||
|
String privateKey = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCXJv1pQFqWNA/++OYEV7WYXwexZK/J8LY1OWlP9X0T6wHFOvxNKRvMkJ5544SbgsJpVcvRDPrcxmhPbi/sAhdO4x2PiPKIz9Yni2OtYCCeaiE056B+e1O2jXoLeXbfi9fPivJZkxH/tb4xfLkH3bA8ZAQnQsoXA0SguykMRZntF0TndUfvDrLqwhlR8r5iRdZLB6F8o8qXH6UPDfNEnf/K8wX5T4EB1b8x8QJ7Ua4GcIUqeUxGHdQpzNbJdaQvoi06lgccmL+PHzminkFYON7alj1CjDN833j7QMHdPtS9l7B67fOU/p2LAAkPMtoVBfxQt9aFj7B8rEhGCz02iJIBAgMBAAECggEARqOuIpY0v6WtJBfmR3lGIOOokLrhfJrGTLF8CiZMQha+SRJ7/wOLPlsH9SbjPlopyViTXCuYwbzn2tdABigkBHYXxpDV6CJZjzmRZ+FY3S/0POlTFElGojYUJ3CooWiVfyUMhdg5vSuOq0oCny53woFrf32zPHYGiKdvU5Djku1onbDU0Lw8w+5tguuEZ76kZ/lUcccGy5978FFmYpzY/65RHCpvLiLqYyWTtaNT1aQ/9pw4jX9HO9NfdJ9gYFK8r/2f36ZE4hxluAfeOXQfRC/WhPmiw/ReUhxPznG/WgKaa/OaRtAx3inbQ+JuCND7uuKeRe4osP2jLPHPP6AUwQKBgQDUNu3BkLoKaimjGOjCTAwtp71g1oo+k5/uEInAo7lyEwpV0EuUMwLA/HCqUgR4K9pyYV+Oyb8d6f0+Hz0BMD92I2pqlXrD7xV2WzDvyXM3s63NvorRooKcyfd9i6ccMjAyTR2qfLkxv0hlbBbsPHz4BbU63xhTJp3Ghi0/ey/1HQKBgQC2VsgqC6ykfSidZUNLmQZe3J0p/Qf9VLkfrQ+xaHapOs6AzDU2H2osuysqXTLJHsGfrwVaTs00ER2z8ljTJPBUtNtOLrwNRlvgdnzyVAKHfOgDBGwJgiwpeE9voB1oAV/mXqSaUWNnuwlOIhvQEBwekqNyWvhLqC7nCAIhj3yvNQKBgQCqYbeec56LAhWP903Zwcj9VvG7sESqXUhIkUqoOkuIBTWFFIm54QLTA1tJxDQGb98heoCIWf5x/A3xNI98RsqNBX5JON6qNWjb7/dobitti3t99v/ptDp9u8JTMC7penoryLKK0Ty3bkan95Kn9SC42YxaSghzqkt+uvfVQgiNGQKBgGxU6P2aDAt6VNwWosHSe+d2WWXt8IZBhO9d6dn0f7ORvcjmCqNKTNGgrkewMZEuVcliueJquR47IROdY8qmwqcBAN7Vg2K7r7CPlTKAWTRYMJxCT1Hi5gwJb+CZF3+IeYqsJk2NF2s0w5WJTE70k1BSvQsfIzAIDz2yE1oPHvwVAoGAA6e+xQkVH4fMEph55RJIZ5goI4Y76BSvt2N5OKZKd4HtaV+eIhM3SDsVYRLIm9ZquJHMiZQGyUGnsvrKL6AAVNK7eQZCRDk9KQz+0GKOGqku0nOZjUbAu6A2/vtXAaAuFSFx1rUQVVjFulLexkXR3KcztL1Qu2k5pB6Si0K/uwQ=";
|
||||||
|
|
||||||
|
|
||||||
|
public void testDownload() throws AlipayApiException, IOException {
|
||||||
|
// 公共请求参数
|
||||||
|
Map<String, String> params = new HashMap<String, String>();
|
||||||
|
params.put("app_id", appId);
|
||||||
|
params.put("method", "story.download");
|
||||||
|
params.put("format", "json");
|
||||||
|
params.put("charset", "utf-8");
|
||||||
|
params.put("sign_type", "RSA2");
|
||||||
|
params.put("timestamp", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
|
||||||
|
params.put("version", "1.0");
|
||||||
|
|
||||||
|
// 业务参数
|
||||||
|
Map<String, String> bizContent = new HashMap<>();
|
||||||
|
bizContent.put("id", "1");
|
||||||
|
bizContent.put("name", "葫芦娃");
|
||||||
|
|
||||||
|
params.put("biz_content", JSON.toJSONString(bizContent));
|
||||||
|
|
||||||
|
System.out.println("----------- 请求信息 -----------");
|
||||||
|
System.out.println("请求参数:" + buildParamQuery(params));
|
||||||
|
System.out.println("商户秘钥:" + privateKey);
|
||||||
|
String content = AlipaySignature.getSignContent(params);
|
||||||
|
System.out.println("待签名内容:" + content);
|
||||||
|
String sign = AlipaySignature.rsa256Sign(content, privateKey, "utf-8");
|
||||||
|
System.out.println("签名(sign):" + sign);
|
||||||
|
|
||||||
|
params.put("sign", sign);
|
||||||
|
|
||||||
|
System.out.println("----------- 返回结果 -----------");
|
||||||
|
InputStream fileInputStream = download(url, params);
|
||||||
|
String fileContent = IOUtils.toString(fileInputStream, StandardCharsets.UTF_8);
|
||||||
|
// 输出文件内容
|
||||||
|
System.out.println("文件内容:");
|
||||||
|
System.out.println(fileContent);
|
||||||
|
|
||||||
|
// 写文件到本地
|
||||||
|
//FileUtils.copyInputStreamToFile(fileInputStream, new File("D:/a.txt"));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSON;
|
|||||||
import junit.framework.TestCase;
|
import junit.framework.TestCase;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@@ -50,6 +51,20 @@ public class TestBase extends TestCase {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 下载文件
|
||||||
|
* @param url
|
||||||
|
* @param params
|
||||||
|
* @return 返回文件流
|
||||||
|
*/
|
||||||
|
public static InputStream download(String url, Map<String, String> params) {
|
||||||
|
try {
|
||||||
|
return httpTool.downloadFile(url, params, null);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException("网络请求异常", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
protected static String buildParamQuery(Map<String, String> params) {
|
protected static String buildParamQuery(Map<String, String> params) {
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
for (Map.Entry<String, String> entry : params.entrySet()) {
|
for (Map.Entry<String, String> entry : params.entrySet()) {
|
||||||
|
Reference in New Issue
Block a user