diff --git a/src/demo/src/main/java/com/example/demo/common/GlobalResult.java b/src/demo/src/main/java/com/example/demo/common/GlobalResult.java deleted file mode 100644 index 4f120c1..0000000 --- a/src/demo/src/main/java/com/example/demo/common/GlobalResult.java +++ /dev/null @@ -1,107 +0,0 @@ -package com.example.demo.common; - -/** - * @Description: 自定义响应数据结构 - * - * - * - * 200:表示成功 - * 500:表示错误,错误信息在msg字段中 - * 501:bean验证错误,不管多少个错误都以map形式返回 - * 502:拦截器拦截到用户token出错 - * 555:异常抛出信息 - */ -public class GlobalResult { - - // 响应业务状态 - private Integer status; - - // 响应消息 - private String msg; - - // 响应中的数据 - private Object data; - - private String ok; // 不使用 - - public static GlobalResult build(Integer status, String msg, Object data) { - return new GlobalResult(status, msg, data); - } - - public static GlobalResult ok(Object data) { - return new GlobalResult(data); - } - - public static GlobalResult ok() { - return new GlobalResult(null); - } - - public static GlobalResult errorMsg(String msg) { - return new GlobalResult(500, msg, null); - } - - public static GlobalResult errorMap(Object data) { - return new GlobalResult(501, "error", data); - } - - public static GlobalResult errorTokenMsg(String msg) { - return new GlobalResult(502, msg, null); - } - - public static GlobalResult errorException(String msg) { - return new GlobalResult(555, msg, null); - } - - public GlobalResult() { - - } - - public GlobalResult(Integer status, String msg, Object data) { - this.status = status; - this.msg = msg; - this.data = data; - } - - public GlobalResult(Object data) { - this.status = 200; - this.msg = "OK"; - this.data = data; - } - - public Boolean isOK() { - return this.status == 200; - } - - public Integer getStatus() { - return status; - } - - public void setStatus(Integer status) { - this.status = status; - } - - public String getMsg() { - return msg; - } - - public void setMsg(String msg) { - this.msg = msg; - } - - public Object getData() { - return data; - } - - public void setData(Object data) { - this.data = data; - } - - public String getOk() { - return ok; - } - - public void setOk(String ok) { - this.ok = ok; - } - -} \ No newline at end of file diff --git a/src/demo/src/main/java/com/example/demo/common/HttpClientUtil.java b/src/demo/src/main/java/com/example/demo/common/HttpClientUtil.java deleted file mode 100644 index 330a053..0000000 --- a/src/demo/src/main/java/com/example/demo/common/HttpClientUtil.java +++ /dev/null @@ -1,135 +0,0 @@ -package com.example.demo.common; - - - -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.client.utils.URIBuilder; -import org.apache.http.entity.ContentType; -import org.apache.http.entity.StringEntity; -import org.apache.http.impl.client.CloseableHttpClient; -import org.apache.http.impl.client.HttpClients; -import org.apache.http.message.BasicNameValuePair; -import org.apache.http.util.EntityUtils; - -import java.io.IOException; -import java.net.URI; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - -public class HttpClientUtil { - - public static String doGet(String url, Map param) { - - // 创建Httpclient对象 - CloseableHttpClient httpclient = HttpClients.createDefault(); - - String resultString = ""; - CloseableHttpResponse response = null; - try { - // 创建uri - URIBuilder builder = new URIBuilder(url); - if (param != null) { - for (String key : param.keySet()) { - builder.addParameter(key, param.get(key)); - } - } - URI uri = builder.build(); - - // 创建http GET请求 - HttpGet httpGet = new HttpGet(uri); - - // 执行请求 - response = httpclient.execute(httpGet); - // 判断返回状态是否为200 - if (response.getStatusLine().getStatusCode() == 200) { - resultString = EntityUtils.toString(response.getEntity(), "UTF-8"); - } - } catch (Exception e) { - e.printStackTrace(); - } finally { - try { - if (response != null) { - response.close(); - } - httpclient.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - return resultString; - } - - public static String doGet(String url) { - return doGet(url, null); - } - - public static String doPost(String url, Map param) { - // 创建Httpclient对象 - CloseableHttpClient httpClient = HttpClients.createDefault(); - CloseableHttpResponse response = null; - String resultString = ""; - try { - // 创建Http Post请求 - HttpPost httpPost = new HttpPost(url); - // 创建参数列表 - if (param != null) { - List paramList = new ArrayList<>(); - for (String key : param.keySet()) { - paramList.add(new BasicNameValuePair(key, param.get(key))); - } - // 模拟表单 - UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList); - httpPost.setEntity(entity); - } - // 执行http请求 - response = httpClient.execute(httpPost); - resultString = EntityUtils.toString(response.getEntity(), "utf-8"); - } catch (Exception e) { - e.printStackTrace(); - } finally { - try { - response.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - - return resultString; - } - - public static String doPost(String url) { - return doPost(url, null); - } - - public static String doPostJson(String url, String json) { - // 创建Httpclient对象 - CloseableHttpClient httpClient = HttpClients.createDefault(); - CloseableHttpResponse response = null; - String resultString = ""; - try { - // 创建Http Post请求 - HttpPost httpPost = new HttpPost(url); - // 创建请求内容 - StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON); - httpPost.setEntity(entity); - // 执行http请求 - response = httpClient.execute(httpPost); - resultString = EntityUtils.toString(response.getEntity(), "utf-8"); - } catch (Exception e) { - e.printStackTrace(); - } finally { - try { - response.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - - return resultString; - } -} \ No newline at end of file diff --git a/src/demo/src/main/java/com/example/demo/common/HttpGetUtil.java b/src/demo/src/main/java/com/example/demo/common/HttpGetUtil.java deleted file mode 100644 index 444ede9..0000000 --- a/src/demo/src/main/java/com/example/demo/common/HttpGetUtil.java +++ /dev/null @@ -1,73 +0,0 @@ -package com.example.demo.common; - -import java.io.*; -import java.net.HttpURLConnection; -import java.net.MalformedURLException; -import java.net.URL; -import java.net.URLEncoder; -import java.util.Map; - -public class HttpGetUtil { - - public static String httpRequestToString(String url, - Map params, String type) { - String result = null; - try { - InputStream is = httpRequestToStream(url, params, type); - BufferedReader in = new BufferedReader(new InputStreamReader(is, - "UTF-8")); - StringBuffer buffer = new StringBuffer(); - String line = ""; - while ((line = in.readLine()) != null) { - buffer.append(line); - } - result = buffer.toString(); - } catch (Exception e) { - return null; - } - System.out.println(result); - return result; - - } - - private static InputStream httpRequestToStream(String url, - Map params, String type) { - InputStream is = null; - try { - String parameters = ""; - boolean hasParams = false; - for (Object key : params.keySet()) { - String value = URLEncoder.encode((String) params.get(key), "UTF-8"); - parameters += key + "=" + value + "&"; - hasParams = true; - } - if (hasParams) { - parameters = parameters.substring(0, parameters.length()-1); - } - - url += "?"+ parameters; - System.out.println(url); - - URL u = new URL(url); - HttpURLConnection conn = (HttpURLConnection) u.openConnection(); - conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); - conn.setRequestProperty("Accept-Charset", "UTF-8"); - conn.setRequestProperty("contentType", "utf-8"); - conn.setConnectTimeout(50000); - conn.setReadTimeout(50000); - conn.setDoInput(true); - //设置请求方式,默认为GET - conn.setRequestMethod(type); - - is = conn.getInputStream(); - } catch (UnsupportedEncodingException e) { - e.printStackTrace(); - } catch (MalformedURLException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } - return is; - } - -} diff --git a/src/demo/src/main/java/com/example/demo/common/UploadFileTool.java b/src/demo/src/main/java/com/example/demo/common/UploadFileTool.java deleted file mode 100644 index 1669fba..0000000 --- a/src/demo/src/main/java/com/example/demo/common/UploadFileTool.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.example.demo.common; - -public class UploadFileTool { -} diff --git a/src/demo/src/main/java/com/example/demo/common/WechatUtil.java b/src/demo/src/main/java/com/example/demo/common/WechatUtil.java deleted file mode 100644 index 3271718..0000000 --- a/src/demo/src/main/java/com/example/demo/common/WechatUtil.java +++ /dev/null @@ -1,69 +0,0 @@ -package com.example.demo.common; - -import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.JSONObject; -import org.apache.shiro.codec.Base64; -import org.bouncycastle.jce.provider.BouncyCastleProvider; - -import javax.crypto.Cipher; -import javax.crypto.spec.IvParameterSpec; -import javax.crypto.spec.SecretKeySpec; -import java.security.AlgorithmParameters; -import java.security.Security; -import java.util.Arrays; -import java.util.HashMap; -import java.util.Map; - - -public class WechatUtil { - public static JSONObject getSessionKeyOrOpenId(String code) { - String requestUrl = "https://api.weixin.qq.com/sns/jscode2session"; - Map requestUrlParam = new HashMap<>(); - // https://mp.weixin.qq.com/wxopen/devprofile?action=get_profile&token=164113089&lang=zh_CN - //小程序appId - requestUrlParam.put("wx08c675f6ba5b2cdc", "wx08c675f6ba5b2cdc"); - //小程序secret - requestUrlParam.put("0c28388c09ff373d391fe66d085dd39d", "0c28388c09ff373d391fe66d085dd39d"); - //小程序端返回的code - requestUrlParam.put("js_code", code); - //默认参数 - requestUrlParam.put("grant_type", "authorization_code"); - //发送post请求读取调用微信接口获取openid用户唯一标识 - JSONObject jsonObject = JSON.parseObject(HttpClientUtil.doPost(requestUrl, requestUrlParam)); - return jsonObject; - } - - public static JSONObject getUserInfo(String encryptedData, String sessionKey, String iv) { - // 被加密的数据 - byte[] dataByte = Base64.decode(encryptedData); - // 加密秘钥 - byte[] keyByte = Base64.decode(sessionKey); - // 偏移量 - byte[] ivByte = Base64.decode(iv); - try { - // 如果密钥不足16位,那么就补足. 这个if 中的内容很重要 - int base = 16; - if (keyByte.length % base != 0) { - int groups = keyByte.length / base + (keyByte.length % base != 0 ? 1 : 0); - byte[] temp = new byte[groups * base]; - Arrays.fill(temp, (byte) 0); - System.arraycopy(keyByte, 0, temp, 0, keyByte.length); - keyByte = temp; - } - // 初始化 - Security.addProvider(new BouncyCastleProvider()); - Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding", "BC"); - SecretKeySpec spec = new SecretKeySpec(keyByte, "AES"); - AlgorithmParameters parameters = AlgorithmParameters.getInstance("AES"); - parameters.init(new IvParameterSpec(ivByte)); - cipher.init(Cipher.DECRYPT_MODE, spec, parameters);// 初始化 - byte[] resultByte = cipher.doFinal(dataByte); - if (null != resultByte && resultByte.length > 0) { - String result = new String(resultByte, "UTF-8"); - return JSON.parseObject(result); - } - } catch (Exception e) { - } - return null; - } -} diff --git a/src/demo/src/main/java/com/example/demo/common/ZipUtils.java b/src/demo/src/main/java/com/example/demo/common/ZipUtils.java deleted file mode 100644 index 63e413e..0000000 --- a/src/demo/src/main/java/com/example/demo/common/ZipUtils.java +++ /dev/null @@ -1,143 +0,0 @@ -package com.example.demo.common; - -import java.io.*; -import java.util.ArrayList; -import java.util.List; -import java.util.zip.ZipEntry; -import java.util.zip.ZipOutputStream; - -public class ZipUtils { - private static final int BUFFER_SIZE = 2 * 1024; - - /** - * 压缩成ZIP 方法1 - * - * @param srcDir 压缩文件夹路径 - * @param out 压缩文件输出流 - * @param KeepDirStructure 是否保留原来的目录结构,true:保留目录结构; - * false:所有文件跑到压缩包根目录下(注意:不保留目录结构可能会出现同名文件,会压缩失败) - * @throws RuntimeException 压缩失败会抛出运行时异常 - */ - - public static void toZip(String srcDir, OutputStream out, boolean KeepDirStructure) - throws RuntimeException { - long start = System.currentTimeMillis(); - ZipOutputStream zos = null; - try { - zos = new ZipOutputStream(out); - File sourceFile = new File(srcDir); - compress(sourceFile, zos, sourceFile.getName(), KeepDirStructure); - long end = System.currentTimeMillis(); - System.out.println("压缩完成,耗时:" + (end - start) + " ms"); - } catch (Exception e) { - throw new RuntimeException("zip error from ZipUtils", e); - } finally { - if (zos != null) { - try { - zos.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - } - } - - /** - * 压缩成ZIP 方法2 - * - * @param srcFiles 需要压缩的文件列表 - * @param out 压缩文件输出流 - * @throws RuntimeException 压缩失败会抛出运行时异常 - */ - - public static void toZip(List srcFiles, OutputStream out) throws RuntimeException { - long start = System.currentTimeMillis(); - ZipOutputStream zos = null; - try { - zos = new ZipOutputStream(out); - for (File srcFile : srcFiles) { - byte[] buf = new byte[BUFFER_SIZE]; - zos.putNextEntry(new ZipEntry(srcFile.getName())); - int len; - FileInputStream in = new FileInputStream(srcFile); - while ((len = in.read(buf)) != -1) { - zos.write(buf, 0, len); - } - zos.closeEntry(); - in.close(); - } - long end = System.currentTimeMillis(); - System.out.println("压缩完成,耗时:" + (end - start) + " ms"); - } catch (Exception e) { - throw new RuntimeException("zip error from ZipUtils", e); - } finally { - if (zos != null) { - try { - zos.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - } - } - - - /** - * 递归压缩方法 - * - * @param sourceFile 源文件 - * @param zos zip输出流 - * @param name 压缩后的名称 - * @param KeepDirStructure 是否保留原来的目录结构,true:保留目录结构; - * false:所有文件跑到压缩包根目录下(注意:不保留目录结构可能会出现同名文件,会压缩失败) - * @throws Exception - */ - - private static void compress(File sourceFile, ZipOutputStream zos, String name, - boolean KeepDirStructure) throws Exception { - byte[] buf = new byte[BUFFER_SIZE]; - if (sourceFile.isFile()) { - // 向zip输出流中添加一个zip实体,构造器中name为zip实体的文件的名字 - zos.putNextEntry(new ZipEntry(name)); - // copy文件到zip输出流中 - int len; - FileInputStream in = new FileInputStream(sourceFile); - while ((len = in.read(buf)) != -1) { - zos.write(buf, 0, len); - } - // Complete the entry - zos.closeEntry(); - in.close(); - } else { - File[] listFiles = sourceFile.listFiles(); - if (listFiles == null || listFiles.length == 0) { - // 需要保留原来的文件结构时,需要对空文件夹进行处理 - if (KeepDirStructure) { - // 空文件夹的处理 - zos.putNextEntry(new ZipEntry(name + "/")); - // 没有文件,不需要文件的copy - zos.closeEntry(); - } - } else { - for (File file : listFiles) { - // 判断是否需要保留原来的文件结构 - if (KeepDirStructure) { - // 注意:file.getName()前面需要带上父文件夹的名字加一斜杠, - // 不然最后压缩包中就不能保留原来的文件结构,即:所有文件都跑到压缩包根目录下了 - compress(file, zos, name + "/" + file.getName(), KeepDirStructure); - } else { - compress(file, zos, file.getName(), KeepDirStructure); - } - } - } - } - } - - - public static void main(String[] args) throws Exception { - /** 测试压缩方法1 */ - FileOutputStream fos1 = new FileOutputStream(new File("./test.zip")); - ZipUtils.toZip("C:\\Users\\1\\OneDrive - sliverki\\学习", fos1, true); - - } -} diff --git a/src/demo/src/main/java/com/example/demo/common/util/FormatResponseUtil.java b/src/demo/src/main/java/com/example/demo/common/util/FormatResponseUtil.java deleted file mode 100644 index 4300190..0000000 --- a/src/demo/src/main/java/com/example/demo/common/util/FormatResponseUtil.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.example.demo.common.util; - -public class FormatResponseUtil { - /** - * 请求成功,不携带数据 - */ - public static ResponseResult formatResponse() { - ResponseResult result = null; - return formatResponse(result); - } - - private static ResponseResult formatResponse(ResponseResult result) { - if (result == null) { - result = new ResponseResult(true, "请求成功", null); - } - return result; - } - - /** - * 请求成功,带数据 - */ - public static ResponseResult formatResponse(Object object) { - return new ResponseResult(true, "请求成功", object); - } - - /** - * 请求成功,携带提示信息和数据 - */ - public static ResponseResult formatResponse(String msg, Object object) { - return new ResponseResult(true, msg, object); - } - - /** - * 请求失败,返回错误和错误信息 - */ - public static ResponseResult error(Exception e) { - return new ResponseResult(false, e.getMessage()); - } - - /** - * 请求失败,返回异常信息 - */ - public static ResponseResult error(String exception) { - return new ResponseResult(false, exception); - } -} - diff --git a/src/demo/src/main/java/com/example/demo/common/util/ResponseResult.java b/src/demo/src/main/java/com/example/demo/common/util/ResponseResult.java deleted file mode 100644 index 03bc9e8..0000000 --- a/src/demo/src/main/java/com/example/demo/common/util/ResponseResult.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.example.demo.common.util; - -import lombok.Data; - -@Data -public class ResponseResult { - /** - * 请求状态 - */ - private boolean success; - /** - * 返回提示信息 - */ - private String msg; - /** - * 返回数据 - */ - private Object data; - - public ResponseResult(boolean success, String msg, Object data) { - this.success = success; - this.msg = msg; - this.data = data; - } - - public ResponseResult(boolean code, String msg) { - this.success = success; - this.msg = msg; - } - - public ResponseResult(boolean success) { - this.success = success; - } -}