diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/filter/FormBodyWrapperFilterExt.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/filter/FormBodyWrapperFilterExt.java index 96fce214..8091b7b2 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/filter/FormBodyWrapperFilterExt.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/filter/FormBodyWrapperFilterExt.java @@ -11,15 +11,4 @@ public class FormBodyWrapperFilterExt extends FormBodyWrapperFilter { return BaseZuulFilter.FORM_BODY_WRAPPER_FILTER_ORDER; } - // @Override -// public boolean shouldFilter() { -// RequestContext ctx = RequestContext.getCurrentContext(); -// HttpServletRequest request = ctx.getRequest(); -// // 如果是文件上传请求,不需要包装 -// if (RequestUtil.isMultipart(request)) { -// return false; -// } else { -// return super.shouldFilter(); -// } -// } } diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/filter/Servlet30WrapperFilterExt.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/filter/Servlet30WrapperFilterExt.java index 336a2ed1..791eedf8 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/filter/Servlet30WrapperFilterExt.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/filter/Servlet30WrapperFilterExt.java @@ -11,15 +11,4 @@ public class Servlet30WrapperFilterExt extends Servlet30WrapperFilter { return BaseZuulFilter.SERVLET_30_WRAPPER_FILTER_ORDER; } - // @Override -// public boolean shouldFilter() { -// RequestContext ctx = RequestContext.getCurrentContext(); -// HttpServletRequest request = ctx.getRequest(); -// // 如果是文件上传请求,不需要包装 -// if (RequestUtil.isMultipart(request)) { -// return false; -// } else { -// return super.shouldFilter(); -// } -// } } diff --git a/sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/client/OpenRequest.java b/sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/client/OpenRequest.java index 959c3624..8c4fe67b 100644 --- a/sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/client/OpenRequest.java +++ b/sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/client/OpenRequest.java @@ -42,9 +42,7 @@ public class OpenRequest { } return openHttp.get(url, header); } else { - return openHttp.request(url, form, header, requestMethod.name()); - // 下面这种方式也可以,以application/json方式请求 - // return openHttp.requestJson(url, JSON.toJSONString(form), header); + return openHttp.requestJson(url, JSON.toJSONString(form), header); } } } catch (IOException e) { diff --git a/sop-test/pom.xml b/sop-test/pom.xml index d0e28885..32057a51 100644 --- a/sop-test/pom.xml +++ b/sop-test/pom.xml @@ -12,8 +12,6 @@ sop-test sop-test - - http://www.example.com UTF-8 @@ -23,28 +21,20 @@ - junit - junit - 4.11 - test - - - org.apache.httpcomponents - httpclient - 4.5.6 - test + com.squareup.okhttp3 + okhttp + 3.14.0 + compile commons-io commons-io 2.5 - test com.alibaba fastjson 1.2.58 - test commons-codec @@ -52,6 +42,19 @@ 1.11 compile + + org.projectlombok + lombok + 1.18.6 + provided + + + + junit + junit + 4.11 + test + diff --git a/sop-test/src/main/java/com/gitee/sop/App.java b/sop-test/src/main/java/com/gitee/sop/App.java deleted file mode 100644 index 5712fbcd..00000000 --- a/sop-test/src/main/java/com/gitee/sop/App.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.gitee.sop; - -/** - * Hello world! - * - */ -public class App -{ - public static void main( String[] args ) - { - System.out.println( "Hello World!" ); - } -} diff --git a/sop-test/src/main/java/com/gitee/sop/test/HttpTool.java b/sop-test/src/main/java/com/gitee/sop/test/HttpTool.java new file mode 100644 index 00000000..c7164566 --- /dev/null +++ b/sop-test/src/main/java/com/gitee/sop/test/HttpTool.java @@ -0,0 +1,381 @@ +package com.gitee.sop.test; + +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; +import java.util.List; +import java.util.Map; +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> cookieStore = new HashMap>(); + + private OkHttpClient httpClient; + + public HttpTool() { + this(new HttpToolConfig()); + } + + public HttpTool(HttpToolConfig httpToolConfig) { + this.initHttpClient(httpToolConfig); + } + + protected void initHttpClient(HttpToolConfig httpToolConfig) { + httpClient = new OkHttpClient.Builder() + .connectTimeout(httpToolConfig.connectTimeoutSeconds, TimeUnit.SECONDS) // 设置链接超时时间,默认10秒 + .readTimeout(httpToolConfig.readTimeoutSeconds, TimeUnit.SECONDS) + .writeTimeout(httpToolConfig.writeTimeoutSeconds, TimeUnit.SECONDS) + .cookieJar(new CookieJar() { + public void saveFromResponse(HttpUrl httpUrl, List list) { + cookieStore.put(httpUrl.host(), list); + } + + public List loadForRequest(HttpUrl httpUrl) { + List cookies = cookieStore.get(httpUrl.host()); + return cookies != null ? cookies : new ArrayList(); + } + }).build(); + } + + @Data + public static class HttpToolConfig { + /** + * 请求超时时间 + */ + private int connectTimeoutSeconds = 10; + /** + * http读取超时时间 + */ + private int readTimeoutSeconds = 10; + /** + * http写超时时间 + */ + private int writeTimeoutSeconds = 10; + } + + /** + * get请求 + * + * @param url + * @param header + * @return + * @throws IOException + */ + public String get(String url, Map header) throws IOException { + Request.Builder builder = new Request.Builder().url(url).get(); + // 添加header + addHeader(builder, header); + + Request request = builder.build(); + Response response = httpClient.newCall(request).execute(); + return response.body().string(); + } + + /** + * 提交表单 + * + * @param url url + * @param form 参数 + * @param header header + * @param method 请求方式,post,get等 + * @return + * @throws IOException + */ + public String request(String url, Map form, Map 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 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(); + try { + return response.body().string(); + } finally { + response.close(); + } + } + + public static Request.Builder buildRequestBuilder(String url, Map form, String method) { + switch (method) { + case "get": + return new Request.Builder() + .url(buildHttpUrl(url, form)) + .get(); + case "head": + return new Request.Builder() + .url(buildHttpUrl(url, form)) + .head(); + case "put": + return new Request.Builder() + .url(url) + .put(buildFormBody(form)); + case "delete": + return new Request.Builder() + .url(url) + .delete(buildFormBody(form)); + default: + return new Request.Builder() + .url(url) + .post(buildFormBody(form)); + } + } + + public static HttpUrl buildHttpUrl(String url, Map form) { + HttpUrl.Builder urlBuilder = HttpUrl.parse(url).newBuilder(); + for (Map.Entry entry : form.entrySet()) { + urlBuilder.addQueryParameter(entry.getKey(), entry.getValue()); + } + return urlBuilder.build(); + } + + public static FormBody buildFormBody(Map form) { + FormBody.Builder paramBuilder = new FormBody.Builder(StandardCharsets.UTF_8); + for (Map.Entry entry : form.entrySet()) { + paramBuilder.add(entry.getKey(), entry.getValue()); + } + return paramBuilder.build(); + } + + /** + * 提交表单,并且上传文件 + * + * @param url + * @param form + * @param header + * @param files + * @return + * @throws IOException + */ + public String requestFile(String url, Map form, Map header, List 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.create(null, uploadFile.getFileData()) // 创建RequestBody,把上传的文件放入 + ); + } + + Set> entrySet = form.entrySet(); + for (Map.Entry entry : entrySet) { + bodyBuilder.addFormDataPart(entry.getKey(), 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(); + try { + return response.body().string(); + } finally { + response.close(); + } + } + + private void addHeader(Request.Builder builder, Map header) { + if (header != null) { + Set> entrySet = header.entrySet(); + for (Map.Entry entry : entrySet) { + builder.addHeader(entry.getKey(), String.valueOf(entry.getValue())); + } + } + } + + public void setCookieStore(Map> cookieStore) { + this.cookieStore = cookieStore; + } + + public void setHttpClient(OkHttpClient httpClient) { + this.httpClient = httpClient; + } + + public enum HTTPMethod { + GET, + POST, + PUT, + HEAD, + DELETE; + + private HTTPMethod() { + } + + public String value() { + return this.name(); + } + + public static HTTPMethod fromValue(String v) { + return valueOf(v); + } + } + + /** + * 文件上传类 + * @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) { + } + } + } + } +} diff --git a/sop-test/src/main/java/com/gitee/sop/alipay/AlipayApiException.java b/sop-test/src/main/java/com/gitee/sop/test/alipay/AlipayApiException.java similarity index 96% rename from sop-test/src/main/java/com/gitee/sop/alipay/AlipayApiException.java rename to sop-test/src/main/java/com/gitee/sop/test/alipay/AlipayApiException.java index 18aa9ea8..adafd989 100644 --- a/sop-test/src/main/java/com/gitee/sop/alipay/AlipayApiException.java +++ b/sop-test/src/main/java/com/gitee/sop/test/alipay/AlipayApiException.java @@ -2,7 +2,7 @@ * Alipay.com Inc. * Copyright (c) 2004-2012 All Rights Reserved. */ -package com.gitee.sop.alipay; +package com.gitee.sop.test.alipay; /** diff --git a/sop-test/src/main/java/com/gitee/sop/alipay/AlipayConstants.java b/sop-test/src/main/java/com/gitee/sop/test/alipay/AlipayConstants.java similarity index 98% rename from sop-test/src/main/java/com/gitee/sop/alipay/AlipayConstants.java rename to sop-test/src/main/java/com/gitee/sop/test/alipay/AlipayConstants.java index 276a3022..afa209eb 100644 --- a/sop-test/src/main/java/com/gitee/sop/alipay/AlipayConstants.java +++ b/sop-test/src/main/java/com/gitee/sop/test/alipay/AlipayConstants.java @@ -2,7 +2,7 @@ * Alipay.com Inc. * Copyright (c) 2004-2012 All Rights Reserved. */ -package com.gitee.sop.alipay; +package com.gitee.sop.test.alipay; /** * diff --git a/sop-test/src/main/java/com/gitee/sop/alipay/AlipaySignature.java b/sop-test/src/main/java/com/gitee/sop/test/alipay/AlipaySignature.java similarity index 99% rename from sop-test/src/main/java/com/gitee/sop/alipay/AlipaySignature.java rename to sop-test/src/main/java/com/gitee/sop/test/alipay/AlipaySignature.java index 1eef3163..2836f113 100644 --- a/sop-test/src/main/java/com/gitee/sop/alipay/AlipaySignature.java +++ b/sop-test/src/main/java/com/gitee/sop/test/alipay/AlipaySignature.java @@ -2,7 +2,7 @@ * Alipay.com Inc. * Copyright (c) 2004-2012 All Rights Reserved. */ -package com.gitee.sop.alipay; +package com.gitee.sop.test.alipay; import org.apache.commons.codec.binary.Base64; diff --git a/sop-test/src/main/java/com/gitee/sop/alipay/StreamUtil.java b/sop-test/src/main/java/com/gitee/sop/test/alipay/StreamUtil.java similarity index 99% rename from sop-test/src/main/java/com/gitee/sop/alipay/StreamUtil.java rename to sop-test/src/main/java/com/gitee/sop/test/alipay/StreamUtil.java index c7bbff96..e32ec10d 100644 --- a/sop-test/src/main/java/com/gitee/sop/alipay/StreamUtil.java +++ b/sop-test/src/main/java/com/gitee/sop/test/alipay/StreamUtil.java @@ -2,7 +2,7 @@ * Alipay.com Inc. * Copyright (c) 2004-2012 All Rights Reserved. */ -package com.gitee.sop.alipay; +package com.gitee.sop.test.alipay; import java.io.IOException; import java.io.InputStream; diff --git a/sop-test/src/main/java/com/gitee/sop/alipay/StringUtils.java b/sop-test/src/main/java/com/gitee/sop/test/alipay/StringUtils.java similarity index 99% rename from sop-test/src/main/java/com/gitee/sop/alipay/StringUtils.java rename to sop-test/src/main/java/com/gitee/sop/test/alipay/StringUtils.java index 6dfa14da..2f6b7f70 100644 --- a/sop-test/src/main/java/com/gitee/sop/alipay/StringUtils.java +++ b/sop-test/src/main/java/com/gitee/sop/test/alipay/StringUtils.java @@ -1,4 +1,4 @@ -package com.gitee.sop.alipay; +package com.gitee.sop.test.alipay; /** * 字符串工具类。 diff --git a/sop-test/src/main/java/com/gitee/sop/taobao/Constants.java b/sop-test/src/main/java/com/gitee/sop/test/taobao/Constants.java similarity index 86% rename from sop-test/src/main/java/com/gitee/sop/taobao/Constants.java rename to sop-test/src/main/java/com/gitee/sop/test/taobao/Constants.java index 222f33b8..e2ac5ccf 100644 --- a/sop-test/src/main/java/com/gitee/sop/taobao/Constants.java +++ b/sop-test/src/main/java/com/gitee/sop/test/taobao/Constants.java @@ -1,4 +1,4 @@ -package com.gitee.sop.taobao; +package com.gitee.sop.test.taobao; /** * @author tanghc diff --git a/sop-test/src/main/java/com/gitee/sop/taobao/TaobaoSignature.java b/sop-test/src/main/java/com/gitee/sop/test/taobao/TaobaoSignature.java similarity index 97% rename from sop-test/src/main/java/com/gitee/sop/taobao/TaobaoSignature.java rename to sop-test/src/main/java/com/gitee/sop/test/taobao/TaobaoSignature.java index 2f3719be..8be4ef4e 100644 --- a/sop-test/src/main/java/com/gitee/sop/taobao/TaobaoSignature.java +++ b/sop-test/src/main/java/com/gitee/sop/test/taobao/TaobaoSignature.java @@ -1,6 +1,6 @@ -package com.gitee.sop.taobao; +package com.gitee.sop.test.taobao; -import com.gitee.sop.alipay.StringUtils; +import com.gitee.sop.test.alipay.StringUtils; import javax.crypto.Mac; import javax.crypto.SecretKey; diff --git a/sop-test/src/test/java/com/gitee/sop/TestBase.java b/sop-test/src/test/java/com/gitee/sop/TestBase.java deleted file mode 100644 index 9b6201cf..00000000 --- a/sop-test/src/test/java/com/gitee/sop/TestBase.java +++ /dev/null @@ -1,145 +0,0 @@ -package com.gitee.sop; - -import junit.framework.TestCase; -import org.apache.commons.io.IOUtils; -import org.apache.http.NameValuePair; -import org.apache.http.client.entity.UrlEncodedFormEntity; -import org.apache.http.client.methods.CloseableHttpResponse; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.client.methods.HttpPost; -import org.apache.http.impl.client.CloseableHttpClient; -import org.apache.http.impl.client.HttpClients; -import org.apache.http.message.BasicHeader; -import org.apache.http.message.BasicNameValuePair; -import org.apache.http.util.EntityUtils; - -import java.util.List; -import java.util.Map; -import java.util.stream.Collectors; - -/** - * @author tanghc - */ -public class TestBase extends TestCase { - - /** - * 发送POST请求 - * @param url - * @return JSON或者字符串 - * @throws Exception - */ - public static String post(String url, Map params) { - CloseableHttpClient client = null; - CloseableHttpResponse response = null; - try{ - /** - * 创建一个httpclient对象 - */ - client = HttpClients.createDefault(); - /** - * 创建一个post对象 - */ - HttpPost post = new HttpPost(url); - List nameValuePairs = params.entrySet().stream().map(entry -> { - return new BasicNameValuePair(entry.getKey(), String.valueOf(entry.getValue())); - }).collect(Collectors.toList()); - /** - * 包装成一个Entity对象 - */ - UrlEncodedFormEntity entity = new UrlEncodedFormEntity(nameValuePairs, "UTF-8"); - /** - * 设置请求的内容 - */ - post.setEntity(entity); - /** - * 设置请求的报文头部的编码 - */ - post.setHeader(new BasicHeader("Content-Type", "application/x-www-form-urlencoded")); - /** - * 执行post请求 - */ - response = client.execute(post); - /** - * 通过EntityUitls获取返回内容 - */ -// Header[] allHeaders = response.getAllHeaders(); -// if (allHeaders != null && allHeaders.length > 0) { -// System.out.println("----------- headers -----------"); -// for (Header allHeader : allHeaders) { -// System.out.println(allHeader); -// } -// } - return EntityUtils.toString(response.getEntity(),"UTF-8"); - }catch (Exception e){ - e.printStackTrace(); - }finally { - IOUtils.closeQuietly(client); - IOUtils.closeQuietly(response); - } - return null; - } - - /** - * 发送get请求 - * @param url - * @return JSON或者字符串 - * @throws Exception - */ - public static String get(String url, Map params) { - CloseableHttpClient client = null; - CloseableHttpResponse response = null; - try{ - /** - * 创建一个httpclient对象 - */ - client = HttpClients.createDefault(); - - List nameValuePairs = params.entrySet() - .stream() - .map(entry -> new BasicNameValuePair(entry.getKey(), String.valueOf(entry.getValue()))) - .collect(Collectors.toList()); - /** - * 包装成一个Entity对象 - */ - UrlEncodedFormEntity entity = new UrlEncodedFormEntity(nameValuePairs, "UTF-8"); - //参数转换为字符串 - String paramsStr = EntityUtils.toString(entity); - url = url + "?" + paramsStr; - /** - * 创建一个post对象 - */ - HttpGet get = new HttpGet(url); - - /** - * 执行post请求 - */ - response = client.execute(get); - /** - * 通过EntityUitls获取返回内容 - */ -// Header[] allHeaders = response.getAllHeaders(); -// if (allHeaders != null && allHeaders.length > 0) { -// System.out.println("----------- headers -----------"); -// for (Header allHeader : allHeaders) { -// System.out.println(allHeader); -// } -// } - return EntityUtils.toString(response.getEntity(),"UTF-8"); - }catch (Exception e){ - e.printStackTrace(); - }finally { - IOUtils.closeQuietly(client); - IOUtils.closeQuietly(response); - } - return null; - } - - protected String buildParamQuery(Map params) { - StringBuilder sb = new StringBuilder(); - for (Map.Entry entry : params.entrySet()) { - sb.append("&").append(entry.getKey()).append("=").append(entry.getValue()); - } - return sb.toString().substring(1); - } - -} diff --git a/sop-test/src/test/java/com/gitee/sop/AlipayClientPostTest.java b/sop-test/src/test/java/com/gitee/sop/test/AlipayClientPostTest.java similarity index 98% rename from sop-test/src/test/java/com/gitee/sop/AlipayClientPostTest.java rename to sop-test/src/test/java/com/gitee/sop/test/AlipayClientPostTest.java index 3d0dac32..fda92c02 100644 --- a/sop-test/src/test/java/com/gitee/sop/AlipayClientPostTest.java +++ b/sop-test/src/test/java/com/gitee/sop/test/AlipayClientPostTest.java @@ -1,7 +1,7 @@ -package com.gitee.sop; +package com.gitee.sop.test; import com.alibaba.fastjson.JSON; -import com.gitee.sop.alipay.AlipaySignature; +import com.gitee.sop.test.alipay.AlipaySignature; import org.junit.Test; import java.text.SimpleDateFormat; @@ -34,7 +34,7 @@ public class AlipayClientPostTest extends TestBase { */ // 这个请求会路由到story服务 @Test - public void testPost() throws Exception { + public void testGet() throws Exception { // 公共请求参数 Map params = new HashMap(); diff --git a/sop-test/src/test/java/com/gitee/sop/DubboDemoTest.java b/sop-test/src/test/java/com/gitee/sop/test/DubboDemoTest.java similarity index 97% rename from sop-test/src/test/java/com/gitee/sop/DubboDemoTest.java rename to sop-test/src/test/java/com/gitee/sop/test/DubboDemoTest.java index 63c241d3..4fbfaeed 100644 --- a/sop-test/src/test/java/com/gitee/sop/DubboDemoTest.java +++ b/sop-test/src/test/java/com/gitee/sop/test/DubboDemoTest.java @@ -1,7 +1,7 @@ -package com.gitee.sop; +package com.gitee.sop.test; import com.alibaba.fastjson.JSON; -import com.gitee.sop.alipay.AlipaySignature; +import com.gitee.sop.test.alipay.AlipaySignature; import org.junit.Test; import java.text.SimpleDateFormat; diff --git a/sop-test/src/test/java/com/gitee/sop/EasyopenClientPostTest.java b/sop-test/src/test/java/com/gitee/sop/test/EasyopenClientPostTest.java similarity index 97% rename from sop-test/src/test/java/com/gitee/sop/EasyopenClientPostTest.java rename to sop-test/src/test/java/com/gitee/sop/test/EasyopenClientPostTest.java index a1445034..d09b4d06 100644 --- a/sop-test/src/test/java/com/gitee/sop/EasyopenClientPostTest.java +++ b/sop-test/src/test/java/com/gitee/sop/test/EasyopenClientPostTest.java @@ -1,7 +1,6 @@ -package com.gitee.sop; +package com.gitee.sop.test; import com.alibaba.fastjson.JSON; -import junit.framework.TestCase; import org.apache.commons.codec.digest.DigestUtils; import org.junit.Test; diff --git a/sop-test/src/test/java/com/gitee/sop/JSR303DemoTest.java b/sop-test/src/test/java/com/gitee/sop/test/JSR303DemoTest.java similarity index 97% rename from sop-test/src/test/java/com/gitee/sop/JSR303DemoTest.java rename to sop-test/src/test/java/com/gitee/sop/test/JSR303DemoTest.java index 0b5aa3c4..cb3cbfc0 100644 --- a/sop-test/src/test/java/com/gitee/sop/JSR303DemoTest.java +++ b/sop-test/src/test/java/com/gitee/sop/test/JSR303DemoTest.java @@ -1,7 +1,7 @@ -package com.gitee.sop; +package com.gitee.sop.test; import com.alibaba.fastjson.JSON; -import com.gitee.sop.alipay.AlipaySignature; +import com.gitee.sop.test.alipay.AlipaySignature; import org.junit.Test; import java.text.SimpleDateFormat; diff --git a/sop-test/src/test/java/com/gitee/sop/LimitDemoPostTest.java b/sop-test/src/test/java/com/gitee/sop/test/LimitDemoPostTest.java similarity index 98% rename from sop-test/src/test/java/com/gitee/sop/LimitDemoPostTest.java rename to sop-test/src/test/java/com/gitee/sop/test/LimitDemoPostTest.java index 8f548fe9..ccc6223d 100644 --- a/sop-test/src/test/java/com/gitee/sop/LimitDemoPostTest.java +++ b/sop-test/src/test/java/com/gitee/sop/test/LimitDemoPostTest.java @@ -1,7 +1,7 @@ -package com.gitee.sop; +package com.gitee.sop.test; import com.alibaba.fastjson.JSON; -import com.gitee.sop.alipay.AlipaySignature; +import com.gitee.sop.test.alipay.AlipaySignature; import org.junit.Test; import java.text.SimpleDateFormat; diff --git a/sop-test/src/test/java/com/gitee/sop/LimitDemoPostTest2.java b/sop-test/src/test/java/com/gitee/sop/test/LimitDemoPostTest2.java similarity index 98% rename from sop-test/src/test/java/com/gitee/sop/LimitDemoPostTest2.java rename to sop-test/src/test/java/com/gitee/sop/test/LimitDemoPostTest2.java index 20dd8383..357424ae 100644 --- a/sop-test/src/test/java/com/gitee/sop/LimitDemoPostTest2.java +++ b/sop-test/src/test/java/com/gitee/sop/test/LimitDemoPostTest2.java @@ -1,7 +1,7 @@ -package com.gitee.sop; +package com.gitee.sop.test; import com.alibaba.fastjson.JSON; -import com.gitee.sop.alipay.AlipaySignature; +import com.gitee.sop.test.alipay.AlipaySignature; import org.junit.Test; import java.text.SimpleDateFormat; diff --git a/sop-test/src/test/java/com/gitee/sop/LimitDemoPostTest3.java b/sop-test/src/test/java/com/gitee/sop/test/LimitDemoPostTest3.java similarity index 97% rename from sop-test/src/test/java/com/gitee/sop/LimitDemoPostTest3.java rename to sop-test/src/test/java/com/gitee/sop/test/LimitDemoPostTest3.java index 40afcd9c..dda892f2 100644 --- a/sop-test/src/test/java/com/gitee/sop/LimitDemoPostTest3.java +++ b/sop-test/src/test/java/com/gitee/sop/test/LimitDemoPostTest3.java @@ -1,7 +1,7 @@ -package com.gitee.sop; +package com.gitee.sop.test; import com.alibaba.fastjson.JSON; -import com.gitee.sop.alipay.AlipaySignature; +import com.gitee.sop.test.alipay.AlipaySignature; import org.junit.Test; import java.text.SimpleDateFormat; diff --git a/sop-test/src/test/java/com/gitee/sop/LimitDemoPostTest4.java b/sop-test/src/test/java/com/gitee/sop/test/LimitDemoPostTest4.java similarity index 97% rename from sop-test/src/test/java/com/gitee/sop/LimitDemoPostTest4.java rename to sop-test/src/test/java/com/gitee/sop/test/LimitDemoPostTest4.java index 076bf620..cbc4a0f4 100644 --- a/sop-test/src/test/java/com/gitee/sop/LimitDemoPostTest4.java +++ b/sop-test/src/test/java/com/gitee/sop/test/LimitDemoPostTest4.java @@ -1,7 +1,7 @@ -package com.gitee.sop; +package com.gitee.sop.test; import com.alibaba.fastjson.JSON; -import com.gitee.sop.alipay.AlipaySignature; +import com.gitee.sop.test.alipay.AlipaySignature; import org.junit.Test; import java.text.SimpleDateFormat; diff --git a/sop-test/src/test/java/com/gitee/sop/LimitDemoPostTest5.java b/sop-test/src/test/java/com/gitee/sop/test/LimitDemoPostTest5.java similarity index 97% rename from sop-test/src/test/java/com/gitee/sop/LimitDemoPostTest5.java rename to sop-test/src/test/java/com/gitee/sop/test/LimitDemoPostTest5.java index dde4ad12..78bf9b76 100644 --- a/sop-test/src/test/java/com/gitee/sop/LimitDemoPostTest5.java +++ b/sop-test/src/test/java/com/gitee/sop/test/LimitDemoPostTest5.java @@ -1,7 +1,7 @@ -package com.gitee.sop; +package com.gitee.sop.test; import com.alibaba.fastjson.JSON; -import com.gitee.sop.alipay.AlipaySignature; +import com.gitee.sop.test.alipay.AlipaySignature; import org.junit.Test; import java.text.SimpleDateFormat; diff --git a/sop-test/src/test/java/com/gitee/sop/LimitDemoPostTest6.java b/sop-test/src/test/java/com/gitee/sop/test/LimitDemoPostTest6.java similarity index 97% rename from sop-test/src/test/java/com/gitee/sop/LimitDemoPostTest6.java rename to sop-test/src/test/java/com/gitee/sop/test/LimitDemoPostTest6.java index e77c7b73..f0228801 100644 --- a/sop-test/src/test/java/com/gitee/sop/LimitDemoPostTest6.java +++ b/sop-test/src/test/java/com/gitee/sop/test/LimitDemoPostTest6.java @@ -1,7 +1,7 @@ -package com.gitee.sop; +package com.gitee.sop.test; import com.alibaba.fastjson.JSON; -import com.gitee.sop.alipay.AlipaySignature; +import com.gitee.sop.test.alipay.AlipaySignature; import org.junit.Test; import java.text.SimpleDateFormat; diff --git a/sop-test/src/test/java/com/gitee/sop/LoadBalanceTest.java b/sop-test/src/test/java/com/gitee/sop/test/LoadBalanceTest.java similarity index 96% rename from sop-test/src/test/java/com/gitee/sop/LoadBalanceTest.java rename to sop-test/src/test/java/com/gitee/sop/test/LoadBalanceTest.java index f9bbaa3b..d82f0bbe 100644 --- a/sop-test/src/test/java/com/gitee/sop/LoadBalanceTest.java +++ b/sop-test/src/test/java/com/gitee/sop/test/LoadBalanceTest.java @@ -1,8 +1,8 @@ -package com.gitee.sop; +package com.gitee.sop.test; import com.alibaba.fastjson.JSON; -import com.gitee.sop.alipay.AlipayApiException; -import com.gitee.sop.alipay.AlipaySignature; +import com.gitee.sop.test.alipay.AlipayApiException; +import com.gitee.sop.test.alipay.AlipaySignature; import org.junit.Test; import java.text.SimpleDateFormat; diff --git a/sop-test/src/test/java/com/gitee/sop/PermissionDemoPostTest.java b/sop-test/src/test/java/com/gitee/sop/test/PermissionDemoPostTest.java similarity index 97% rename from sop-test/src/test/java/com/gitee/sop/PermissionDemoPostTest.java rename to sop-test/src/test/java/com/gitee/sop/test/PermissionDemoPostTest.java index 8a55f6d6..f7a479bb 100644 --- a/sop-test/src/test/java/com/gitee/sop/PermissionDemoPostTest.java +++ b/sop-test/src/test/java/com/gitee/sop/test/PermissionDemoPostTest.java @@ -1,7 +1,7 @@ -package com.gitee.sop; +package com.gitee.sop.test; import com.alibaba.fastjson.JSON; -import com.gitee.sop.alipay.AlipaySignature; +import com.gitee.sop.test.alipay.AlipaySignature; import org.junit.Test; import java.text.SimpleDateFormat; diff --git a/sop-test/src/test/java/com/gitee/sop/RestUrlTest.java b/sop-test/src/test/java/com/gitee/sop/test/RestUrlTest.java similarity index 97% rename from sop-test/src/test/java/com/gitee/sop/RestUrlTest.java rename to sop-test/src/test/java/com/gitee/sop/test/RestUrlTest.java index e04cb6f5..1af4393a 100644 --- a/sop-test/src/test/java/com/gitee/sop/RestUrlTest.java +++ b/sop-test/src/test/java/com/gitee/sop/test/RestUrlTest.java @@ -1,7 +1,7 @@ -package com.gitee.sop; +package com.gitee.sop.test; import com.alibaba.fastjson.JSON; -import com.gitee.sop.alipay.AlipaySignature; +import com.gitee.sop.test.alipay.AlipaySignature; import org.junit.Test; import java.text.SimpleDateFormat; diff --git a/sop-test/src/test/java/com/gitee/sop/SpringCloudGatewayClientPostTest.java b/sop-test/src/test/java/com/gitee/sop/test/SpringCloudGatewayClientPostTest.java similarity index 98% rename from sop-test/src/test/java/com/gitee/sop/SpringCloudGatewayClientPostTest.java rename to sop-test/src/test/java/com/gitee/sop/test/SpringCloudGatewayClientPostTest.java index 66a2388e..6b213bd6 100644 --- a/sop-test/src/test/java/com/gitee/sop/SpringCloudGatewayClientPostTest.java +++ b/sop-test/src/test/java/com/gitee/sop/test/SpringCloudGatewayClientPostTest.java @@ -1,7 +1,7 @@ -package com.gitee.sop; +package com.gitee.sop.test; import com.alibaba.fastjson.JSON; -import com.gitee.sop.alipay.AlipaySignature; +import com.gitee.sop.test.alipay.AlipaySignature; import org.junit.Test; import java.text.SimpleDateFormat; diff --git a/sop-test/src/test/java/com/gitee/sop/SpringmvcDemoPostTest.java b/sop-test/src/test/java/com/gitee/sop/test/SpringmvcDemoPostTest.java similarity index 97% rename from sop-test/src/test/java/com/gitee/sop/SpringmvcDemoPostTest.java rename to sop-test/src/test/java/com/gitee/sop/test/SpringmvcDemoPostTest.java index 540216cc..497700f7 100644 --- a/sop-test/src/test/java/com/gitee/sop/SpringmvcDemoPostTest.java +++ b/sop-test/src/test/java/com/gitee/sop/test/SpringmvcDemoPostTest.java @@ -1,7 +1,7 @@ -package com.gitee.sop; +package com.gitee.sop.test; import com.alibaba.fastjson.JSON; -import com.gitee.sop.alipay.AlipaySignature; +import com.gitee.sop.test.alipay.AlipaySignature; import org.junit.Test; import java.text.SimpleDateFormat; diff --git a/sop-test/src/test/java/com/gitee/sop/StoryDemoTest.java b/sop-test/src/test/java/com/gitee/sop/test/StoryDemoTest.java similarity index 98% rename from sop-test/src/test/java/com/gitee/sop/StoryDemoTest.java rename to sop-test/src/test/java/com/gitee/sop/test/StoryDemoTest.java index 2d5aa766..26536d84 100644 --- a/sop-test/src/test/java/com/gitee/sop/StoryDemoTest.java +++ b/sop-test/src/test/java/com/gitee/sop/test/StoryDemoTest.java @@ -1,7 +1,7 @@ -package com.gitee.sop; +package com.gitee.sop.test; import com.alibaba.fastjson.JSON; -import com.gitee.sop.alipay.AlipaySignature; +import com.gitee.sop.test.alipay.AlipaySignature; import org.junit.Test; import java.text.SimpleDateFormat; diff --git a/sop-test/src/test/java/com/gitee/sop/TaobaoClientPostTest.java b/sop-test/src/test/java/com/gitee/sop/test/TaobaoClientPostTest.java similarity index 96% rename from sop-test/src/test/java/com/gitee/sop/TaobaoClientPostTest.java rename to sop-test/src/test/java/com/gitee/sop/test/TaobaoClientPostTest.java index 6aed2e60..1cc31c73 100644 --- a/sop-test/src/test/java/com/gitee/sop/TaobaoClientPostTest.java +++ b/sop-test/src/test/java/com/gitee/sop/test/TaobaoClientPostTest.java @@ -1,7 +1,6 @@ -package com.gitee.sop; +package com.gitee.sop.test; -import com.alibaba.fastjson.JSON; -import com.gitee.sop.taobao.TaobaoSignature; +import com.gitee.sop.test.taobao.TaobaoSignature; import org.junit.Test; import java.text.SimpleDateFormat; diff --git a/sop-test/src/test/java/com/gitee/sop/test/TestBase.java b/sop-test/src/test/java/com/gitee/sop/test/TestBase.java new file mode 100644 index 00000000..fb96518e --- /dev/null +++ b/sop-test/src/test/java/com/gitee/sop/test/TestBase.java @@ -0,0 +1,53 @@ +package com.gitee.sop.test; + +import com.alibaba.fastjson.JSON; +import junit.framework.TestCase; + +import java.io.IOException; +import java.util.Collections; +import java.util.Map; + +/** + * @author tanghc + */ +public class TestBase extends TestCase { + + static HttpTool httpTool = new HttpTool(); + + /** + * 发送POST请求 + * + * @param url + * @return JSON或者字符串 + */ + public static String post(String url, Map params) { + try { + return httpTool.requestJson(url, JSON.toJSONString(params), Collections.emptyMap()); + } catch (IOException e) { + throw new RuntimeException("网络请求异常", e); + } + } + + /** + * 发送get请求 + * + * @param url + * @return JSON或者字符串 + */ + public static String get(String url, Map params) { + try { + return httpTool.request(url, params, Collections.emptyMap(), HttpTool.HTTPMethod.GET); + } catch (IOException e) { + throw new RuntimeException("网络请求异常", e); + } + } + + protected static String buildParamQuery(Map params) { + StringBuilder sb = new StringBuilder(); + for (Map.Entry entry : params.entrySet()) { + sb.append("&").append(entry.getKey()).append("=").append(entry.getValue()); + } + return sb.toString().substring(1); + } + +} diff --git a/sop-test/src/test/java/com/gitee/sop/ThrowExceptionDemoTest.java b/sop-test/src/test/java/com/gitee/sop/test/ThrowExceptionDemoTest.java similarity index 97% rename from sop-test/src/test/java/com/gitee/sop/ThrowExceptionDemoTest.java rename to sop-test/src/test/java/com/gitee/sop/test/ThrowExceptionDemoTest.java index ba2612e0..1e64cf36 100644 --- a/sop-test/src/test/java/com/gitee/sop/ThrowExceptionDemoTest.java +++ b/sop-test/src/test/java/com/gitee/sop/test/ThrowExceptionDemoTest.java @@ -1,7 +1,7 @@ -package com.gitee.sop; +package com.gitee.sop.test; import com.alibaba.fastjson.JSON; -import com.gitee.sop.alipay.AlipaySignature; +import com.gitee.sop.test.alipay.AlipaySignature; import org.junit.Test; import java.text.SimpleDateFormat; diff --git a/sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/bean/HttpTool.java b/sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/bean/HttpTool.java index a0ff4e60..8e0cce0e 100644 --- a/sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/bean/HttpTool.java +++ b/sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/bean/HttpTool.java @@ -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> cookieStore = new HashMap>(); private OkHttpClient httpClient; @@ -97,8 +110,35 @@ public class HttpTool { * @return * @throws IOException */ - public String request(String url, Map form, Map header, String method) throws IOException { - Request.Builder requestBuilder = buildRequestBuilder(url, form, method); + public String request(String url, Map form, Map 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 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) { + } + } + } + } } diff --git a/sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/bean/UploadFile.java b/sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/bean/UploadFile.java deleted file mode 100644 index 7045bc58..00000000 --- a/sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/bean/UploadFile.java +++ /dev/null @@ -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; - -} diff --git a/sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/controller/SandboxController.java b/sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/controller/SandboxController.java index cd89a8bc..5bfd6aa0 100644 --- a/sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/controller/SandboxController.java +++ b/sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/controller/SandboxController.java @@ -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; diff --git a/sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/util/FileUtil.java b/sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/util/FileUtil.java deleted file mode 100644 index ce4ea33e..00000000 --- a/sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/util/FileUtil.java +++ /dev/null @@ -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) { - } - } - } -}