diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..261eeb9 --- /dev/null +++ b/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/pom.xml b/pom.xml deleted file mode 100644 index 458b04b..0000000 --- a/pom.xml +++ /dev/null @@ -1,65 +0,0 @@ - - - 4.0.0 - - org.springframework.boot - spring-boot-starter-parent - 2.2.6.RELEASE - - - cn.sric - javaUtil - 1.0-SNAPSHOT - - - 1.8 - - - - - - org.springframework.boot - spring-boot-starter - - - - - - org.springframework.boot - spring-boot-starter-data-redis - - - - - - com.squareup.okhttp3 - okhttp - 4.6.0 - - - - - org.apache.httpcomponents - httpclient - 4.5.2 - - - - - com.baomidou - mybatis-plus-boot-starter - 3.3.0 - - - - - org.projectlombok - lombok - - - - - - \ No newline at end of file diff --git a/src/main/java/cn/sric/config/MybatisPlusConfig.java b/src/main/java/cn/sric/config/MybatisPlusConfig.java deleted file mode 100644 index b22348b..0000000 --- a/src/main/java/cn/sric/config/MybatisPlusConfig.java +++ /dev/null @@ -1,25 +0,0 @@ -package cn.sric.config; - -import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.transaction.annotation.EnableTransactionManagement; - -/** - * 功能说明:mybatis-plus注册配置 - * 功能描述:分页插件 - * 如果分页不管用的话,添加这个设置就可以了 - * - * @author sric - */ -@Configuration -@EnableTransactionManagement -public class MybatisPlusConfig { - /** - * 启动分页插件 - */ - @Bean - public PaginationInterceptor paginationInterceptor() { - return new PaginationInterceptor(); - } -} \ No newline at end of file diff --git a/src/main/java/cn/sric/config/SpringBeanConfig.java b/src/main/java/cn/sric/config/SpringBeanConfig.java deleted file mode 100644 index 330ef2e..0000000 --- a/src/main/java/cn/sric/config/SpringBeanConfig.java +++ /dev/null @@ -1,67 +0,0 @@ -package cn.sric.config; - -import org.springframework.beans.BeansException; -import org.springframework.context.ApplicationContext; -import org.springframework.context.ApplicationContextAware; -import org.springframework.stereotype.Component; - -/** - * @author sunchuanchuan - * @version V1.0 - * @date 2020/11/27 - * @package cn.sric.common - * @description - **/ -@Component -public class SpringBeanConfig implements ApplicationContextAware { - private static ApplicationContext applicationContext = null; - - - @Override - public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { - if (SpringBeanConfig.applicationContext == null) { - SpringBeanConfig.applicationContext = applicationContext; - } - } - - - public static ApplicationContext getApplicationContext() { - return applicationContext; - } - - /** - * 根据名字获取bean - * - * @param name bean的名字 - * @return - */ - public static Object getBean(String name) { - return applicationContext.getBean(name); - - } - - /** - * 根据bean的类型来注入bean - * - * @param clazz 类型 - * @param 类型 - * @return - */ - public static T getBean(Class clazz) { - return applicationContext.getBean(clazz); - } - - /** - * 根据名字和类型来获取bean - * - * @param name bean name - * @param clazz 类型 - * @param 类型 - * @return - */ - public static T getBean(String name, Class clazz) { - return applicationContext.getBean(name, clazz); - } - - -} diff --git a/src/main/java/cn/sric/util/Base64Util.java b/src/main/java/cn/sric/util/Base64Util.java deleted file mode 100644 index 463a132..0000000 --- a/src/main/java/cn/sric/util/Base64Util.java +++ /dev/null @@ -1,117 +0,0 @@ -package cn.sric.util; - -import org.springframework.util.StringUtils; -import sun.misc.BASE64Decoder; -import sun.misc.BASE64Encoder; - -import java.io.*; - - -/** - * base64编码解析 - * - * @author sric - */ -public class Base64Util { - private static final String separator = "/"; - - /** - * 获取base64字符串 - * - * @param fileName 路径 - * @param isSafe 是否为安全的 - * @return - */ - public static String encodeBase64(String fileName, boolean isSafe) { - if (StringUtils.isEmpty(fileName)) { - throw new NullPointerException(); - } - InputStream in = null; - byte[] data = null; - String encodedText = null; - //读取图片字节数组 - try { - in = new BufferedInputStream(new FileInputStream(fileName)); - data = new byte[in.available()]; - in.read(data); - in.close(); - } catch (IOException e) { - e.printStackTrace(); - } - //对字节数组Base64编码 - if (isSafe) { - java.util.Base64.Encoder encoder = java.util.Base64.getUrlEncoder(); - encodedText = encoder.encodeToString(data); - } else { - BASE64Encoder encoder = new BASE64Encoder(); - encodedText = encoder.encode(data); - encodedText = encodedText.replaceAll("[\\s*\t\n\r]", ""); - } - return encodedText; - } - - /** - * 解析base64 - * - * @param base64 base64编码 - * @param filePath 文件路径 - * @param suffix 文件后缀 - * @param isSafe 是否是安全的文件 - * @return 返回文件最后生成的路径 - */ - public static String decodeBase64(String base64, String filePath, String suffix, boolean isSafe) { - if (StringUtils.isEmpty(base64) || StringUtils.isEmpty(filePath) || StringUtils.isEmpty(suffix)) { - throw new NullPointerException(); - } - OutputStream out = null; - String fileName = null; - try { - byte[] b; - if (isSafe) { - java.util.Base64.Decoder decoder = java.util.Base64.getUrlDecoder(); - b = decoder.decode(base64); - } else { - BASE64Decoder decoder = new BASE64Decoder(); - b = decoder.decodeBuffer(base64.substring(base64.indexOf(",") + 1)); - } - for (int i = 0; i < b.length; ++i) { - // 调整异常数据 - if (b[i] < 0) { - b[i] += 256; - } - } - File file = new File(filePath); - if (!file.exists()) { - file.mkdirs(); - } - fileName = filePath + System.currentTimeMillis() + "." + suffix; - out = new BufferedOutputStream(new FileOutputStream(fileName)); - out.write(b); - out.flush(); - } catch (Exception e) { - - } finally { - if (out != null) { - try { - out.close(); - } catch (IOException e) { - e.printStackTrace(); - } - - } - } - return fileName; - } - - - public static void main(String[] args) { - //base64 编码 - String base64 = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAALQAAAC0CAYAAAA9zQ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qeKIyVTxR+U0Vk8o3Hda6yGGtixzWusgP/+MqJpWp4onKVPGGylQxVUwqTyomlScqU8VUMalMFZPKGxX/ksNaFzmsdZHDWhf54f8zKlPFN6lMFVPFN1VMKr+p4psqvumw1kUOa13ksNZFfvhlFX9TxaQyVTxRmSqeVEwqk8pU8URlqnijYlL5TSpTxaTypOKbDmtd5LDWRQ5rXeSHL1P5m1Q+oTJVPFGZKp5UTCpvqEwVk8pUMVW8UTGpTCpTxSdUpopPHNa6yGGtixzWuoj9wVqXOKx1kcNaFzmsdZHDWhc5rHWRw1oXOax1kcNaFzmsdZHDWhc5rHWRw1oXOax1kcNaFzmsdZH/B4/ruH6ug685AAAAAElFTkSuQmCC"; - //储存的文件路径 - String filePath = "C:/Users/sric/Desktop/image/"; - //文件后缀 - String suffix = "JPG"; - String s = decodeBase64(base64, filePath, suffix, false); - System.out.println(s); - } -} \ No newline at end of file diff --git a/src/main/java/cn/sric/util/DivisionTxtUtil.java b/src/main/java/cn/sric/util/DivisionTxtUtil.java deleted file mode 100644 index 8cc8e5d..0000000 --- a/src/main/java/cn/sric/util/DivisionTxtUtil.java +++ /dev/null @@ -1,63 +0,0 @@ -package cn.sric.util; - - -import org.springframework.util.StringUtils; - -import java.io.*; - -/** - * @author sunchuanchuan - * @version V1.0 - * @date 2021/1/19 - * @package cn.sric.activemq.controller - * @description - **/ -public class DivisionTxtUtil { - - - public static void main(String[] args) { - divisionTxt("E:/usr", "a1", 500000); - } - - /** - * 拆分文本文件 按照行数来拆分 - * - * @param path 文件路径 - * @param fileName 文件名 不需要挂.txt - * @param line 多少行拆分一个文件 - * @return rownum 行总数, fileNo拆分了几个文件 - */ - public static String divisionTxt(String path, String fileName, Integer line) { - if (StringUtils.isEmpty(line) || line == 0) { - return "请填写正确的行数"; - } - - - try { - FileReader read = new FileReader(path + "/" + fileName + ".txt"); - BufferedReader br = new BufferedReader(read); - String row; - - int rownum = 1; - - int fileNo = 1; - FileWriter fw = new FileWriter(path + "/" + fileName + fileNo + ".txt"); - while ((row = br.readLine()) != null) { - rownum++; - fw.append(row + "\r\n"); - - if ((rownum / line) > (fileNo - 1)) { - fw.close(); - fileNo++; - fw = new FileWriter(path + "/" + fileName + fileNo + ".txt"); - } - } - fw.close(); - System.out.println("rownum=" + rownum + ";fileNo=" + fileNo); - return "rownum=" + rownum + ";fileNo=" + fileNo; - } catch (IOException e) { - e.printStackTrace(); - } - return "有点出错了"; - } -} diff --git a/src/main/java/cn/sric/util/ListUtil.java b/src/main/java/cn/sric/util/ListUtil.java deleted file mode 100644 index 87a9b5f..0000000 --- a/src/main/java/cn/sric/util/ListUtil.java +++ /dev/null @@ -1,50 +0,0 @@ -package cn.sric.util; - -import java.util.ArrayList; -import java.util.List; - -/** - * @author sric - * @version V1.0 - * @date 2021/1/20 - * @package cn.sric.util - * @description List的一些帮助类 - **/ -public class ListUtil { - - /** - * 封装一下List集合的分页 - * - * @param list 本来的数据 - * @param current 当前页 - * @param size 页大小 - * @param 元素类型 - * @return 分页后的数据 - */ - public static List pageList(List list, int current, int size) { - if (current < 1) { - current = 1; - } - if (size < 1) { - size = 10; - } - - //从第几条开始查询 - int skip = (current - 1) * size; - //从哪个索引开始查询 - int fromIndex = skip > list.size() ? list.size() / size * size : skip; - - //结束的索引位置 判断剩余的数量是否大于页大小,如果不大于就查询到最后一条数据 - int toIndex = (list.size() - skip) > size ? skip + size : list.size(); - list = list.subList(fromIndex, toIndex); - return list; - } - - public static void main(String[] args) { - List list = new ArrayList<>(); - for (int i = 0; i < 18; i++) { - list.add(i); - } - pageList(list, 2, 9).forEach(System.out::println); - } -} diff --git a/src/main/java/cn/sric/util/OkHttp.java b/src/main/java/cn/sric/util/OkHttp.java deleted file mode 100644 index 727512b..0000000 --- a/src/main/java/cn/sric/util/OkHttp.java +++ /dev/null @@ -1,124 +0,0 @@ -package cn.sric.util; - -import okhttp3.*; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.concurrent.TimeUnit; - -/** - * 导入okHttp依赖 - * - * - * com.squareup.okhttp3 - * okhttp - * 4.6.0 - * - * @author sric - */ -public class OkHttp { - static OkHttpClient client = new OkHttpClient().newBuilder().connectTimeout(60000, TimeUnit.MILLISECONDS) - .readTimeout(60000, TimeUnit.MILLISECONDS).build(); - - /** - * get 请求 - * - * @param url url - * @return 返回请求内容 - */ - public static String get(String url) { - Request request = new Request.Builder() - .url(url).addHeader("application/json;", "charset=utf-8") - .build(); - - try { - Response response = client.newCall(request).execute(); - return response.body().string(); - } catch (IOException e) { - e.printStackTrace(); - } - return null; - } - - /** - * post 请求 - * - * @param url url - * @param requestBody 请求体 - * @return 返回请求结果 - */ - private static String post(String url, RequestBody requestBody) { - Request request = new Request.Builder() - .url(url).post(requestBody) - .build(); - - try (Response response = client.newCall(request).execute()) { - return response.body().string(); - } catch (IOException e) { - e.printStackTrace(); - } - return null; - } - - - /** - * 由于上面设置请求体有点麻烦,所以我重载了一下post方法 - * - * @param url 请求路径 - * @param map 请求内容放入map中就可以 - * @return 返回请求内容 - */ - public static String post(String url, Map map) { - List name = new ArrayList<>(); - List value = new ArrayList<>(); - map.keySet().forEach(key -> { - name.add(key); - value.add(map.get(key).toString()); - }); - FormBody formBody = new FormBody(name, value); - return post(url, formBody); - } - -// /** -// * @param url 请求地址 -// * @param json 上报内容 -// * @return 返回值 -// */ -// public String okHttpPostMethod(String url, String json) { -// MediaType JSON = MediaType.parse("application/json; charset=utf-8"); -// OkHttpClient client = new OkHttpClient(); -// RequestBody body = RequestBody.create(JSON, json); -// -// Request request = new Request.Builder() -// .url(url) -// .build(); -// try (Response response = client.newCall(request).execute()) { -// return response.body().string(); -// } catch (IOException e) { -// e.printStackTrace(); -// } -// return null; -// } -// -// public static String get(String url, JSONObject jsonObject) { -// MediaType JSON = MediaType.parse("application/json; charset=utf-8"); -// //1 . 拿到OkHttpClient对象 -// OkHttpClient client = new OkHttpClient(); -// //创建一个RequestBody(参数1:数据类型 参数2传递的json串) -// RequestBody requestBody = RequestBody.create(JSON, String.valueOf(jsonObject)); -// //3 . 构建Request,将FormBody作为Post方法的参数传入 -// Request request = new Request.Builder() -// .url(url).post(requestBody) -// .build(); -// -// try { -// Response response = client.newCall(request).execute(); -// return response.body().string(); -// } catch (IOException e) { -// e.printStackTrace(); -// } -// return ""; -// } -} \ No newline at end of file diff --git a/src/main/java/cn/sric/util/PictureFileUtil.java b/src/main/java/cn/sric/util/PictureFileUtil.java deleted file mode 100644 index 03b2f20..0000000 --- a/src/main/java/cn/sric/util/PictureFileUtil.java +++ /dev/null @@ -1,95 +0,0 @@ -package cn.sric.util; - - -import lombok.extern.slf4j.Slf4j; -import okhttp3.OkHttpClient; -import okhttp3.Request; -import okhttp3.Response; - -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.util.Objects; - -/** - * @author sric - *

- * 由于某些文件使用RestTemplate会出现异常,所以这里使用了OkHttp - * 首先导入okhttp的依赖 - * - * - * com.squareup.okhttp3 - * okhttp - * 4.6.0 - * - * @version V1.0 - * @date 2020/11/18 - * @package cn.sric.util - * @description - **/ -@Slf4j -public class PictureFileUtil { - - - /** - * 下载网络图片 - * - * @param url 文件URL - * @param localUrl 下载到本地的路径 (加文件名 - * @return 返回下载的路径 - */ - public static String download(String url, String localUrl) { - InputStream inputStream = null; - FileOutputStream out = null; - try { - //将网络URL转换为流 - inputStream = sendGetForFile(url); - if (inputStream != null) { - out = new FileOutputStream(localUrl); - int j; - while ((j = inputStream.read()) != -1) { - out.write(j); - } - out.flush(); - return localUrl; - } - } catch (Exception e) { - return ""; - } finally { - try { - if (inputStream != null) { - inputStream.close(); - } - if (out != null) { - out.close(); - } - } catch (IOException e) { - e.printStackTrace(); - } - } - return ""; - } - - /** - * 网络文件 由URL转换为流的形式 - * - * @param url - * @return - */ - public static InputStream sendGetForFile(String url) { - InputStream inputStream; - Request req = (new Request.Builder()).url(url).get().build(); - Response response; - try { - response = new OkHttpClient().newCall(req).execute(); - if (!response.isSuccessful()) { - log.error("【调用HTTP请求异常】 code:{},message:{},url:{}", response.code(), response.message(), url); - return null; - } - inputStream = Objects.requireNonNull(response.body()).byteStream(); - return inputStream; - } catch (IOException e) { - return null; - } - } -} diff --git a/src/main/java/cn/sric/util/RedisUtil.java b/src/main/java/cn/sric/util/RedisUtil.java deleted file mode 100644 index 96fa698..0000000 --- a/src/main/java/cn/sric/util/RedisUtil.java +++ /dev/null @@ -1,147 +0,0 @@ -package cn.sric.util; - -import org.springframework.data.redis.connection.RedisConnection; -import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; -import org.springframework.data.redis.core.StringRedisTemplate; -import org.springframework.stereotype.Component; -import org.springframework.transaction.annotation.Transactional; - -import javax.annotation.Resource; -import java.util.Calendar; -import java.util.Set; -import java.util.concurrent.TimeUnit; - -/** - * 使用前加入 redis 依赖 - * - * - * org.springframework.boot - * spring-boot-starter-data-redis - * - * - * @author sric - */ -@Component(value = "redisUtil") -public class RedisUtil { - - @Resource - private StringRedisTemplate stringRedisTemplate; - - public boolean set(String key, String value) { - boolean flag = false; - try { - stringRedisTemplate.opsForValue().set(key, value); - flag = true; - } catch (Exception e) { - flag = false; - } - return flag; - } - - - public boolean set(String key, String value, long timeout) { - boolean flag = false; - try { - stringRedisTemplate.opsForValue().set(key, value, timeout, TimeUnit.SECONDS); - flag = true; - } catch (Exception e) { - flag = false; - } - return flag; - } - - - public boolean set(String key, String value, long timeout, TimeUnit timeUnit) { - boolean flag = false; - try { - stringRedisTemplate.opsForValue().set(key, value, timeout, timeUnit); - flag = true; - } catch (Exception e) { - flag = false; - } - return flag; - } - - public String get(String key) { - return stringRedisTemplate.opsForValue().get(key); - } - - - public boolean setNX(String key, String value) { - RedisConnection redisConnection = stringRedisTemplate.getConnectionFactory().getConnection(); - return redisConnection.setNX(key.getBytes(), new byte[]{1}); - } - - - /** - * 设置过期时间是凌晨0点0分0秒 - * 如果存在不进行设置 - * - * @param key - * @param value - */ - @Transactional(rollbackFor = Exception.class) - public boolean putTime(String key, String value) { - //获取剩余时间 - long seconds = (RedisUtil.getMiol() - System.currentTimeMillis()) / 1000; -// boolean b = setNX(key, value); - -// if (b) { -// try { -// if (expire(key, seconds, TimeUnit.SECONDS)) { -// return true; -// } -// } catch (Exception e) { -// del(key); -// e.printStackTrace(); -// } -// } - return set(key, value, seconds, TimeUnit.SECONDS); - } - - - public boolean del(String key) { - return stringRedisTemplate.delete(key); - } - - public boolean expire(String key, long time, TimeUnit timeUnit) { - return stringRedisTemplate.expire(key, time, timeUnit); - } - - public Set keys(String keyPatterns) { - return stringRedisTemplate.keys(keyPatterns); - } - - - public boolean exists(String key) { - RedisConnection redisConnection = stringRedisTemplate.getConnectionFactory().getConnection(); - return redisConnection.exists(key.getBytes()); - } - - public void setDatabase(int index) { - LettuceConnectionFactory connectionFactory = (LettuceConnectionFactory) stringRedisTemplate.getConnectionFactory(); - connectionFactory.setDatabase(index); - } - - - public static Long getMiol() { - Calendar ca = Calendar.getInstance(); - //失效的时间 - ca.set(Calendar.HOUR_OF_DAY, 24); - ca.set(Calendar.MINUTE, 0); - ca.set(Calendar.SECOND, 0); - long fl = ca.getTimeInMillis(); - return fl; - } - - public static void main(String[] args) { - Calendar ca = Calendar.getInstance(); - //失效的时间 - ca.set(Calendar.HOUR_OF_DAY, 24); - ca.set(Calendar.MINUTE, 0); - ca.set(Calendar.SECOND, 0); - long fl = ca.getTimeInMillis(); - System.out.println(fl / 1000); - } - -} \ No newline at end of file diff --git a/src/main/java/cn/sric/util/ThreadPoolExecutorUtil.java b/src/main/java/cn/sric/util/ThreadPoolExecutorUtil.java deleted file mode 100644 index fc1288d..0000000 --- a/src/main/java/cn/sric/util/ThreadPoolExecutorUtil.java +++ /dev/null @@ -1,152 +0,0 @@ -package cn.sric.util; - - -import java.util.concurrent.*; -import java.util.concurrent.locks.Condition; -import java.util.concurrent.locks.ReentrantLock; - -/** - * 创建线程池的帮助类 - * 可以使线程池状态为暂停,继续运行 - * - * @author sunchuanchuan - * @version V1.0 - * @date 2020/12/22 - * @package cn.sric.config - * @description - **/ -public class ThreadPoolExecutorUtil extends ThreadPoolExecutor { - - - /** - * @param corePoolSize 核心线程池数量 不会被回收 - * @param maximumPoolSize 最大线程数量 当核心线程到达最大值,并且队列中也到最大值,就会创建新的线程 - * @param keepAliveTime 设置线程最多保持多长时间默认 0 超过这个时间会回收不是核心的线程 也就是 maximumPoolSize - corePoolSize - * @param unit keepAliveTime 时间类型 - * @param workQueue 创建队列的方式 (当新的任务来临后并且核心线程满了之后会创建队列来进行排队等待 - * @param handler 拒绝策略 当队列达到最大值并且 最大线程池( maximumPoolSize)也是最大值,这时新来的任务没有地方储存,则会使用拒绝策略 - */ - public ThreadPoolExecutorUtil(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue workQueue, RejectedExecutionHandler handler) { - super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, handler); - } - - - /** - * @param threadFactory 创建线程的方式 - * 这个构造方法对比上面的多了一个 threadFactory 参数 - * 表示线程的创建工厂,此参数一般用的比较少,我们通常在创建线程池时不指定此参数,它会使用默认的线程创建工厂的方法来创建线程, - *

- * 其余参数看上面的构造方法注释 - */ - - public ThreadPoolExecutorUtil(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue workQueue, - ThreadFactory threadFactory, RejectedExecutionHandler handler) { - super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler); - } - - - boolean isPause = false; - ReentrantLock lock = new ReentrantLock(); - Condition condition = lock.newCondition(); - - - @Override - protected void beforeExecute(Thread t, Runnable r) { - super.beforeExecute(t, r); - lock.lock(); - try { - while (isPause) { - long ms = 10L; - System.out.printf("pausing, %s\n", t.getName()); - Thread.sleep(ms); - condition.await(); - } - } catch (InterruptedException e) { - e.printStackTrace(); - } finally { - lock.unlock(); - } - } - - @Override - protected void afterExecute(Runnable r, Throwable t) { - super.afterExecute(r, t); - } - - /** - * 暂停 - */ - public void pause() { - lock.lock(); - try { - System.out.println("exe pause"); - isPause = true; - } catch (Exception e) { - e.printStackTrace(); - } finally { - lock.unlock(); - } - } - - /** - * 继续执行 - */ - public void resume() { - lock.lock(); - try { - System.out.println("un pause"); - isPause = false; - condition.signalAll(); - } catch (Exception e) { - e.printStackTrace(); - } finally { - lock.unlock(); - } - } - - public static void main(String[] args) throws InterruptedException { - /** - * new LinkedBlockingQueue<>(100) 队列 括号填写参数为队列的最大值,不填写参数为无界队列 - * new ThreadPoolExecutor.CallerRunsPolicy() 拒绝策略,如果添加到线程池失败,那么主线程会自己去执行该任务,不会等待线程池中的线程去执行。 - */ - ThreadPoolExecutorUtil threadPoolExecutorUtil = new ThreadPoolExecutorUtil(5, 10, - 0, TimeUnit.SECONDS, new LinkedBlockingQueue<>(100), new ThreadPoolExecutor.CallerRunsPolicy()); - - for (int i = 1; i <= 150; i++) { - - System.out.println("i:" + i + "---->>>当前活动的线程数量:" + threadPoolExecutorUtil.getActiveCount() + "队列中的数量为:" + threadPoolExecutorUtil.getQueue().size() + - "-----》》》》最大线程数" + threadPoolExecutorUtil.getPoolSize()); - - int finalI = i; - threadPoolExecutorUtil.execute(() -> { - try { - System.err.println(Thread.currentThread().getName() + "---->>>i:" + finalI); - TimeUnit.SECONDS.sleep(1); - } catch (InterruptedException e) { - e.printStackTrace(); - } - }); - - - if (i == 100) { - System.out.println("100当前活动的线程数量:" + threadPoolExecutorUtil.getActiveCount() + "队列中的数量为:" + threadPoolExecutorUtil.getQueue().size() + - "最大线程数" + threadPoolExecutorUtil.getPoolSize()); - } else if (i == 110) { - System.out.println("110当前活动的线程数量:" + threadPoolExecutorUtil.getActiveCount() + "队列中的数量为:" + threadPoolExecutorUtil.getQueue().size() + - "最大线程数" + threadPoolExecutorUtil.getPoolSize()); - } else if (i == 120) { - System.out.println("120当前活动的线程数量:" + threadPoolExecutorUtil.getActiveCount() + "队列中的数量为:" + threadPoolExecutorUtil.getQueue().size() + - "最大线程数" + threadPoolExecutorUtil.getPoolSize()); - } else if (i == 130) { - System.out.println("120当前活动的线程数量:" + threadPoolExecutorUtil.getActiveCount() + "队列中的数量为:" + threadPoolExecutorUtil.getQueue().size() + - "最大线程数" + threadPoolExecutorUtil.getPoolSize()); - } else if (i == 150) { - System.out.println("150当前活动的线程数量:" + threadPoolExecutorUtil.getActiveCount() + "队列中的数量为:" + threadPoolExecutorUtil.getQueue().size() + - "最大线程数" + threadPoolExecutorUtil.getPoolSize()); - } - } - TimeUnit.SECONDS.sleep(5); - System.out.println("当前活动的线程数量:" + threadPoolExecutorUtil.getActiveCount() + "队列中的数量为:" + threadPoolExecutorUtil.getQueue().size() + - "最大线程数" + threadPoolExecutorUtil.getPoolSize()); - } -} diff --git a/src/main/java/cn/sric/util/TimeCountUtil.java b/src/main/java/cn/sric/util/TimeCountUtil.java deleted file mode 100644 index ef4765d..0000000 --- a/src/main/java/cn/sric/util/TimeCountUtil.java +++ /dev/null @@ -1,102 +0,0 @@ -package cn.sric.util; - - -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.Date; - -public class TimeCountUtil { - public static final String DEFAULT_PATTERN = "yyyy-MM-dd HH:mm:ss"; - - - private static final long ONE_MINUTE = 60000L; - private static final long ONE_HOUR = 3600000L; - private static final long ONE_DAY = 86400000L; - private static final long ONE_WEEK = 604800000L; - - private static final String ONE_SECOND_AGO = "秒前"; - private static final String ONE_MINUTE_AGO = "分钟前"; - private static final String ONE_HOUR_AGO = "小时前"; - private static final String ONE_DAY_AGO = "天前"; - private static final String ONE_MONTH_AGO = "月前"; - private static final String ONE_YEAR_AGO = "年前"; - - // 实际调用方法 - public static String timeCount(Date dateTime) { - return format(dateTime); - } - - // 实际调用方法 - public static String timeCount(String dateTime) { - SimpleDateFormat format = new SimpleDateFormat(DEFAULT_PATTERN); - try { - return format(format.parse(dateTime)); - } catch (ParseException e) { - return null; - } - } - - // 时间转换 - public static String format(Date date) { - Date currDate = new Date(); - long delta = currDate.getTime() - date.getTime(); - if (delta < 1L * ONE_MINUTE) { - long seconds = toSeconds(delta); - return (seconds <= 0 ? 1 : seconds) + ONE_SECOND_AGO; - } - if (delta < 45L * ONE_MINUTE) { - long minutes = toMinutes(delta); - return (minutes <= 0 ? 1 : minutes) + ONE_MINUTE_AGO; - } - - if (delta < 24L * ONE_HOUR) { - long hours = toHours(delta); - return (hours <= 0 ? 1 : hours) + ONE_HOUR_AGO; - } -// String currDateStr = DateUtil.parseDateToStr(currDate, DateUtil.DATE_TIME_FORMAT_YYYY年MM月DD日); -// delta = DateUtil.parseStrToDate(currDateStr,DateUtil.DATE_TIME_FORMAT_YYYY年MM月DD日).getTime() - date.getTime(); -// if (delta < 24L * ONE_HOUR) { -// return "昨天"; -// } - if (delta < 30L * ONE_DAY) { - long days = toDays(delta); - return (days <= 0 ? 1 : days) + ONE_DAY_AGO; - } - if (delta < 12L * 4L * ONE_WEEK) { - long months = toMonths(delta); - return (months <= 0 ? 1 : months) + ONE_MONTH_AGO; - } else { - long years = toYears(delta); - return (years <= 0 ? 1 : years) + ONE_YEAR_AGO; - } - } - - private static long toSeconds(long date) { - return date / 1000L; - } - - private static long toMinutes(long date) { - return toSeconds(date) / 60L; - } - - private static long toHours(long date) { - return toMinutes(date) / 60L; - } - - private static long toDays(long date) { - return toHours(date) / 24L; - } - - private static long toMonths(long date) { - return toDays(date) / 30L; - } - - private static long toYears(long date) { - return toMonths(date) / 365L; - } - - - public static void main(String[] args) { - System.out.println(timeCount(new Date())); - } -}