From 76490f8ec9a375b689e824f0047603e82b317586 Mon Sep 17 00:00:00 2001 From: TangZhaoZhen <207525215@qq.com> Date: Thu, 26 Oct 2023 08:15:38 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/four/common/core/utils/FastUtil.java | 54 --- .../com/four/common/core/utils/HttpUtils.java | 318 ------------------ .../com/four/common/core/utils/MsgUtil.java | 55 --- .../four/common/core/utils/TelSmsUtils.java | 87 ----- 4 files changed, 514 deletions(-) delete mode 100644 src/main/java/com/four/common/core/utils/FastUtil.java delete mode 100644 src/main/java/com/four/common/core/utils/HttpUtils.java delete mode 100644 src/main/java/com/four/common/core/utils/MsgUtil.java delete mode 100644 src/main/java/com/four/common/core/utils/TelSmsUtils.java diff --git a/src/main/java/com/four/common/core/utils/FastUtil.java b/src/main/java/com/four/common/core/utils/FastUtil.java deleted file mode 100644 index 479c931..0000000 --- a/src/main/java/com/four/common/core/utils/FastUtil.java +++ /dev/null @@ -1,54 +0,0 @@ -package com.four.common.core.utils; - -import com.github.tobato.fastdfs.domain.fdfs.StorePath; -import com.github.tobato.fastdfs.service.FastFileStorageClient; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.stereotype.Component; -import org.springframework.web.multipart.MultipartFile; - -import javax.annotation.Resource; - -/** - * @BelongsProject: 0107day02 - * @BelongsPackage: com.bw.config - * @Author: zhupengfei - * @CreateTime: 2023-02-01 08:52 - */ -@Component -public class FastUtil { - private static final Logger log = LoggerFactory.getLogger(FastUtil.class); - - @Resource - private FastFileStorageClient storageClient ; - - /** - * 上传文件 - */ - public String upload(MultipartFile multipartFile) throws Exception { - String originalFilename = multipartFile.getOriginalFilename(). - substring(multipartFile.getOriginalFilename(). - lastIndexOf(".") + 1); - StorePath storePath = this.storageClient.uploadImageAndCrtThumbImage( - multipartFile.getInputStream(), - multipartFile.getSize(),originalFilename , null); - return storePath.getFullPath() ; - } - /** - * 删除文件 - */ - public String deleteFile(String fileUrl) { - if (StringUtils.isEmpty(fileUrl)) { - log.info("fileUrl == >>文件路径为空..."); - return "文件路径不能为空"; - } - try { - StorePath storePath = StorePath.parseFromUrl(fileUrl); - storageClient.deleteFile(storePath.getGroup(), storePath.getPath()); - } catch (Exception e) { - log.error(e.getMessage()); - } - return "删除成功"; - } - -} diff --git a/src/main/java/com/four/common/core/utils/HttpUtils.java b/src/main/java/com/four/common/core/utils/HttpUtils.java deleted file mode 100644 index 39a3bcb..0000000 --- a/src/main/java/com/four/common/core/utils/HttpUtils.java +++ /dev/null @@ -1,318 +0,0 @@ -package com.four.common.core.utils; - -import com.alibaba.fastjson.JSON; -import org.apache.http.HttpResponse; -import org.apache.http.NameValuePair; -import org.apache.http.client.HttpClient; -import org.apache.http.client.entity.UrlEncodedFormEntity; -import org.apache.http.client.methods.HttpDelete; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.client.methods.HttpPost; -import org.apache.http.client.methods.HttpPut; -import org.apache.http.conn.ClientConnectionManager; -import org.apache.http.conn.scheme.Scheme; -import org.apache.http.conn.scheme.SchemeRegistry; -import org.apache.http.conn.ssl.SSLSocketFactory; -import org.apache.http.entity.ByteArrayEntity; -import org.apache.http.entity.StringEntity; -import org.apache.http.impl.client.DefaultHttpClient; -import org.apache.http.message.BasicNameValuePair; -import org.apache.http.util.EntityUtils; - -import javax.net.ssl.SSLContext; -import javax.net.ssl.TrustManager; -import javax.net.ssl.X509TrustManager; -import java.io.UnsupportedEncodingException; -import java.net.URLEncoder; -import java.security.KeyManagementException; -import java.security.NoSuchAlgorithmException; -import java.security.cert.X509Certificate; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - -public class HttpUtils { - - /** - * get - * - * @param host - * @param path - * @param method - * @param headers - * @param querys - * @return - * @throws Exception - */ - public static List doGet(String host, String path, String method, - Map headers, - Map querys - , Class clazz) - throws Exception { - HttpClient httpClient = wrapClient(host); - - HttpGet request = new HttpGet(buildUrl(host, path, querys)); - if(headers!=null){ - for (Map.Entry e : headers.entrySet()) { - request.addHeader(e.getKey(), e.getValue()); - } - - } - HttpResponse execute = httpClient.execute(request); - String s = EntityUtils.toString(execute.getEntity(),"UTF-8"); - List ts = JSON.parseArray(s, clazz); - return ts; - } - - /** - * post form - * - * @param host - * @param path - * @param method - * @param headers - * @param querys - * @param bodys - * @return - * @throws Exception - */ - public static HttpResponse doPost(String host, String path, String method, - Map headers, - Map querys, - Map bodys) - throws Exception { - HttpClient httpClient = wrapClient(host); - - HttpPost request = new HttpPost(buildUrl(host, path, querys)); - for (Map.Entry e : headers.entrySet()) { - request.addHeader(e.getKey(), e.getValue()); - } - - if (bodys != null) { - List nameValuePairList = new ArrayList(); - - for (String key : bodys.keySet()) { - nameValuePairList.add(new BasicNameValuePair(key, bodys.get(key))); - } - UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nameValuePairList, "utf-8"); - formEntity.setContentType("application/x-www-form-urlencoded; charset=UTF-8"); - request.setEntity(formEntity); - } - - return httpClient.execute(request); - } - - /** - * Post String - * - * @param host - * @param path - * @param method - * @param headers - * @param querys - * @param body - * @return - * @throws Exception - */ - public static HttpResponse doPost(String host, String path, String method, - Map headers, - Map querys, - String body) - throws Exception { - HttpClient httpClient = wrapClient(host); - - HttpPost request = new HttpPost(buildUrl(host, path, querys)); - for (Map.Entry e : headers.entrySet()) { - request.addHeader(e.getKey(), e.getValue()); - } - - if (StringUtils.isNotBlank(body)) { - request.setEntity(new StringEntity(body, "utf-8")); - } - - return httpClient.execute(request); - } - - /** - * Post stream - * - * @param host - * @param path - * @param method - * @param headers - * @param querys - * @param body - * @return - * @throws Exception - */ - public static HttpResponse doPost(String host, String path, String method, - Map headers, - Map querys, - byte[] body) - throws Exception { - HttpClient httpClient = wrapClient(host); - - HttpPost request = new HttpPost(buildUrl(host, path, querys)); - for (Map.Entry e : headers.entrySet()) { - request.addHeader(e.getKey(), e.getValue()); - } - - if (body != null) { - request.setEntity(new ByteArrayEntity(body)); - } - - return httpClient.execute(request); - } - - /** - * Put String - * @param host - * @param path - * @param method - * @param headers - * @param querys - * @param body - * @return - * @throws Exception - */ - public static HttpResponse doPut(String host, String path, String method, - Map headers, - Map querys, - String body) - throws Exception { - HttpClient httpClient = wrapClient(host); - - HttpPut request = new HttpPut(buildUrl(host, path, querys)); - for (Map.Entry e : headers.entrySet()) { - request.addHeader(e.getKey(), e.getValue()); - } - - if (StringUtils.isNotBlank(body)) { - request.setEntity(new StringEntity(body, "utf-8")); - } - - return httpClient.execute(request); - } - - /** - * Put stream - * @param host - * @param path - * @param method - * @param headers - * @param querys - * @param body - * @return - * @throws Exception - */ - public static HttpResponse doPut(String host, String path, String method, - Map headers, - Map querys, - byte[] body) - throws Exception { - HttpClient httpClient = wrapClient(host); - - HttpPut request = new HttpPut(buildUrl(host, path, querys)); - for (Map.Entry e : headers.entrySet()) { - request.addHeader(e.getKey(), e.getValue()); - } - - if (body != null) { - request.setEntity(new ByteArrayEntity(body)); - } - - return httpClient.execute(request); - } - - /** - * Delete - * - * @param host - * @param path - * @param method - * @param headers - * @param querys - * @return - * @throws Exception - */ - public static HttpResponse doDelete(String host, String path, String method, - Map headers, - Map querys) - throws Exception { - HttpClient httpClient = wrapClient(host); - - HttpDelete request = new HttpDelete(buildUrl(host, path, querys)); - for (Map.Entry e : headers.entrySet()) { - request.addHeader(e.getKey(), e.getValue()); - } - - return httpClient.execute(request); - } - - private static String buildUrl(String host, String path, Map querys) throws UnsupportedEncodingException { - StringBuilder sbUrl = new StringBuilder(); - sbUrl.append(host); - if (!StringUtils.isBlank(path)) { - sbUrl.append(path); - } - if (null != querys) { - StringBuilder sbQuery = new StringBuilder(); - for (Map.Entry query : querys.entrySet()) { - if (0 < sbQuery.length()) { - sbQuery.append("&"); - } - if (StringUtils.isBlank(query.getKey()) && !StringUtils.isBlank(query.getValue())) { - sbQuery.append(query.getValue()); - } - if (!StringUtils.isBlank(query.getKey())) { - sbQuery.append(query.getKey()); - if (!StringUtils.isBlank(query.getValue())) { - sbQuery.append("="); - sbQuery.append(URLEncoder.encode(query.getValue(), "utf-8")); - } - } - } - if (0 < sbQuery.length()) { - sbUrl.append("?").append(sbQuery); - } - } - - return sbUrl.toString(); - } - - private static HttpClient wrapClient(String host) { - HttpClient httpClient = new DefaultHttpClient(); - if (host.startsWith("https://")) { - sslClient(httpClient); - } - - return httpClient; - } - - private static void sslClient(HttpClient httpClient) { - try { - SSLContext ctx = SSLContext.getInstance("TLS"); - X509TrustManager tm = new X509TrustManager() { - public X509Certificate[] getAcceptedIssuers() { - return null; - } - public void checkClientTrusted(X509Certificate[] xcs, String str) { - - } - public void checkServerTrusted(X509Certificate[] xcs, String str) { - - } - }; - ctx.init(null, new TrustManager[] { tm }, null); - SSLSocketFactory ssf = new SSLSocketFactory(ctx); - ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); - ClientConnectionManager ccm = httpClient.getConnectionManager(); - SchemeRegistry registry = ccm.getSchemeRegistry(); - registry.register(new Scheme("https", 443, ssf)); - } catch (KeyManagementException ex) { - throw new RuntimeException(ex); - } catch (NoSuchAlgorithmException ex) { - throw new RuntimeException(ex); - } - } -} diff --git a/src/main/java/com/four/common/core/utils/MsgUtil.java b/src/main/java/com/four/common/core/utils/MsgUtil.java deleted file mode 100644 index 07fda61..0000000 --- a/src/main/java/com/four/common/core/utils/MsgUtil.java +++ /dev/null @@ -1,55 +0,0 @@ -package com.four.common.core.utils; - - -import org.apache.http.HttpResponse; -import org.apache.http.util.EntityUtils; - -import java.util.HashMap; -import java.util.Map; - -/** - * @author - * @version 1.0.0 - * @ClassName MsgUtil.java - * @Description TODO - * @createTime 2022年05月26日 15:49:00 - */ -public class MsgUtil { - - - public static void sendMsg(String phone, String code){ - String host = "https://gyytz.market.alicloudapi.com"; - String path = "/sms/smsSend"; - String method = "POST"; - String appcode = "d742f8e74e03432f92eec7267b919ee"; - Map headers = new HashMap(); - //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105 - headers.put("Authorization", "APPCODE " + appcode); - Map querys = new HashMap(); - querys.put("mobile", phone); - querys.put("param", "**code**:"+code+",**minute**:5"); - -//smsSignId(短信前缀)和templateId(短信模板),可登录国阳云控制台自助申请。参考文档:http://help.guoyangyun.com/Problem/Qm.html - - querys.put("smsSignId", "2e65b1bb3d054466b82f0c9d125465e2"); - querys.put("templateId", "908e94ccf08b4476ba6c876d13f084ad"); - Map bodys = new HashMap(); - - - try { - /** - * 重要提示如下: - * HttpUtils请从\r\n\t \t* https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java\r\n\t \t* 下载 - * - * 相应的依赖请参照 - * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml - */ - HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys); - System.out.println(response.toString()); - //获取response的body - System.out.println(EntityUtils.toString(response.getEntity())); - } catch (Exception e) { - e.printStackTrace(); - } - } -} diff --git a/src/main/java/com/four/common/core/utils/TelSmsUtils.java b/src/main/java/com/four/common/core/utils/TelSmsUtils.java deleted file mode 100644 index 82ac204..0000000 --- a/src/main/java/com/four/common/core/utils/TelSmsUtils.java +++ /dev/null @@ -1,87 +0,0 @@ -package com.four.common.core.utils; - -import com.alibaba.fastjson.JSONObject; -import com.aliyun.dysmsapi20170525.Client; -import com.aliyun.dysmsapi20170525.models.SendSmsRequest; -import com.aliyun.dysmsapi20170525.models.SendSmsResponse; -import com.aliyun.teaopenapi.models.Config; -import lombok.extern.log4j.Log4j2; - -import java.util.Map; - -/** - * 短信工具类 - */ -@Log4j2 -public class TelSmsUtils { - - /** - * 阿里云主账号AccessKey,accessKeySecret拥有所有API的访问权限 - */ - private static String accessKeyId = "LTAIEVXszCmcd1T5"; - private static String accessKeySecret = "2zHwciQXln8wExSEnkIYtRTSwLeRNd"; - - /** - * 短信访问域名 - */ - private static String endpoint = "dysmsapi.aliyuncs.com"; - /** - * 短信签名 - */ - private static String signName = "登录验证"; - - /** - * 实例化短信对象 - */ - private static Client client; - - static { - log.info("初始化短信服务开始"); - long startTime = System.currentTimeMillis(); - try { - client = initClient(); - log.info("初始化短信成功:{}",signName); - } catch (Exception e) { - e.printStackTrace(); - } - log.info("初始化短信服务结束:耗时:{}MS",(System.currentTimeMillis()-startTime)); - } - /** - * 初始化短信对象 - * @return - * @throws Exception - */ - private static Client initClient() throws Exception { - Config config = new Config() - // 您的AccessKey ID - .setAccessKeyId(accessKeyId) - // 您的AccessKey Secret - .setAccessKeySecret(accessKeySecret); - // 访问的域名 - config.endpoint = endpoint; - return new Client(config); - } - - /** - * 发送单条短信 - * @param tel - * @param templateCode SMS_153991546 - * @param sendDataMap - */ - public static String sendSms(String tel , String templateCode , Map sendDataMap){ - SendSmsRequest sendSmsRequest = new SendSmsRequest() - .setPhoneNumbers(tel) - .setSignName(signName) - .setTemplateCode(templateCode) - .setTemplateParam(JSONObject.toJSONString(sendDataMap)); - SendSmsResponse sendSmsResponse = null; - try { - log.info("发送短信验证码:消息内容是:【{}】", JSONObject.toJSONString(sendDataMap)); - sendSmsResponse = client.sendSms(sendSmsRequest); - } catch (Exception e) { - log.error("短信发送异常,手机号:【{}】,短信内容:【{}】,异常信息:【{}】", tel, sendDataMap, e); - } - return JSONObject.toJSONString(sendSmsResponse.getBody()); - } - -}