This commit is contained in:
xiangwbs
2017-11-21 22:35:51 +08:00
parent 2495cb5f65
commit f4023eef3c
8 changed files with 47 additions and 67 deletions
@@ -1,9 +1,9 @@
package com.xwbing.util;
import com.xwbing.exception.BusinessException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.math.BigDecimal;
import java.text.ParseException;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
@@ -27,6 +27,7 @@ public class DateUtil2 {
public static final String YYYY = "yyyy";
public static final String HH_MM_SS = "HH:mm:ss";
public static final String HH_MM = "HH:mm";
private static final Logger logger= LoggerFactory.getLogger(DateUtil2.class);
/*
* ChronoUnit:各种时间单位 | TemporalAdjusters:时态对象 可以获取第一天,最后一天等
* 获取时间分量:Duration要求是localDateTime/localTime类型 | Period要求是localDate类型
@@ -69,7 +70,8 @@ public class DateUtil2 {
LocalDateTime localDateTime;
Instant instant;
if (dateStr.length() < 10) {
throw new BusinessException("时间格式错误:" + dateStr);
logger.error("时间格式错误:" + dateStr);
throw new RuntimeException("时间格式错误:" + dateStr);
}
if (dateStr.length() == 10) {
dateStr += " 00:00:00";
@@ -275,7 +277,6 @@ public class DateUtil2 {
* @param startMoth YYYY-MM
* @param endMonth YYYY-MM
* @return
* @throws ParseException
*/
public static List<String> listYearMonth(String startMoth, String endMonth) {
LocalDate start = LocalDate.parse(startMoth + "-01");
@@ -299,7 +300,6 @@ public class DateUtil2 {
* @param startDate
* @param endDate
* @return
* @throws Exception
*/
public static List<String> listDate(String startDate, String endDate) {
List<String> dateList = new ArrayList<>();
@@ -1,5 +1,8 @@
package com.xwbing.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.math.BigDecimal;
/**
@@ -10,6 +13,7 @@ import java.math.BigDecimal;
*/
public class DecimalUtil {
private static final int DEF_DIV_SCALE = 10;
private static final Logger LOGGER = LoggerFactory.getLogger(DecimalUtil.class);
/**
* 两个Double数相加
@@ -72,8 +76,10 @@ public class DecimalUtil {
* @return Double
*/
public static Double div(Double v1, Double v2, int scale) {
if (scale < 0)
if (scale < 0){
LOGGER.error("小数位数不能为负数");
throw new IllegalArgumentException("小数位数不能为负数");
}
BigDecimal b1 = new BigDecimal(v1.toString());
BigDecimal b2 = new BigDecimal(v2.toString());
return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
@@ -98,7 +104,7 @@ public class DecimalUtil {
* @param d
* @return
*/
public static double decimalND(Double d,int scale) {
public static double decimalND(Double d, int scale) {
BigDecimal bg = new BigDecimal(d);
return bg.setScale(scale, BigDecimal.ROUND_DOWN).doubleValue();
}
@@ -118,11 +124,11 @@ public class DecimalUtil {
public static void main(String[] args) {
Double div = div((double) 9, (double) 8, 2);
System.out.println(div);
div=formate(div,1);
div = formate(div, 1);
System.out.println(div);
Double formate = formate(6.2, 1);
System.out.println(formate);
Double d=1.11;
Double d = 1.11;
int i = d.intValue();
System.out.println(i);
}
@@ -1,7 +1,9 @@
package com.xwbing.util;
import java.io.IOException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.InputStream;
import java.security.GeneralSecurityException;
import java.security.MessageDigest;
@@ -13,11 +15,10 @@ import java.security.SecureRandom;
* 作者: xiangwb
*/
public class Digests {
private static final String SHA1 = "SHA-1";
private static final String MD5 = "MD5";
private static SecureRandom random = new SecureRandom();
private static final Logger LOGGER = LoggerFactory.getLogger(Digests.class);
/**
* 对输入字符串进行sha1散列.
@@ -50,7 +51,8 @@ public class Digests {
}
return result;
} catch (GeneralSecurityException e) {
throw new RuntimeException(e);
LOGGER.error(e.getMessage());
throw new RuntimeException("加密失败");
}
}
@@ -71,18 +73,18 @@ public class Digests {
/**
* 对文件进行md5散列.
*/
public static byte[] md5(InputStream input) throws IOException {
public static byte[] md5(InputStream input) {
return digest(input, MD5);
}
/**
* 对文件进行sha1散列.
*/
public static byte[] sha1(InputStream input) throws IOException {
public static byte[] sha1(InputStream input) {
return digest(input, SHA1);
}
private static byte[] digest(InputStream input, String algorithm) throws IOException {
private static byte[] digest(InputStream input, String algorithm) {
try {
MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
int bufferLength = 8 * 1024;
@@ -93,7 +95,8 @@ public class Digests {
read = input.read(buffer, 0, bufferLength);
}
return messageDigest.digest();
} catch (GeneralSecurityException e) {
} catch (Exception e) {
LOGGER.error(e.getMessage());
throw new RuntimeException(e);
}
}
@@ -2,7 +2,6 @@ package com.xwbing.util;
import com.xwbing.domain.entity.model.EmailModel;
import com.xwbing.exception.BusinessException;
import org.apache.commons.lang3.StringUtils;
import javax.mail.*;
@@ -29,7 +28,7 @@ public class EmailUtil {
String serverHost = emailModel.getServerHost();// 设置发送邮件的邮件服务器的属性(这里使用网易的smtp服务器)
if (StringUtils.isEmpty(serverHost)) {
result = false;
throw new BusinessException("发送邮件主机不能为空");
throw new RuntimeException("发送邮件主机不能为空");
}
props.setProperty("mail.transport.protocol",
emailModel.getProtocol());// 使用的协议(JavaMail规范要求)
@@ -48,13 +47,13 @@ public class EmailUtil {
String fromEmail = emailModel.getFromEmail();
if (StringUtils.isEmpty(fromEmail)) {
result = false;
throw new BusinessException("发送邮箱不能为空");
throw new RuntimeException("发送邮箱不能为空");
}
message.setFrom(new InternetAddress(emailModel.getFromEmail()));
// To: 收件人
if (StringUtils.isEmpty(emailModel.getToEmail())) {
result = false;
throw new BusinessException("接收邮箱不能为空");
throw new RuntimeException("接收邮箱不能为空");
}
InternetAddress[] toEmailList = InternetAddress.parse(emailModel
.getToEmail());// toEmail格式为"aaa,bbb,..."或"aaa"
@@ -70,7 +69,7 @@ public class EmailUtil {
// Subject: 邮件主题
if (StringUtils.isEmpty(emailModel.getSubject())) {
result = false;
throw new BusinessException("邮件主题不能为空");
throw new RuntimeException("邮件主题不能为空");
}
message.setSubject(emailModel.getSubject());
// 向multipart对象中添加邮件的各个部分内容,包括文本内容和附件
@@ -88,7 +87,7 @@ public class EmailUtil {
// 5. 使用 邮箱账号 和 密码 连接邮件服务器
if (StringUtils.isEmpty(emailModel.getPassword())) {
result = false;
throw new BusinessException("发送邮箱密码不能为空");
throw new RuntimeException("发送邮箱密码不能为空");
}
transport.connect(serverHost, fromEmail, emailModel.getPassword());// 这里认证的邮箱必须与
// message中的发件人邮箱一致,否则报错
@@ -1,7 +1,6 @@
package com.xwbing.util;
import com.alibaba.fastjson.JSONObject;
import com.xwbing.exception.BusinessException;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.*;
import org.apache.http.client.HttpRequestRetryHandler;
@@ -131,7 +130,7 @@ public class HttpClientUtil {
post.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
} catch (UnsupportedEncodingException e) {
logger.error(e.getMessage());
throw new BusinessException("postByForm数据转换错误");
throw new RuntimeException("postByForm数据转换错误");
}
return getResult(post);
}
@@ -215,7 +214,7 @@ public class HttpClientUtil {
// result.setSuccess(false);
// result.setMsg(e.getMessage());
logger.error(e.getMessage());
throw new BusinessException("请求网络接口错误");
throw new RuntimeException("请求网络接口错误");
} finally {
poolingHttpClientConnectionManager.closeExpiredConnections();
poolingHttpClientConnectionManager.closeIdleConnections(120, TimeUnit.MILLISECONDS);
@@ -1,6 +1,7 @@
package com.xwbing.util;
import com.xwbing.exception.BusinessException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
@@ -15,6 +16,8 @@ import java.util.zip.ZipOutputStream;
* 说明:
*/
public class ZipUtil {
private static final Logger LOGGER = LoggerFactory.getLogger(ZipUtil.class);
/**
* @param response 响应
* @param files 所有文件
@@ -61,7 +64,8 @@ public class ZipUtil {
out.close();
zipFile.delete();
} catch (Exception e) {
throw new BusinessException("文件压缩错误");
LOGGER.error(e.getMessage());
throw new RuntimeException("文件压缩错误");
}
}
@@ -86,7 +90,8 @@ public class ZipUtil {
fis.close();
bos.close();
} catch (IOException e) {
throw new BusinessException("文件转化错误");
LOGGER.error(e.getMessage());
throw new RuntimeException("文件转化错误");
}
return bytes;
}
@@ -1,36 +0,0 @@
package com.xwbing.util.captcha;
/**
* 说明:创建验证码异常类
* 创建日期: 2016年8月29日 上午11:02:42
* 作者: xiangwb
*/
public class CaptchaException extends RuntimeException {
private static final long serialVersionUID = 1L;
public CaptchaException() {
super();
}
public CaptchaException(String message, Throwable cause) {
super(message, cause);
}
public CaptchaException(String message) {
super(message);
}
public CaptchaException(Throwable cause) {
super(cause);
}
}
@@ -2,6 +2,8 @@ package com.xwbing.util.captcha;
import com.xwbing.constant.CommonConstant;
import com.xwbing.util.CommonDataUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServlet;
@@ -17,7 +19,8 @@ import java.io.OutputStream;
* 作者: xiangwbd
*/
public class CaptchaServlet extends HttpServlet {
private static final long serialVersionUID = -124247581620199710L;
private static final long serialVersionUID = -8687266469702749102L;
private final Logger logger = LoggerFactory.getLogger(CaptchaServlet.class);
@Override
protected void service(HttpServletRequest req, HttpServletResponse res) {
@@ -39,7 +42,8 @@ public class CaptchaServlet extends HttpServlet {
OutputStream out = res.getOutputStream();
ImageIO.write(image, "JPEG", out);
} catch (IOException e) {
e.printStackTrace();
logger.error(e.getMessage());
throw new RuntimeException("获取验证码错误");
}
}
}