This commit is contained in:
xiangwb
2017-11-22 15:19:16 +08:00
parent 035b39c87c
commit ffb5107315
16 changed files with 87 additions and 62 deletions
@@ -0,0 +1,23 @@
package com.xwbing.exception;
/**
* 项目名称: boot-module-demo
* 创建时间: 2017/11/22 15:08
* 作者: xiangwb
* 说明: 工具类异常
*/
public class UtilException extends RuntimeException {
private static final long serialVersionUID = 3338405359271762532L;
public UtilException(Throwable cause) {
super(cause);
}
public UtilException(String message) {
super(message);
}
public UtilException(String message, Throwable cause) {
super(message, cause);
}
}
@@ -1,5 +1,6 @@
package com.xwbing.util;
import com.xwbing.exception.UtilException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -70,7 +71,7 @@ public class DateUtil2 {
LocalDateTime localDateTime;
Instant instant;
if (dateStr.length() < 10) {
throw new RuntimeException("时间格式错误:" + dateStr);
throw new UtilException("时间格式错误:" + dateStr);
}
if (dateStr.length() == 10) {
dateStr += " 00:00:00";
@@ -1,5 +1,6 @@
package com.xwbing.util;
import com.xwbing.exception.UtilException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -77,7 +78,7 @@ public class DecimalUtil {
*/
public static Double div(Double v1, Double v2, int scale) {
if (scale < 0){
throw new RuntimeException("小数位数不能为负数");
throw new UtilException("小数位数不能为负数");
}
BigDecimal b1 = new BigDecimal(v1.toString());
BigDecimal b2 = new BigDecimal(v2.toString());
@@ -1,6 +1,7 @@
package com.xwbing.util;
import com.xwbing.exception.UtilException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -52,7 +53,7 @@ public class Digests {
return result;
} catch (GeneralSecurityException e) {
LOGGER.error(e.getMessage());
throw new RuntimeException("加密失败");
throw new UtilException("加密失败");
}
}
@@ -63,7 +64,7 @@ public class Digests {
*/
public static byte[] generateSalt(int numBytes) {
if (numBytes <= 0) {
throw new RuntimeException("numBytes argument must be a positive integer (1 or larger)");
throw new UtilException("numBytes argument must be a positive integer (1 or larger)");
}
byte[] bytes = new byte[numBytes];
random.nextBytes(bytes);
@@ -97,7 +98,7 @@ public class Digests {
return messageDigest.digest();
} catch (Exception e) {
LOGGER.error(e.getMessage());
throw new RuntimeException("加密失败");
throw new UtilException("加密失败");
}
}
}
@@ -2,6 +2,7 @@ package com.xwbing.util;
import com.xwbing.domain.entity.model.EmailModel;
import com.xwbing.exception.UtilException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -30,7 +31,7 @@ public class EmailUtil {
Properties props = new Properties();// 用于连接邮件服务器的参数配置(发送邮件时才需要用到)
String serverHost = emailModel.getServerHost();// 设置发送邮件的邮件服务器的属性(这里使用网易的smtp服务器)
if (StringUtils.isEmpty(serverHost)) {
throw new RuntimeException("发送邮件主机不能为空");
throw new UtilException("发送邮件主机不能为空");
}
props.setProperty("mail.transport.protocol", emailModel.getProtocol());// 使用的协议(JavaMail规范要求)
props.setProperty("mail.smtp.host", serverHost); // 发件人的邮箱的
@@ -47,12 +48,12 @@ public class EmailUtil {
// From: 发件人
String fromEmail = emailModel.getFromEmail();
if (StringUtils.isEmpty(fromEmail)) {
throw new RuntimeException("发送邮箱不能为空");
throw new UtilException("发送邮箱不能为空");
}
message.setFrom(new InternetAddress(emailModel.getFromEmail()));
// To: 收件人
if (StringUtils.isEmpty(emailModel.getToEmail())) {
throw new RuntimeException("接收邮箱不能为空");
throw new UtilException("接收邮箱不能为空");
}
InternetAddress[] toEmailList = InternetAddress.parse(emailModel.getToEmail());// toEmail格式为"aaa,bbb,..."或"aaa"
message.setRecipients(Message.RecipientType.TO, toEmailList);
@@ -66,7 +67,7 @@ public class EmailUtil {
*/
// Subject: 邮件主题
if (StringUtils.isEmpty(emailModel.getSubject())) {
throw new RuntimeException("邮件主题不能为空");
throw new UtilException("邮件主题不能为空");
}
message.setSubject(emailModel.getSubject());
// 向multipart对象中添加邮件的各个部分内容,包括文本内容和附件
@@ -83,7 +84,7 @@ public class EmailUtil {
Transport transport = session.getTransport("smtp");
// 5. 使用 邮箱账号 和 密码 连接邮件服务器
if (StringUtils.isEmpty(emailModel.getPassword())) {
throw new RuntimeException("发送邮箱密码不能为空");
throw new UtilException("发送邮箱密码不能为空");
}
transport.connect(serverHost, fromEmail, emailModel.getPassword());// 这里认证的邮箱必须与
// message中的发件人邮箱一致,否则报错
@@ -95,7 +96,7 @@ public class EmailUtil {
return true;
} catch (MessagingException e) {
LOGGER.error(e.getMessage());
throw new RuntimeException("发送邮件失败");
throw new UtilException("发送邮件失败");
}
}
@@ -1,5 +1,6 @@
package com.xwbing.util;
import com.xwbing.exception.UtilException;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.poi.hssf.usermodel.*;
@@ -208,7 +209,7 @@ public class ExcelUtil {
return resultList;
} catch (IOException e) {
LOGGER.error(e.getMessage());
throw new RuntimeException("excel文件生成类失败");
throw new UtilException("excel文件生成类失败");
}
}
@@ -1,6 +1,7 @@
package com.xwbing.util;
import com.alibaba.fastjson.JSONObject;
import com.xwbing.exception.UtilException;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.*;
import org.apache.http.client.HttpRequestRetryHandler;
@@ -130,7 +131,7 @@ public class HttpClientUtil {
post.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
} catch (UnsupportedEncodingException e) {
LOGGER.error(e.getMessage());
throw new RuntimeException("postByForm数据转换错误");
throw new UtilException("postByForm数据转换错误");
}
return getResult(post);
}
@@ -214,7 +215,7 @@ public class HttpClientUtil {
// result.setSuccess(false);
// result.setMsg(e.getMessage());
LOGGER.error(e.getMessage());
throw new RuntimeException("请求网络接口错误");
throw new UtilException("请求网络接口错误");
} finally {
poolingHttpClientConnectionManager.closeExpiredConnections();
poolingHttpClientConnectionManager.closeIdleConnections(120, TimeUnit.MILLISECONDS);
@@ -1,5 +1,6 @@
package com.xwbing.util;
import com.xwbing.exception.UtilException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -41,7 +42,7 @@ public class IpUtil {
inet = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
LOGGER.error(e.getMessage());
throw new RuntimeException("ip地址异常");
throw new UtilException("ip地址异常");
}
ip = inet.getHostAddress();
}
@@ -1,6 +1,7 @@
package com.xwbing.util;
import com.alibaba.fastjson.JSONObject;
import com.xwbing.exception.UtilException;
import org.apache.commons.beanutils.PropertyUtilsBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -55,7 +56,7 @@ public class JSONUtil {
}
} catch (Exception e) {
LOGGER.error(e.getMessage());
throw new RuntimeException("不序列化对象转换错误");
throw new UtilException("不序列化对象转换错误");
}
return new JSONObject(params);
}
@@ -1,5 +1,6 @@
package com.xwbing.util;
import com.xwbing.exception.UtilException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -72,7 +73,7 @@ public class KdniaoUtil {
}
} catch (Exception e) {
LOGGER.error(e.getMessage());
throw new RuntimeException("快递鸟接口请求失败");
throw new UtilException("快递鸟接口请求失败");
}
// 使用finally块来关闭输出流、输入流
finally {
@@ -100,7 +101,7 @@ public class KdniaoUtil {
return URLEncoder.encode(str, charset);
} catch (UnsupportedEncodingException e) {
LOGGER.error(e.getMessage());
throw new RuntimeException("urlEncoder失败");
throw new UtilException("urlEncoder失败");
}
}
@@ -121,7 +122,7 @@ public class KdniaoUtil {
return base64(MD5(content, charset), charset);
} catch (Exception e) {
LOGGER.error(e.getMessage());
throw new RuntimeException("快递鸟签名出错");
throw new UtilException("快递鸟签名出错");
}
}
@@ -154,7 +155,7 @@ public class KdniaoUtil {
return sb.toString().toLowerCase();
} catch (Exception e) {
LOGGER.error(e.getMessage());
throw new RuntimeException("快递鸟加密出错");
throw new UtilException("快递鸟加密出错");
}
}
@@ -169,7 +170,7 @@ public class KdniaoUtil {
return base64Encode(str.getBytes(charset));
} catch (UnsupportedEncodingException e) {
LOGGER.error(e.getMessage());
throw new RuntimeException("快递鸟编码出错");
throw new UtilException("快递鸟编码出错");
}
}
@@ -1,6 +1,7 @@
package com.xwbing.util;
import com.xwbing.exception.UtilException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -32,7 +33,7 @@ public class PingUtil {
return InetAddress.getByName(ipAddress).isReachable(timeOut);// 当返回值是true时,说明host是可用的,false则不可。
} catch (IOException e) {
LOGGER.error(e.getMessage());
throw new RuntimeException("无法连接该地址");
throw new UtilException("无法连接该地址");
}
}
@@ -52,7 +53,7 @@ public class PingUtil {
return true;
} catch (Exception ex) {
LOGGER.error(ex.getMessage());
throw new RuntimeException("无法连接该地址");
throw new UtilException("无法连接该地址");
}
}
@@ -85,7 +86,7 @@ public class PingUtil {
return connectedCount == pingTimes;
} catch (Exception ex) {
LOGGER.error(ex.getMessage());
throw new RuntimeException("无法连接该地址");
throw new UtilException("无法连接该地址");
}
}
@@ -6,6 +6,7 @@ import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.xwbing.exception.UtilException;
import org.apache.commons.io.output.ByteArrayOutputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -59,7 +60,7 @@ public class QRCodeUtils {
internal.output(output, internal.createCode(text));
} catch (Exception e) {
LOGGER.error(e.getMessage());
throw new RuntimeException("生成自定义尺寸不带logo的二维码失败");
throw new UtilException("生成自定义尺寸不带logo的二维码失败");
}
}
@@ -94,7 +95,7 @@ public class QRCodeUtils {
//internal.output(output, bitMatrix);
} catch (WriterException e) {
LOGGER.error(e.getMessage());
throw new RuntimeException("生成自定义尺寸带logo的二维码失败");
throw new UtilException("生成自定义尺寸带logo的二维码失败");
}
}
@@ -121,7 +122,7 @@ public class QRCodeUtils {
return result.getText();
} catch (Exception e) {
LOGGER.error(e.getMessage());
throw new RuntimeException("解析二维码图片失败");
throw new UtilException("解析二维码图片失败");
}
}
@@ -1,5 +1,6 @@
package com.xwbing.util;
import com.xwbing.exception.UtilException;
import org.bouncycastle.asn1.ASN1Sequence;
import org.bouncycastle.asn1.pkcs.RSAPrivateKeyStructure;
import org.slf4j.Logger;
@@ -54,7 +55,7 @@ public class RSAUtil {
return loadPublicKey(sb.toString());
} catch (IOException e) {
LOGGER.error(e.getMessage());
throw new RuntimeException("公钥数据流读取错误");
throw new UtilException("公钥数据流读取错误");
}
}
@@ -73,13 +74,13 @@ public class RSAUtil {
return (RSAPublicKey) keyFactory.generatePublic(keySpec);
} catch (NoSuchAlgorithmException e) {
LOGGER.error(e.getMessage());
throw new RuntimeException("无此算法");
throw new UtilException("无此算法");
} catch (InvalidKeySpecException e) {
LOGGER.error(e.getMessage());
throw new RuntimeException("公钥非法");
throw new UtilException("公钥非法");
} catch (IOException e) {
LOGGER.error(e.getMessage());
throw new RuntimeException("公钥数据内容读取错误");
throw new UtilException("公钥数据内容读取错误");
}
}
@@ -106,7 +107,7 @@ public class RSAUtil {
br.close();
return loadPrivateKey(sb.toString());
} catch (IOException e) {
throw new RuntimeException("私钥数据读取错误");
throw new UtilException("私钥数据读取错误");
}
}
@@ -132,13 +133,13 @@ public class RSAUtil {
return (RSAPrivateKey) keyFactory.generatePrivate(rsaPrivKeySpec);
} catch (NoSuchAlgorithmException e) {
LOGGER.error(e.getMessage());
throw new RuntimeException("无此算法");
throw new UtilException("无此算法");
} catch (InvalidKeySpecException e) {
LOGGER.error(e.getMessage());
throw new RuntimeException("私钥非法");
throw new UtilException("私钥非法");
} catch (IOException e) {
LOGGER.error(e.getMessage());
throw new RuntimeException("私钥数据内容读取错误");
throw new UtilException("私钥数据内容读取错误");
}
}
@@ -151,7 +152,7 @@ public class RSAUtil {
*/
public static String encrypt(RSAPublicKey publicKey, String data) {
if (publicKey == null) {
throw new RuntimeException("加密公钥为空,请设置");
throw new UtilException("加密公钥为空,请设置");
}
byte[] plainTextData = data.getBytes();
Cipher cipher;
@@ -163,16 +164,16 @@ public class RSAUtil {
return base64Encoder.encode(output);// Base64编码成字符串
} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
LOGGER.error(e.getMessage());
throw new RuntimeException("无此加密算法");
throw new UtilException("无此加密算法");
} catch (InvalidKeyException e) {
LOGGER.error(e.getMessage());
throw new RuntimeException("加密公钥非法,请检查");
throw new UtilException("加密公钥非法,请检查");
} catch (IllegalBlockSizeException e) {
LOGGER.error(e.getMessage());
throw new RuntimeException("明文长度非法");
throw new UtilException("明文长度非法");
} catch (BadPaddingException e) {
LOGGER.error(e.getMessage());
throw new RuntimeException("明文数据已损坏");
throw new UtilException("明文数据已损坏");
}
}
@@ -195,7 +196,7 @@ public class RSAUtil {
*/
public static String decrypt(RSAPrivateKey privateKey, String data) {
if (privateKey == null) {
throw new RuntimeException("解密私钥为空, 请设置");
throw new UtilException("解密私钥为空, 请设置");
}
BASE64Decoder base64Decoder = new BASE64Decoder();
Cipher cipher;
@@ -207,19 +208,19 @@ public class RSAUtil {
return new String(output);
} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
LOGGER.error(e.getMessage());
throw new RuntimeException("无此解密算法");
throw new UtilException("无此解密算法");
} catch (InvalidKeyException e) {
LOGGER.error(e.getMessage());
throw new RuntimeException("解密私钥非法,请检查");
throw new UtilException("解密私钥非法,请检查");
} catch (IllegalBlockSizeException e) {
LOGGER.error(e.getMessage());
throw new RuntimeException("密文长度非法");
throw new UtilException("密文长度非法");
} catch (BadPaddingException e) {
LOGGER.error(e.getMessage());
throw new RuntimeException("密文数据已损坏");
throw new UtilException("密文数据已损坏");
} catch (IOException e) {
LOGGER.error(e.getMessage());
throw new RuntimeException("解密数据读取失败");
throw new UtilException("解密数据读取失败");
}
}
@@ -1,5 +1,7 @@
package com.xwbing.util;
import com.xwbing.exception.UtilException;
import java.util.Random;
/**
@@ -13,7 +15,7 @@ public class RadomUtil {
public static String buildRandom(int length) {
if (length < 1)
throw new RuntimeException("参数异常!!!");
throw new UtilException("参数异常!!!");
Random random = new Random();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < length; i++) {
@@ -1,5 +1,6 @@
package com.xwbing.util;
import com.xwbing.exception.UtilException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -65,7 +66,7 @@ public class ZipUtil {
zipFile.delete();
} catch (Exception e) {
LOGGER.error(e.getMessage());
throw new RuntimeException("文件压缩错误");
throw new UtilException("文件压缩错误");
}
}
@@ -91,7 +92,7 @@ public class ZipUtil {
bos.close();
} catch (IOException e) {
LOGGER.error(e.getMessage());
throw new RuntimeException("文件转化错误");
throw new UtilException("文件转化错误");
}
return bytes;
}
@@ -91,19 +91,6 @@ public class GlobalExceptionHandler {
return JSONObjResult.toJSONObj(stringBuffer.toString());
}
/**
* 处理运行异常
* @param ex
* @return
*/
@ExceptionHandler(value = RuntimeException.class)
@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
@ResponseBody
public JSONObject handlerRuntimeException(RuntimeException ex) {
logger.error(ex.getMessage());
return JSONObjResult.toJSONObj(ex.getMessage());
}
/**
* 全部捕获
* @param ex