mirror of
https://gitee.com/durcframework/SOP.git
synced 2025-08-11 21:57:56 +08:00
1.13.1
This commit is contained in:
@@ -1,17 +1,27 @@
|
||||
package com.gitee.sop.websiteserver.bean;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import okhttp3.Cookie;
|
||||
import okhttp3.CookieJar;
|
||||
import okhttp3.FormBody;
|
||||
import okhttp3.HttpUrl;
|
||||
import okhttp3.MediaType;
|
||||
import okhttp3.MultipartBody;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.RequestBody;
|
||||
import okhttp3.Response;
|
||||
import org.apache.commons.codec.digest.DigestUtils;
|
||||
|
||||
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.io.Serializable;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
@@ -21,9 +31,12 @@ import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* http请求工具,基于OKHTTP3
|
||||
* @author tanghc
|
||||
*/
|
||||
public class HttpTool {
|
||||
private static final MediaType MEDIA_TYPE_JSON = MediaType.parse("application/json; charset=utf-8");
|
||||
|
||||
private Map<String, List<Cookie>> cookieStore = new HashMap<String, List<Cookie>>();
|
||||
|
||||
private OkHttpClient httpClient;
|
||||
@@ -97,8 +110,35 @@ public class HttpTool {
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
public String request(String url, Map<String, String> form, Map<String, String> header, String method) throws IOException {
|
||||
Request.Builder requestBuilder = buildRequestBuilder(url, form, method);
|
||||
public String request(String url, Map<String, String> form, Map<String, String> header, HTTPMethod method) throws IOException {
|
||||
Request.Builder requestBuilder = buildRequestBuilder(url, form, method.value());
|
||||
// 添加header
|
||||
addHeader(requestBuilder, header);
|
||||
|
||||
Request request = requestBuilder.build();
|
||||
Response response = httpClient
|
||||
.newCall(request)
|
||||
.execute();
|
||||
try {
|
||||
return response.body().string();
|
||||
} finally {
|
||||
response.close();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 请求json数据,contentType=application/json
|
||||
* @param url 请求路径
|
||||
* @param json json数据
|
||||
* @param header header
|
||||
* @return 返回响应结果
|
||||
* @throws IOException
|
||||
*/
|
||||
public String requestJson(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);
|
||||
|
||||
@@ -215,4 +255,124 @@ public class HttpTool {
|
||||
this.httpClient = httpClient;
|
||||
}
|
||||
|
||||
public enum HTTPMethod {
|
||||
GET,
|
||||
POST,
|
||||
PUT,
|
||||
HEAD,
|
||||
DELETE;
|
||||
|
||||
public String value() {
|
||||
return this.name();
|
||||
}
|
||||
|
||||
public static HTTPMethod fromValue(String v) {
|
||||
return valueOf(v.toUpperCase());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件上传类
|
||||
* @author tanghc
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
public static class UploadFile implements Serializable {
|
||||
private static final long serialVersionUID = -1100614660944996398L;
|
||||
|
||||
/**
|
||||
* @param name 表单名称,不能重复
|
||||
* @param file 文件
|
||||
* @throws IOException
|
||||
*/
|
||||
public UploadFile(String name, File file) throws IOException {
|
||||
this(name, file.getName(), FileUtil.toBytes(file));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param name 表单名称,不能重复
|
||||
* @param fileName 文件名
|
||||
* @param input 文件流
|
||||
* @throws IOException
|
||||
*/
|
||||
public UploadFile(String name, String fileName, InputStream input) throws IOException {
|
||||
this(name, fileName, FileUtil.toBytes(input));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param name 表单名称,不能重复
|
||||
* @param fileName 文件名
|
||||
* @param fileData 文件数据
|
||||
*/
|
||||
public UploadFile(String name, String fileName, byte[] fileData) {
|
||||
super();
|
||||
this.name = name;
|
||||
this.fileName = fileName;
|
||||
this.fileData = fileData;
|
||||
this.md5 = DigestUtils.md5Hex(fileData);
|
||||
}
|
||||
|
||||
private String name;
|
||||
private String fileName;
|
||||
private byte[] fileData;
|
||||
private String md5;
|
||||
|
||||
}
|
||||
|
||||
public static class FileUtil {
|
||||
|
||||
/**
|
||||
* The default buffer size to use.
|
||||
*/
|
||||
private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
|
||||
private static final int EOF = -1;
|
||||
|
||||
/**
|
||||
* 将文件流转换成byte[]
|
||||
* @param input
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
public static byte[] toBytes(InputStream input) throws IOException {
|
||||
ByteArrayOutputStream output = new ByteArrayOutputStream();
|
||||
int n = 0;
|
||||
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
|
||||
|
||||
while (EOF != (n = input.read(buffer))) {
|
||||
output.write(buffer, 0, n);
|
||||
}
|
||||
return output.toByteArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* 将文件转换成数据流
|
||||
* @param file 文件
|
||||
* @return 返回数据流
|
||||
* @throws IOException
|
||||
*/
|
||||
public static byte[] toBytes(File file) throws IOException {
|
||||
if (file.exists()) {
|
||||
if (file.isDirectory()) {
|
||||
throw new IOException("File '" + file + "' exists but is a directory");
|
||||
}
|
||||
if (file.canRead() == false) {
|
||||
throw new IOException("File '" + file + "' cannot be read");
|
||||
}
|
||||
} else {
|
||||
throw new FileNotFoundException("File '" + file + "' does not exist");
|
||||
}
|
||||
InputStream input = null;
|
||||
try {
|
||||
input = new FileInputStream(file);
|
||||
return toBytes(input);
|
||||
} finally {
|
||||
try {
|
||||
if (input != null) {
|
||||
input.close();
|
||||
}
|
||||
} catch (IOException ioe) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,60 +0,0 @@
|
||||
package com.gitee.sop.websiteserver.bean;
|
||||
|
||||
|
||||
import com.gitee.sop.websiteserver.util.FileUtil;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.apache.commons.codec.digest.DigestUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 文件上传类
|
||||
* @author tanghc
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
public class UploadFile implements Serializable {
|
||||
private static final long serialVersionUID = -1100614660944996398L;
|
||||
|
||||
/**
|
||||
* @param name 表单名称,不能重复
|
||||
* @param file 文件
|
||||
* @throws IOException
|
||||
*/
|
||||
public UploadFile(String name, File file) throws IOException {
|
||||
this(name, file.getName(), FileUtil.toBytes(file));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param name 表单名称,不能重复
|
||||
* @param fileName 文件名
|
||||
* @param input 文件流
|
||||
* @throws IOException
|
||||
*/
|
||||
public UploadFile(String name, String fileName, InputStream input) throws IOException {
|
||||
this(name, fileName, FileUtil.toBytes(input));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param name 表单名称,不能重复
|
||||
* @param fileName 文件名
|
||||
* @param fileData 文件数据
|
||||
*/
|
||||
public UploadFile(String name, String fileName, byte[] fileData) {
|
||||
super();
|
||||
this.name = name;
|
||||
this.fileName = fileName;
|
||||
this.fileData = fileData;
|
||||
this.md5 = DigestUtils.md5Hex(fileData);
|
||||
}
|
||||
|
||||
private String name;
|
||||
private String fileName;
|
||||
private byte[] fileData;
|
||||
private String md5;
|
||||
|
||||
}
|
@@ -1,7 +1,7 @@
|
||||
package com.gitee.sop.websiteserver.controller;
|
||||
|
||||
import com.gitee.sop.websiteserver.bean.HttpTool;
|
||||
import com.gitee.sop.websiteserver.bean.UploadFile;
|
||||
import com.gitee.sop.websiteserver.bean.HttpTool.*;
|
||||
import com.gitee.sop.websiteserver.sign.AlipayApiException;
|
||||
import com.gitee.sop.websiteserver.sign.AlipaySignature;
|
||||
import com.gitee.sop.websiteserver.util.UploadUtil;
|
||||
@@ -37,6 +37,7 @@ import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
|
||||
/**
|
||||
* 沙箱环境代理类
|
||||
* @author tanghc
|
||||
@@ -113,7 +114,7 @@ public class SandboxController {
|
||||
if (!CollectionUtils.isEmpty(files)) {
|
||||
responseData = httpTool.requestFile(url, params, Collections.emptyMap(), files);
|
||||
} else {
|
||||
responseData = httpTool.request(url, params, Collections.emptyMap(), httpMethod);
|
||||
responseData = httpTool.request(url, params, Collections.emptyMap(), HTTPMethod.fromValue(httpMethod));
|
||||
}
|
||||
result.apiResult = responseData;
|
||||
return result;
|
||||
|
@@ -1,65 +0,0 @@
|
||||
package com.gitee.sop.websiteserver.util;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
public class FileUtil {
|
||||
|
||||
/**
|
||||
* The default buffer size to use.
|
||||
*/
|
||||
private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
|
||||
private static final int EOF = -1;
|
||||
|
||||
/**
|
||||
* 将文件流转换成byte[]
|
||||
* @param input
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
public static byte[] toBytes(InputStream input) throws IOException {
|
||||
ByteArrayOutputStream output = new ByteArrayOutputStream();
|
||||
int n = 0;
|
||||
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
|
||||
|
||||
while (EOF != (n = input.read(buffer))) {
|
||||
output.write(buffer, 0, n);
|
||||
}
|
||||
return output.toByteArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* 将文件转换成数据流
|
||||
* @param file 文件
|
||||
* @return 返回数据流
|
||||
* @throws IOException
|
||||
*/
|
||||
public static byte[] toBytes(File file) throws IOException {
|
||||
if (file.exists()) {
|
||||
if (file.isDirectory()) {
|
||||
throw new IOException("File '" + file + "' exists but is a directory");
|
||||
}
|
||||
if (file.canRead() == false) {
|
||||
throw new IOException("File '" + file + "' cannot be read");
|
||||
}
|
||||
} else {
|
||||
throw new FileNotFoundException("File '" + file + "' does not exist");
|
||||
}
|
||||
InputStream input = null;
|
||||
try {
|
||||
input = new FileInputStream(file);
|
||||
return toBytes(input);
|
||||
} finally {
|
||||
try {
|
||||
if (input != null) {
|
||||
input.close();
|
||||
}
|
||||
} catch (IOException ioe) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user