├── JetBrains_Open_Source
├── LICENSE
├── README.md
├── common-cache
├── pom.xml
├── pom.xml.versionsBackup
└── src
│ └── main
│ └── java
│ └── com
│ └── yz
│ └── common
│ └── cache
│ └── ICache.java
├── common-core
├── pom.xml
├── pom.xml.versionsBackup
└── src
│ └── main
│ └── java
│ └── com
│ └── yz
│ └── common
│ └── core
│ ├── configuration
│ └── ValidatorConfiguration.java
│ ├── dao
│ └── BaseDao.java
│ ├── enums
│ ├── CacheTypeEnum.java
│ ├── ErrorInfo.java
│ ├── JsonTypeEnum.java
│ └── PayEnum.java
│ ├── exception
│ └── HandlerException.java
│ ├── file
│ └── dowload
│ │ ├── FileDownload.java
│ │ ├── FileDownloadManager.java
│ │ └── FileDownloadProgress.java
│ ├── json
│ ├── FastJson.java
│ ├── IJsonInterface.java
│ ├── JSON.java
│ ├── Jackson.java
│ └── JacksonUtil.java
│ ├── message
│ └── ResponseMessage.java
│ ├── model
│ └── Page.java
│ ├── service
│ ├── BaseService.java
│ └── BaseServiceManager.java
│ └── utils
│ ├── AioOperation.java
│ ├── BeanUtil.java
│ ├── ClassLocationUtils.java
│ ├── ClassUtil.java
│ ├── CommandUtil.java
│ ├── DataCompressUtil.java
│ ├── DateUtils.java
│ ├── EncryptUtil.java
│ ├── FileUploadUtil.java
│ ├── FileUtils.java
│ ├── GraphicsMagick.java
│ ├── HttpUtil.java
│ ├── IDCardUtil.java
│ ├── IHttpClient.java
│ ├── IPUtil.java
│ ├── ImageUtil.java
│ ├── ImgCompress.java
│ ├── ListUtils.java
│ ├── MD5Util.java
│ ├── PlatformUtil.java
│ ├── PropertyUtil.java
│ ├── QRCodeUtil.java
│ ├── RandomUtil.java
│ ├── SHA256Util.java
│ ├── SerializeUtil.java
│ ├── SnowflakeID.java
│ ├── SnowflakeIdWorker.java
│ ├── SolrAPI.java
│ ├── StringUtils.java
│ ├── UUIDUtil.java
│ ├── VerifyCodeUtil.java
│ ├── WordUtil.java
│ ├── XMLUtil.java
│ └── ZipUtil.java
├── common-distributed-lock
├── pom.xml
├── pom.xml.versionsBackup
└── src
│ └── main
│ └── java
│ └── com
│ └── yz
│ └── common
│ └── distributed
│ └── lock
│ ├── DistributedLock.java
│ ├── MethodLockConfigBeanNameUtils.java
│ ├── annotation
│ ├── EnableMethodLock.java
│ └── MethodLock.java
│ ├── configuration
│ └── MethodLockConfiguration.java
│ └── interceptor
│ └── MethodLockInterceptor.java
├── common-elasticsearch
├── pom.xml
├── pom.xml.versionsBackup
└── src
│ └── main
│ └── java
│ └── com
│ └── yz
│ └── elasticsearch
│ ├── ESOpt.java
│ └── ESSearch.java
├── common-pay
├── pom.xml
├── pom.xml.versionsBackup
└── src
│ └── main
│ └── java
│ └── com
│ └── yz
│ └── common
│ ├── pay
│ ├── bean
│ │ ├── AliPayParams.java
│ │ ├── AliRefundParams.java
│ │ ├── AliRequestParams.java
│ │ ├── RequestParams.java
│ │ ├── WXCreateOrderParams.java
│ │ ├── WXPayParams.java
│ │ ├── WXRefundParams.java
│ │ └── WXRequestParams.java
│ ├── service
│ │ ├── AliPay.java
│ │ ├── IPay.java
│ │ ├── PayFactory.java
│ │ └── WXPay.java
│ └── util
│ │ ├── AlipayUtil.java
│ │ └── WXPayUtil.java
│ └── payment
│ ├── AliPayServiceImpl.java
│ ├── PayService.java
│ ├── ThirdPayFactory.java
│ ├── WXPayServiceImpl.java
│ ├── config
│ ├── AliPayConfig.java
│ ├── PayConfig.java
│ └── WXPayConfig.java
│ ├── constants
│ └── WXPayConstant.java
│ ├── enums
│ ├── PayChannelEnum.java
│ ├── PayTypeEnum.java
│ └── WXTradeState.java
│ ├── order
│ └── query
│ │ ├── OrderQueryResponse.java
│ │ └── WxOrderQueryResponse.java
│ ├── trade
│ └── pay
│ │ ├── AliAppTradePayImpl.java
│ │ ├── AliH5TradePayImpl.java
│ │ ├── TradePay.java
│ │ ├── WXJsApiTradePayImpl.java
│ │ ├── WXTradeAppPayImpl.java
│ │ ├── bo
│ │ ├── AliTradePayResponse.java
│ │ ├── TradePayResponse.java
│ │ ├── UserOrder.java
│ │ └── WXTradePayResponse.java
│ │ ├── builder
│ │ ├── AliTradePayBuilder.java
│ │ ├── AliTradePayParamsBuilder.java
│ │ ├── WXTradePayBuilder.java
│ │ └── WXTradePayParamsBuilder.java
│ │ └── params
│ │ ├── AliTradeAppPayParams.java
│ │ ├── AliTradeH5PayParams.java
│ │ ├── AliTradePayParams.java
│ │ ├── TradePayParams.java
│ │ ├── WXTradeAppPayParams.java
│ │ ├── WXTradeJsApiPayParams.java
│ │ └── WXTradePayParams.java
│ └── utils
│ ├── WXPayUtil.java
│ └── WXPayXmlUtil.java
├── common-security
├── pom.xml
├── pom.xml.versionsBackup
└── src
│ └── main
│ └── java
│ └── com
│ └── yz
│ └── common
│ └── security
│ ├── DES.java
│ ├── ISecurity.java
│ ├── RSA.java
│ ├── RSA256.java
│ ├── SHA.java
│ └── aes
│ ├── AES_CBC.java
│ └── AES_ECB.java
├── common-web
├── pom.xml
├── pom.xml.versionsBackup
└── src
│ └── main
│ └── java
│ └── com
│ └── yz
│ └── common
│ └── web
│ ├── IHttpServletRequestWrapper.java
│ ├── annotations
│ ├── HttpParam.java
│ └── ParamsValidate.java
│ ├── controller
│ └── ControllerAdviceHandler.java
│ ├── filter
│ ├── BaseFilter.java
│ └── DataSafeFilter.java
│ ├── http
│ └── params
│ │ └── validate
│ │ ├── HttpParamsValidateAdvisor.java
│ │ └── HttpRequestParamsValidateManager.java
│ ├── springmvc
│ └── interceptor
│ │ └── BaseInterceptor.java
│ └── utils
│ └── WebUtil.java
├── common
├── pom.xml
└── pom.xml.versionsBackup
├── pom.xml
└── pom.xml.versionsBackup
/JetBrains_Open_Source:
--------------------------------------------------------------------------------
1 | yangzhao_java@163.com
2 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # common-project
2 |
3 | Java后端常用工具类、缓存接口、消息队列接口、第三方支付接口封装;Restful接口参数验证,错误信息友好提示。
4 |
5 | [TOC]
6 |
7 | ## 1.分布式锁、分布式方法锁(common-distributed-lock):
8 |
9 | ### DistributedLock类(依赖RedisTemplate接口实现)
10 | ```
11 |
12 | /**
13 | *
14 | * @param key 锁标识
15 | * @param attempt 重试次数
16 | * @return
17 | */
18 | public boolean lock(String key,int attempt);
19 |
20 | /**
21 | *
22 | * @param key 锁标识
23 | * @param time 过期时间
24 | * @param timeUnit 过期时间单位
25 | * @param attemptNum 重试次数
26 | * @return
27 | */
28 | public boolean lock(String key, long time, TimeUnit timeUnit,int attemptNum)
29 | ```
30 |
31 | ### @EnableMethodLock @MethodLock 使用
32 | #### @EnableMethodLock
33 |
34 | **在Spring Boot 项目启动类添加该注解开启分布式方法锁**
35 |
36 | **普通Spring项目配置component-scan自动扫描com.yz.common.distributed.lock.configuration包即可**
37 |
38 | #### @MethodLock
39 |
40 | ```
41 | @Target({ElementType.METHOD})
42 | @Retention(RetentionPolicy.RUNTIME)
43 | @Documented
44 | public @interface MethodLock {
45 | /**
46 | * 锁标识
47 | * @return
48 | */
49 | String key() default "";
50 |
51 | /**
52 | * 时间
53 | * @return
54 | */
55 | long time() default 10;
56 |
57 | /**
58 | * 单位
59 | * @return
60 | */
61 | TimeUnit timeUnit() default TimeUnit.SECONDS;
62 |
63 | /**
64 | * 是否自动解锁
65 | * @return
66 | */
67 | boolean autoUnLock() default true;
68 |
69 | /**
70 | * 重试次数
71 | * @return
72 | */
73 | int attemptNum() default 0;
74 |
75 | }
76 | ```
77 |
78 | 在需要使用分布式方法锁的method上使用该注解即可
79 |
80 |
81 |
82 | ## 2.ES(common-elasticsearch)
83 |
84 | ① ElasticSearch 增删改操作
85 |
86 | ② ElasticSearch 高级查询(链式调用)
87 |
88 | ## 3.常用工具类(common-core)
89 |
90 | 网络类(HttpUtil)
91 |
92 | 时间类(DateUtils)
93 |
94 | JSON类(JsonUtil)
95 |
96 | 安全类(MD5加密、AES加密、Base64编码、3DES加密、RSA加密、SHA256)
97 |
98 | 图片处理(GraphicsMagick)
99 |
100 | ...
101 |
102 | ## 4.Redis(common-redis)
103 |
104 | ①redis常用API接口(RedisUtil)
105 |
106 | ②基于redis实现分布式锁(RedisLockUtil)
107 |
108 | ## 5.支付模块(common-pay)
109 |
110 | ①使用简单工厂模式实现预支付订单生成(位于com.yz.common.payment.trade.pay包下)
111 |
112 | 
113 |
114 | ②使用建造者模式对预支付订单参数进行动态构建(位于com.yz.common.payment.trade.pay.builder包下)
115 |
116 | 
117 |
118 | ③通过ThirdPayFactory工厂类获取PayService
119 |
120 | 
121 |
122 | ④发起预支付订单、退款、订单查询调用
123 |
124 | ## 6.common-web
125 |
126 | ①请求参数转换(非Form表单或get+参数方式):
127 |
128 | 自定义web拦截器(DataSafeFilter),加密json数据解析为Map保存到HttpServletRequest
129 | 中;自定义HttpServletRequest包装类(IHttpServletRequestWrapper),将解析的Map数
130 | 据保存到Parameter中
131 |
132 |
133 | ②基于hibernate-validator restful接口参数验证
134 |
--------------------------------------------------------------------------------
/common-cache/pom.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 | common-project
5 | com.yz
6 | 1.0.3
7 |
8 | 4.0.0
9 |
10 | common-cache
11 | jar
12 |
13 | common-cache
14 | http://maven.apache.org
15 |
16 |
17 |
18 | redis.clients
19 | jedis
20 |
21 |
22 | org.springframework.data
23 | spring-data-redis
24 |
25 |
26 |
27 |
28 | ${artifactId}
29 |
30 |
31 |
--------------------------------------------------------------------------------
/common-cache/pom.xml.versionsBackup:
--------------------------------------------------------------------------------
1 |
3 |
4 | common-project
5 | com.yz
6 | 0.0.1-SNAPSHOT
7 |
8 | 4.0.0
9 |
10 | common-cache
11 | jar
12 |
13 | common-cache
14 | http://maven.apache.org
15 |
16 |
17 |
18 |
19 | com.yz
20 | common-core
21 | 0.0.1-SNAPSHOT
22 |
23 |
24 |
25 | org.springframework.data
26 | spring-data-redis
27 | 2.0.5.RELEASE
28 |
29 |
30 |
31 |
32 |
33 | ${artifactId}
34 |
35 |
36 |
--------------------------------------------------------------------------------
/common-core/pom.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 | common-project
5 | com.yz
6 | 1.0.3
7 |
8 | 4.0.0
9 |
10 | common-core
11 | jar
12 |
13 | common-core
14 | http://maven.apache.org
15 |
16 |
17 |
18 | org.apache.httpcomponents
19 | httpclient
20 |
21 |
22 |
23 | org.apache.httpcomponents
24 | httpmime
25 |
26 |
27 |
28 |
29 | org.apache.solr
30 | solr-solrj
31 |
32 |
33 |
34 |
35 | org.apache.poi
36 | poi
37 |
38 |
39 |
40 |
41 | org.apache.poi
42 | poi-ooxml
43 |
44 |
45 |
46 |
47 | org.apache.poi
48 | poi-scratchpad
49 |
50 |
51 |
52 |
53 | org.apache.poi
54 | poi-ooxml-schemas
55 |
56 |
57 |
58 | com.fasterxml.jackson.core
59 | jackson-core
60 |
61 |
62 | com.fasterxml.jackson.core
63 | jackson-databind
64 |
65 |
66 | com.fasterxml.jackson.core
67 | jackson-annotations
68 |
69 |
70 |
71 |
72 | com.google.zxing
73 | core
74 |
75 |
76 |
77 | org.hibernate.validator
78 | hibernate-validator
79 |
80 |
81 |
82 | javax.validation
83 | validation-api
84 |
85 |
86 |
87 | org.javassist
88 | javassist
89 |
90 |
91 | org.dom4j
92 | dom4j
93 |
94 |
95 |
96 |
97 | ${artifactId}
98 |
99 |
100 |
--------------------------------------------------------------------------------
/common-core/pom.xml.versionsBackup:
--------------------------------------------------------------------------------
1 |
3 |
4 | common-project
5 | com.yz
6 | 1.0.1
7 |
8 | 4.0.0
9 |
10 | common-core
11 | jar
12 |
13 | common-core
14 | http://maven.apache.org
15 |
16 |
17 |
18 | org.apache.httpcomponents
19 | httpclient
20 |
21 |
22 |
23 | org.apache.httpcomponents
24 | httpmime
25 |
26 |
27 |
28 |
29 | org.apache.solr
30 | solr-solrj
31 |
32 |
33 |
34 |
35 | org.apache.poi
36 | poi
37 |
38 |
39 |
40 |
41 | org.apache.poi
42 | poi-ooxml
43 |
44 |
45 |
46 |
47 | org.apache.poi
48 | poi-scratchpad
49 |
50 |
51 |
52 |
53 | org.apache.poi
54 | poi-ooxml-schemas
55 |
56 |
57 |
58 | com.fasterxml.jackson.core
59 | jackson-core
60 |
61 |
62 | com.fasterxml.jackson.core
63 | jackson-databind
64 |
65 |
66 |
67 |
68 | com.google.zxing
69 | core
70 |
71 |
72 |
73 | org.hibernate.validator
74 | hibernate-validator
75 |
76 |
77 | org.javassist
78 | javassist
79 |
80 |
81 | dom4j
82 | dom4j
83 |
84 |
85 |
86 |
87 | ${artifactId}
88 |
89 |
90 |
--------------------------------------------------------------------------------
/common-core/src/main/java/com/yz/common/core/configuration/ValidatorConfiguration.java:
--------------------------------------------------------------------------------
1 | package com.yz.common.core.configuration;
2 |
3 | import org.hibernate.validator.HibernateValidator;
4 |
5 | import javax.validation.Validation;
6 | import javax.validation.Validator;
7 | import javax.validation.ValidatorFactory;
8 |
9 | /**
10 | * @author yangzhao
11 | * @Description
12 | * @Date create by 21:25 18/2/25
13 | */
14 | public class ValidatorConfiguration {
15 |
16 | public static Validator validator(){
17 | ValidatorFactory validatorFactory = Validation.byProvider( HibernateValidator.class )
18 | .configure()
19 | .addProperty( "hibernate.validator.fail_fast", "true" )
20 | .buildValidatorFactory();
21 | Validator validator = validatorFactory.getValidator();
22 |
23 | return validator;
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/common-core/src/main/java/com/yz/common/core/dao/BaseDao.java:
--------------------------------------------------------------------------------
1 | package com.yz.common.core.dao;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 |
6 | import java.util.List;
7 |
8 | /**
9 | * @author yangzhao
10 | * @Description
11 | * @Date create by 11:32 18/1/2
12 | */
13 | public interface BaseDao {
14 |
15 | public final Logger logger = LoggerFactory.getLogger(BaseDao.class);
16 |
17 | boolean deleteByPrimaryKey(Long id);
18 |
19 | boolean insert(E e);
20 |
21 | boolean insertSelective(E e);
22 |
23 | E selectByPrimaryKey(Long id);
24 |
25 | boolean updateByPrimaryKeySelective(E e);
26 |
27 | boolean updateByPrimaryKey(E e);
28 |
29 | boolean insertList(List eList);
30 |
31 | public boolean deleteByIdList(Long[] ids);
32 |
33 | List select(E e);
34 |
35 | List selectByIdList(List idList);
36 |
37 | }
38 |
--------------------------------------------------------------------------------
/common-core/src/main/java/com/yz/common/core/enums/CacheTypeEnum.java:
--------------------------------------------------------------------------------
1 | package com.yz.common.core.enums;
2 |
3 | /**
4 | * Created by yangzhao on 18/1/17.
5 | */
6 | public enum CacheTypeEnum {
7 |
8 | JVM(1),REDIS(2);
9 |
10 | private int type;
11 |
12 | private CacheTypeEnum(int type){
13 | this.type = type;
14 | }
15 |
16 | public int getType(){
17 | return this.type;
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/common-core/src/main/java/com/yz/common/core/enums/ErrorInfo.java:
--------------------------------------------------------------------------------
1 | package com.yz.common.core.enums;
2 |
3 | /**
4 | * @author: yangzhao
5 | * @Date: 2020-01-05 13:57
6 | * @Description:
7 | */
8 | public interface ErrorInfo {
9 |
10 | public int getErrorCode();
11 |
12 | public String getErrorMsg();
13 | }
14 |
--------------------------------------------------------------------------------
/common-core/src/main/java/com/yz/common/core/enums/JsonTypeEnum.java:
--------------------------------------------------------------------------------
1 | package com.yz.common.core.enums;
2 |
3 | /**
4 | * Created by yangzhao on 18/1/17.
5 | */
6 | public enum JsonTypeEnum {
7 |
8 | FASTJSON(1),JACKSON(2);
9 |
10 | private int type;
11 |
12 | private JsonTypeEnum(int type){
13 | this.type = type;
14 | }
15 |
16 | public int getType() {
17 | return type;
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/common-core/src/main/java/com/yz/common/core/enums/PayEnum.java:
--------------------------------------------------------------------------------
1 | package com.yz.common.core.enums;
2 |
3 | /**
4 | * Created by yangzhao on 18/1/17.
5 | */
6 | public enum PayEnum {
7 |
8 | WEIXIN(1),ALI(2);
9 |
10 | private int type;
11 |
12 | private PayEnum(int type){
13 | this.type = type;
14 | }
15 |
16 | public int getType() {
17 | return type;
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/common-core/src/main/java/com/yz/common/core/exception/HandlerException.java:
--------------------------------------------------------------------------------
1 | package com.yz.common.core.exception;
2 |
3 | import com.yz.common.core.enums.ErrorInfo;
4 |
5 | /**
6 | * Created by yangzhao on 17/3/29.
7 | */
8 | public class HandlerException extends RuntimeException {
9 |
10 | private int code;
11 |
12 | public HandlerException(int code, String message) {
13 | super(String.valueOf(message));
14 | this.code = code;
15 | }
16 |
17 | public HandlerException(ErrorInfo errorInfo) {
18 | super(String.valueOf(errorInfo.getErrorMsg()));
19 | this.code = errorInfo.getErrorCode();
20 | }
21 |
22 | public HandlerException(String message) {
23 | super(String.valueOf(message));
24 | }
25 |
26 | public String getErrorInfo() {
27 | return super.getMessage();
28 | }
29 |
30 | public int getCode() {
31 | return code;
32 | }
33 |
34 | public void setCode(int code) {
35 | this.code = code;
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/common-core/src/main/java/com/yz/common/core/file/dowload/FileDownload.java:
--------------------------------------------------------------------------------
1 | package com.yz.common.core.file.dowload;
2 |
3 | import org.apache.http.HttpEntity;
4 | import org.apache.http.HttpResponse;
5 | import org.apache.http.client.HttpClient;
6 | import org.apache.http.client.methods.HttpGet;
7 | import org.apache.http.impl.client.HttpClients;
8 |
9 | import java.io.*;
10 | import java.util.Date;
11 | import java.util.Timer;
12 | import java.util.TimerTask;
13 | import java.util.concurrent.CountDownLatch;
14 |
15 | /**
16 | * @author: yangzhao
17 | * @Date: 2019/7/28 22:34
18 | * @Description:
19 | */
20 | public class FileDownload implements Runnable {
21 |
22 | public static final CountDownLatch countDownLatch =new CountDownLatch(1);
23 |
24 | private long fileSize;
25 |
26 | private String fileUrl;
27 |
28 | private String fileSavePath;
29 | /**
30 | * 已下载文件大小
31 | */
32 | private long downloadFileSize=0;
33 |
34 | private FileDownloadProgress fileDownloadProgress;
35 |
36 | public FileDownload(long fileSize, String fileUrl, String fileSavePath){
37 | this.fileSize=fileSize;
38 | this.fileUrl=fileUrl;
39 | this.fileSavePath=fileSavePath;
40 | }
41 |
42 | public FileDownload(long fileSize, String fileUrl, String fileSavePath, FileDownloadProgress fileDownloadProgress){
43 | this.fileSize=fileSize;
44 | this.fileUrl=fileUrl;
45 | this.fileSavePath=fileSavePath;
46 | this.fileDownloadProgress=fileDownloadProgress;
47 | }
48 |
49 | @Override
50 | public void run() {
51 | HttpClient httpClient = HttpClients.createDefault();
52 | OutputStream out = null;
53 | InputStream in = null;
54 | Timer timer = new Timer();
55 | try {
56 | HttpGet httpGet = new HttpGet(this.fileUrl);
57 | HttpResponse httpResponse = httpClient.execute(httpGet);
58 | HttpEntity entity = httpResponse.getEntity();
59 | in = entity.getContent();
60 | long length = entity.getContentLength();
61 | if (length <= 0) {
62 | System.out.println("下载文件不存在!");
63 | return;
64 | }
65 |
66 | File file = new File(this.fileSavePath);
67 | if(!file.exists()){
68 | file.createNewFile();
69 | }
70 | out = new FileOutputStream(file);
71 | byte[] buffer = new byte[4096];
72 | int readLength = 0;
73 | timer.scheduleAtFixedRate(new TimerTask() {
74 | public void run() {
75 | if (fileDownloadProgress!=null){
76 | //回调进度信息
77 | fileDownloadProgress.progressCallBack(fileSize,downloadFileSize);
78 | }
79 | }
80 | }, new Date(),2000);
81 | while ((readLength=in.read(buffer)) > 0) {
82 | byte[] bytes = new byte[readLength];
83 | System.arraycopy(buffer, 0, bytes, 0, readLength);
84 | downloadFileSize+=bytes.length;
85 | out.write(bytes);
86 | }
87 | out.flush();
88 | if (fileDownloadProgress!=null){
89 | //回调进度信息
90 | fileDownloadProgress.finish(fileSavePath);
91 | }
92 | } catch (Exception e) {
93 | e.printStackTrace();
94 | }finally{
95 | try {
96 | if(in != null){
97 | in.close();
98 | }
99 | } catch (IOException e) {
100 | e.printStackTrace();
101 | }
102 |
103 | try {
104 | if(out != null){
105 | out.close();
106 | }
107 | } catch (IOException e) {
108 | e.printStackTrace();
109 | }
110 | if (countDownLatch!=null){
111 | countDownLatch.countDown();
112 | }
113 | timer.cancel();
114 | }
115 | }
116 | }
117 |
--------------------------------------------------------------------------------
/common-core/src/main/java/com/yz/common/core/file/dowload/FileDownloadManager.java:
--------------------------------------------------------------------------------
1 | package com.yz.common.core.file.dowload;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 |
6 | import java.io.IOException;
7 | import java.net.HttpURLConnection;
8 | import java.net.URL;
9 |
10 | /**
11 | * @author: yangzhao
12 | * @Date: 2019/7/28 22:27
13 | * @Description:
14 | */
15 | public class FileDownloadManager {
16 |
17 | private static final Logger logger = LoggerFactory.getLogger(FileDownloadManager.class);
18 |
19 | /**
20 | * 获取远程文件大小
21 | */
22 | public static long getRemoteFileSize(String remoteFileUrl) throws IOException {
23 | long fileSize = 0;
24 | HttpURLConnection httpConnection = (HttpURLConnection) new URL(remoteFileUrl).openConnection();
25 | httpConnection.setRequestMethod("HEAD");
26 | int responseCode = httpConnection.getResponseCode();
27 | if (responseCode >= 400) {
28 | logger.debug("Web服务器响应错误!");
29 | return 0;
30 | }
31 | String sHeader;
32 | for (int i = 1; ; i++) {
33 | sHeader = httpConnection.getHeaderFieldKey(i);
34 | if (sHeader != null && sHeader.equals("Content-Length")) {
35 | logger.info("文件大小ContentLength:"+ httpConnection.getContentLength());
36 | fileSize = Long.parseLong(httpConnection.getHeaderField(sHeader));
37 | break;
38 | }
39 |
40 | }
41 | return fileSize;
42 | }
43 |
44 | /**
45 | * 同步下载文件
46 | * @param url
47 | * @param savePath
48 | * @throws Exception
49 | */
50 | public static void syncDownload(String url,String savePath) throws Exception {
51 | long remoteFileSize = getRemoteFileSize(url);
52 | FileDownload fileDownload = new FileDownload(remoteFileSize, url, savePath);
53 | Thread thread=new Thread(fileDownload);
54 | thread.start();
55 | FileDownload.countDownLatch.await();
56 | }
57 |
58 | /**
59 | * 同步下载文件(回调)
60 | * @param url
61 | * @param savePath
62 | * @param fileDownloadProgress
63 | * @throws Exception
64 | */
65 | public static void syncDownload(String url,String savePath,FileDownloadProgress fileDownloadProgress) throws Exception {
66 | long remoteFileSize = getRemoteFileSize(url);
67 | FileDownload fileDownload = new FileDownload(remoteFileSize, url, savePath,fileDownloadProgress);
68 | Thread thread=new Thread(fileDownload);
69 | thread.start();
70 | FileDownload.countDownLatch.await();
71 | }
72 |
73 | /**
74 | * 异步下载文件
75 | * @param url
76 | * @param savePath
77 | * @throws Exception
78 | */
79 | public static void asyncDownload(String url,String savePath) throws Exception {
80 | long remoteFileSize = getRemoteFileSize(url);
81 | FileDownload fileDownload = new FileDownload(remoteFileSize, url, savePath);
82 | Thread thread=new Thread(fileDownload);
83 | thread.start();
84 | }
85 |
86 | /**
87 | * 异步下载文件(回调)
88 | * @param url
89 | * @param savePath
90 | * @param fileDownloadProgress
91 | * @throws Exception
92 | */
93 | public static void asyncDownload(String url,String savePath,FileDownloadProgress fileDownloadProgress) throws Exception {
94 | long remoteFileSize = getRemoteFileSize(url);
95 | FileDownload fileDownload = new FileDownload(remoteFileSize, url, savePath,fileDownloadProgress);
96 | Thread thread=new Thread(fileDownload);
97 | thread.start();
98 | }
99 |
100 | }
101 |
--------------------------------------------------------------------------------
/common-core/src/main/java/com/yz/common/core/file/dowload/FileDownloadProgress.java:
--------------------------------------------------------------------------------
1 | package com.yz.common.core.file.dowload;
2 |
3 | /**
4 | * @author: yangzhao
5 | * @Date: 2019/7/28 22:42
6 | * @Description:
7 | */
8 | public interface FileDownloadProgress {
9 | /**
10 | * 进度回调
11 | * @param fileSize
12 | * @param currentFileSize
13 | */
14 | public void progressCallBack(long fileSize,long currentFileSize);
15 |
16 | /**
17 | * 完成下载
18 | * @param filePath
19 | */
20 | public void finish(String filePath);
21 | }
22 |
--------------------------------------------------------------------------------
/common-core/src/main/java/com/yz/common/core/json/FastJson.java:
--------------------------------------------------------------------------------
1 | package com.yz.common.core.json;
2 |
3 | import com.alibaba.fastjson.JSON;
4 |
5 | import java.util.List;
6 |
7 | /**
8 | * 阿里巴巴fastjson
9 | *
10 | * @auther yangzhao
11 | * create by 17/10/9
12 | */
13 | public class FastJson implements IJsonInterface {
14 | @Override
15 | public T parseObject(String json, Class t) {
16 | T t1 = JSON.parseObject(json, t);
17 | return t1;
18 | }
19 |
20 | @Override
21 | public List parseList(String json, Class t) {
22 | List tList = JSON.parseArray(json, t);
23 | return tList;
24 | }
25 |
26 | @Override
27 | public String toJsonString(Object obj) {
28 | return JSON.toJSONString(obj);
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/common-core/src/main/java/com/yz/common/core/json/IJsonInterface.java:
--------------------------------------------------------------------------------
1 | package com.yz.common.core.json;
2 |
3 | import java.util.List;
4 |
5 | /**
6 | * json接口
7 | *
8 | * @auther yangzhao
9 | * create by 17/10/9
10 | */
11 | public interface IJsonInterface {
12 |
13 | public T parseObject(String json,Class t);
14 |
15 | public List parseList(String json, Class t);
16 |
17 | public String toJsonString(Object obj);
18 |
19 | }
20 |
--------------------------------------------------------------------------------
/common-core/src/main/java/com/yz/common/core/json/JSON.java:
--------------------------------------------------------------------------------
1 | package com.yz.common.core.json;
2 |
3 |
4 | /**
5 | * json装饰类
6 | *
7 | * @auther yangzhao
8 | * create by 17/10/9
9 | */
10 | public final class JSON {
11 |
12 | private static IJsonInterface jsonInterface;
13 |
14 | public static void init(int type){
15 | switch (type){
16 | case 1:
17 | jsonInterface = new FastJson();
18 | break;
19 | case 2:
20 | jsonInterface = new Jackson();
21 | break;
22 | default:
23 | jsonInterface=new FastJson();
24 | }
25 | }
26 |
27 | public static IJsonInterface getInterface(){
28 | if (jsonInterface==null){
29 | throw new RuntimeException("jsonInterface接口==null,请预先实例化该工具类");
30 | }
31 | return jsonInterface;
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/common-core/src/main/java/com/yz/common/core/json/Jackson.java:
--------------------------------------------------------------------------------
1 | package com.yz.common.core.json;
2 |
3 | import com.fasterxml.jackson.annotation.JsonInclude;
4 | import com.fasterxml.jackson.core.JsonGenerator;
5 | import com.fasterxml.jackson.core.JsonParser;
6 | import com.fasterxml.jackson.core.JsonProcessingException;
7 | import com.fasterxml.jackson.databind.*;
8 | import org.slf4j.Logger;
9 | import org.slf4j.LoggerFactory;
10 |
11 | import java.io.IOException;
12 | import java.util.ArrayList;
13 | import java.util.Collection;
14 | import java.util.List;
15 |
16 | /**
17 | * jackson
18 | *
19 | * @auther yangzhao
20 | * create by 17/10/9
21 | */
22 | public class Jackson implements IJsonInterface {
23 |
24 | private static final Logger logger= LoggerFactory.getLogger(Jackson.class);
25 |
26 | @Override
27 | public T parseObject(String json, Class t) {
28 | try {
29 | return objectMapper.readValue(json, t);
30 | } catch (Exception e) {
31 | logger.error("JSON字符串转对象失败 ----", e);
32 | }
33 | return null;
34 | }
35 |
36 | @Override
37 | public List parseList(String json, Class t) {
38 | try {
39 | Collection collection = this.parse(json, ArrayList.class, t);
40 | return (List) collection;
41 | } catch (IOException e) {
42 | e.printStackTrace();
43 | }
44 | return null;
45 | }
46 |
47 | @Override
48 | public String toJsonString(Object obj) {
49 | try {
50 | return objectMapper.writeValueAsString(obj);
51 | } catch (Exception e) {
52 | logger.error(e.getMessage(), e);
53 | }
54 | return null;
55 | }
56 |
57 | private ObjectMapper objectMapper = new ObjectMapper();
58 |
59 | public Jackson() {
60 | //不允许出现特殊字符和转义符
61 | objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true) ;
62 | //支持单引号
63 | objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
64 | // 设置输出时包含属性的风格
65 | objectMapper.configure(MapperFeature.USE_ANNOTATIONS, false);
66 |
67 | objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true);
68 | //jackson 实体转json为NULL或者为空不参加序列化
69 | objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
70 |
71 | //jackson实体转json为null时修改null值为""
72 | objectMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer