JDBCUtils      获得连接池和获得连接

package cn.utils;import java.sql.Connection;
import java.sql.SQLException;import javax.sql.DataSource;import com.mchange.v2.c3p0.ComboPooledDataSource;/*** 工具类 提供数据库连接池 和数据库连接* * @author seawind* */
public class JDBCUtils {private static DataSource dataSource = new ComboPooledDataSource();public static DataSource getDataSource() {return dataSource;}/*** 当DBUtils需要手动控制事务时,调用该方法获得一个连接* * @return* @throws SQLException*/public static Connection getConnection() throws SQLException {return dataSource.getConnection();}
}

开启新线程发送激活邮件代码(用于实现Runnable接口的线程类调用的run方法)

package cn.utils;import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;import cn.domain.User;public class MailThread implements Runnable {private User user;public MailThread(User user) {this.user = user;}@Overridepublic void run() {// 1、 sessonProperties properties = new Properties();properties.put("mail.transport.protocol", "smtp");properties.put("mail.smtp.host", "localhost");Session session = Session.getDefaultInstance(properties);// 2、messageMimeMessage message = new MimeMessage(session);try {message.setFrom(new InternetAddress("aaa@estore.com"));message.setRecipient(RecipientType.TO, new InternetAddress(user.getEmail()));message.setSubject("estore商城注册确认邮件");DateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 hh时mm分ss秒");message.setContent("<h4>欢迎使用estore商城系统,您的用户名是"+ user.getUsername()+ ",您的密码是"+ user.getPassword()+ ",请妥善保管。</h4><h4>请于"+ dateFormat.format(new Date(user.getExpiresTime()))+ "之前点击下面链接激活账号,否则账号将被删除!</h4><h3><a href='http://www.estore.com/active?activeCode="+ user.getActiveCode()+ "'>http://www.estore.com/active?activeCode="+ user.getActiveCode() + "</a></h3>","text/html;charset=utf-8");// 3 transportTransport transport = session.getTransport();transport.connect("aaa", "123");transport.sendMessage(message, message.getAllRecipients());} catch (AddressException e) {e.printStackTrace();} catch (MessagingException e) {e.printStackTrace();}}
}

PaymentUtile 支付工具类

package cn.utils;import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;public class PaymentUtil {private static String encodingCharset = "UTF-8";/*** 生成hmac方法* * @param p0_Cmd*            业务类型* @param p1_MerId*            商户编号* @param p2_Order*            商户订单号* @param p3_Amt*            支付金额* @param p4_Cur*            交易币种* @param p5_Pid*            商品名称* @param p6_Pcat*            商品种类* @param p7_Pdesc*            商品描述* @param p8_Url*            商户接收支付成功数据的地址* @param p9_SAF*            送货地址* @param pa_MP*            商户扩展信息* @param pd_FrpId*            银行编码* @param pr_NeedResponse*            应答机制* @param keyValue*            商户密钥* @return*/public static String buildHmac(String p0_Cmd, String p1_MerId,String p2_Order, String p3_Amt, String p4_Cur, String p5_Pid,String p6_Pcat, String p7_Pdesc, String p8_Url, String p9_SAF,String pa_MP, String pd_FrpId, String pr_NeedResponse,String keyValue) {StringBuilder sValue = new StringBuilder();// 业务类型sValue.append(p0_Cmd);// 商户编号sValue.append(p1_MerId);// 商户订单号sValue.append(p2_Order);// 支付金额sValue.append(p3_Amt);// 交易币种sValue.append(p4_Cur);// 商品名称sValue.append(p5_Pid);// 商品种类sValue.append(p6_Pcat);// 商品描述sValue.append(p7_Pdesc);// 商户接收支付成功数据的地址sValue.append(p8_Url);// 送货地址sValue.append(p9_SAF);// 商户扩展信息sValue.append(pa_MP);// 银行编码sValue.append(pd_FrpId);// 应答机制sValue.append(pr_NeedResponse);return PaymentUtil.hmacSign(sValue.toString(), keyValue);}/*** 返回校验hmac方法* * @param hmac*            支付网关发来的加密验证码* @param p1_MerId*            商户编号* @param r0_Cmd*            业务类型* @param r1_Code*            支付结果* @param r2_TrxId*            易宝支付交易流水号* @param r3_Amt*            支付金额* @param r4_Cur*            交易币种* @param r5_Pid*            商品名称* @param r6_Order*            商户订单号* @param r7_Uid*            易宝支付会员ID* @param r8_MP*            商户扩展信息* @param r9_BType*            交易结果返回类型* @param keyValue*            密钥* @return*/public static boolean verifyCallback(String hmac, String p1_MerId,String r0_Cmd, String r1_Code, String r2_TrxId, String r3_Amt,String r4_Cur, String r5_Pid, String r6_Order, String r7_Uid,String r8_MP, String r9_BType, String keyValue) {StringBuilder sValue = new StringBuilder();// 商户编号sValue.append(p1_MerId);// 业务类型sValue.append(r0_Cmd);// 支付结果sValue.append(r1_Code);// 易宝支付交易流水号sValue.append(r2_TrxId);// 支付金额sValue.append(r3_Amt);// 交易币种sValue.append(r4_Cur);// 商品名称sValue.append(r5_Pid);// 商户订单号sValue.append(r6_Order);// 易宝支付会员IDsValue.append(r7_Uid);// 商户扩展信息sValue.append(r8_MP);// 交易结果返回类型sValue.append(r9_BType);String sNewString = PaymentUtil.hmacSign(sValue.toString(), keyValue);return sNewString.equals(hmac);}/*** @param aValue* @param aKey* @return*/public static String hmacSign(String aValue, String aKey) {byte k_ipad[] = new byte[64];byte k_opad[] = new byte[64];byte keyb[];byte value[];try {keyb = aKey.getBytes(encodingCharset);value = aValue.getBytes(encodingCharset);} catch (UnsupportedEncodingException e) {keyb = aKey.getBytes();value = aValue.getBytes();}Arrays.fill(k_ipad, keyb.length, 64, (byte) 54);Arrays.fill(k_opad, keyb.length, 64, (byte) 92);for (int i = 0; i < keyb.length; i++) {k_ipad[i] = (byte) (keyb[i] ^ 0x36);k_opad[i] = (byte) (keyb[i] ^ 0x5c);}MessageDigest md = null;try {md = MessageDigest.getInstance("MD5");} catch (NoSuchAlgorithmException e) {return null;}md.update(k_ipad);md.update(value);byte dg[] = md.digest();md.reset();md.update(k_opad);md.update(dg, 0, 16);dg = md.digest();return toHex(dg);}public static String toHex(byte input[]) {if (input == null)return null;StringBuffer output = new StringBuffer(input.length * 2);for (int i = 0; i < input.length; i++) {int current = input[i] & 0xff;if (current < 16)output.append("0");output.append(Integer.toString(current, 16));}return output.toString();}/*** * @param args* @param key* @return*/public static String getHmac(String[] args, String key) {if (args == null || args.length == 0) {return (null);}StringBuffer str = new StringBuffer();for (int i = 0; i < args.length; i++) {str.append(args[i]);}return (hmacSign(str.toString(), key));}/*** @param aValue* @return*/public static String digest(String aValue) {aValue = aValue.trim();byte value[];try {value = aValue.getBytes(encodingCharset);} catch (UnsupportedEncodingException e) {value = aValue.getBytes();}MessageDigest md = null;try {md = MessageDigest.getInstance("SHA");} catch (NoSuchAlgorithmException e) {e.printStackTrace();return null;}return toHex(md.digest(value));}// public static void main(String[] args) {// System.out.println(hmacSign("AnnulCard1000043252120080620160450.0http://localhost/SZXpro/callback.asp杩?4564868265473632445648682654736324511","8UPp0KE8sq73zVP370vko7C39403rtK1YwX40Td6irH216036H27Eb12792t"));// }
}

生成缩略图工具类

package cn.utils;import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;import org.apache.log4j.Logger;import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;/*** 制作图片缩略图* * @author seawind* */
public class PicUtils {private String srcFile;private String destFile;private int width;private int height;private Image img;private static final Logger LOG = Logger.getLogger(PicUtils.class);/*** 构造函数* * @param fileName*            String* @throws IOException*/public PicUtils(String fileName) throws IOException {File _file = new File(fileName); // 读入文件this.srcFile = fileName;String ext = this.srcFile.substring(this.srcFile.lastIndexOf("."));this.destFile = this.srcFile.substring(0, this.srcFile.lastIndexOf("."))+ "_s" + ext;LOG.info("----------------:" + destFile);img = javax.imageio.ImageIO.read(_file); // 构造Image对象width = img.getWidth(null); // 得到源图宽height = img.getHeight(null); // 得到源图长}/*** 强制压缩/放大图片到固定的大小* * @param w*            int 新宽度* @param h*            int 新高度* @throws IOException*/public void resize(int w, int h) throws IOException {BufferedImage _image = new BufferedImage(w, h,BufferedImage.TYPE_INT_RGB);_image.getGraphics().drawImage(img, 0, 0, w, h, null); // 绘制缩小后的图FileOutputStream out = new FileOutputStream(destFile); // 输出到文件流JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);encoder.encode(_image); // 近JPEG编码out.close();}/*** 按照固定的比例缩放图片* * @param t*            double 比例* @throws IOException*/public void resize(double t) throws IOException {int w = (int) (width * t);int h = (int) (height * t);resize(w, h);}/*** 以宽度为基准,等比例放缩图片* * @param w*            int 新宽度* @throws IOException*/public void resizeByWidth(int w) throws IOException {int h = (int) (height * w / width);resize(w, h);}/*** 以高度为基准,等比例缩放图片* * @param h*            int 新高度* @throws IOException*/public void resizeByHeight(int h) throws IOException {int w = (int) (width * h / height);resize(w, h);}/*** 按照最大高度限制,生成最大的等比例缩略图* * @param w*            int 最大宽度* @param h*            int 最大高度* @throws IOException*/public void resizeFix(int w, int h) throws IOException {if (width / height > w / h) {resizeByWidth(w);} else {resizeByHeight(h);}}/*** 设置目标文件名 setDestFile* * @param fileName*            String 文件名字符串*/public void setDestFile(String fileName) throws Exception {if (!fileName.endsWith(".jpg")) {throw new Exception("Dest File Must end with \".jpg\".");}destFile = fileName;}/*** 获取目标文件名 getDestFile*/public String getDestFile() {return destFile;}/*** 获取图片原始宽度 getSrcWidth*/public int getSrcWidth() {return width;}/*** 获取图片原始高度 getSrcHeight*/public int getSrcHeight() {return height;}
}

上传文件工具类

package cn.utils;import java.util.UUID;public class UploadUtils {/*** 截取真实文件名* * @param fileName* @return*/public static String subFileName(String fileName) {// 查找最后一个 \出现位置int index = fileName.lastIndexOf("\\");if (index == -1) {return fileName;}return fileName.substring(index + 1);}// 获得随机UUID文件名public static String generateRandonFileName(String fileName) {// 获得扩展名String ext = fileName.substring(fileName.lastIndexOf("."));return UUID.randomUUID().toString() + ext;}// 获得hashcode生成二级目录public static String generateRandomDir(String uuidFileName) {int hashCode = uuidFileName.hashCode();// 一级目录int d1 = hashCode & 0xf;// 二级目录int d2 = (hashCode >> 4) & 0xf;return "/" + d1 + "/" + d2;}
}

一些获得随机编号和激活码的工具类

package cn.utils;import java.util.Map;
import java.util.UUID;import org.apache.commons.beanutils.BeanUtils;
import org.apache.log4j.Logger;import sun.misc.BASE64Encoder;/*** 所有web层 相关工具方法* * @author seawind* */
public class WebUtils {private static Logger logger = Logger.getLogger(WebUtils.class);// 生成订单编号public static String generateOrderId() {String uuid = UUID.randomUUID().toString();int hashCode = uuid.hashCode();return "order-" + hashCode;}// 生成商品编号public static String generateProductId() {String uuid = UUID.randomUUID().toString();int hashCode = uuid.hashCode();return "estore-" + hashCode;}//封装数据public static void populate(Object object, Map properties) {try {BeanUtils.populate(object, properties);} catch (Exception e) {logger.error("用户注册 user 数据 封装失败...");e.printStackTrace();throw new RuntimeException(e);}}// base64编码uuid 作为激活码public static String generateActiveCode() {String uuid = UUID.randomUUID().toString();BASE64Encoder base64Encoder = new BASE64Encoder();return base64Encoder.encode(uuid.getBytes());}
}

estore案例的一些工具类相关推荐

  1. 【adcdn优化案例】某工具类app广告优化收益增长275%经验分享

    截至 2020 年 3 月,工具行业用户规模保持稳定增长,其中系统工具行业月活跃用户超 过 10 亿,实用工具行业月活跃用户超过 5 亿,移动时代,网民触媒习惯更加碎片化,工具导向的 APP 虽然在多 ...

  2. java练习案例_Java项目案例之---常用工具类练习

    常用工具类练习 1. 请根据控制台输入的特定日期格式拆分日期,如:请输入一个日期(格式如:**月**日****年),经过处理得到:****年**月**日 importjava.util.Scanner ...

  3. Java中集合相关案例(泛型通配符、Collections工具类、TreeSet、TreeMap、HashMap、HashSet和集合嵌套案例)

    集合 一.集合相关案例 1.泛型通配符案例 2.集合工具类(Collections工具类) 3.TreeSet和TreeMap案例 4.HashMap案例 5.HashSet案例 6.TreeSet案 ...

  4. 剑指高效编程之工具类

    工具类 Google Guava 工具集简介 Guava工程包含了若干被Google的Java项目广泛依赖的核心库,例如∶集合.缓存﹑原生类型支持.并发库.通用注解.字符串处理.I/O等等. 所有这些 ...

  5. hutool工具类生成二维码案例

    hutool工具类生成二维码案例 一.环境: 添加hutool工具类依赖,hutool生成二维码是利用Google的zixing,而且不是强依赖,所以还需引入zxing依赖 <dependenc ...

  6. Day18JavaSE——Map集合Collections工具类集合案例练习

    Day18JavaSE--Map集合&Collections工具类&集合案例练习 文章目录 Day18JavaSE--Map集合&Collections工具类&集合案例 ...

  7. java自学——arrary工具类和双色球案例

    java自学--arrary工具类 定义 常用方法 代码示例 双色球案例 双色彩票的玩法 分析 实现步骤 代码示例 定义 arrary工具类:用来操作数组(比如排序和搜索)的各种方法 使用这个方法需要 ...

  8. Unity 工具类 之 WWW/UnityWebRequest 下载压缩文件(zip),解压到本地且加载使用解压数据的简单案例(内也含压缩文件例子)

    Unity 工具类 之 WWW/UnityWebRequest 网络下载压缩文件(zip),解压到本地,且加载使用解压数据的简单案例(内也含压缩文件例子) 目录 Unity 工具类 之 WWW/Uni ...

  9. Java实现pdf转图片的工具类(三种方法实现PDF转图片的案例)【亲测可用】

    提示:有些时候我们需要在项目中展示PDF,所以我们可以将PDF转为图片,然后已图片的方式展示,效果很好.Java使用各种技术将pdf转换成图片格式,并且内容不失帧.清晰可见,该工具类也是开发中常用到的 ...

最新文章

  1. SAP QM QS41 试图维护Catalog为3的Code Group, 报错-You need to maintain catalog 3 (Usage Decisions) in Customi
  2. apache重定向无效
  3. arduino红外热释电传感器_压力传感器在汽车空调系统中的应用
  4. $$和$BASHPID区别
  5. 学长毕业日记 :本科毕业论文写成博士论文的神操作20170411
  6. 关于SharePoint中管理列表项权限
  7. ASP.NETmvc常用JQUERY收藏【jquery.form.js结合jquery.validate.js】
  8. SAP CDS view里如何定义association
  9. cf 1059e 思维 贪心 树
  10. [参考]查看ORACLE DB信息的一些SQL
  11. 二项式定理等价变换与简单推论
  12. php isnumber 小数点,JavaScript常用正则验证函数实例小结【年龄,数字,Email,手机,URL,日期等】...
  13. 严重性 代码 说明 项目 文件 行 警告 C4819 该文件包含不能在当前代码页(936)中表示的字符。请将该文件保存为 Unicode 格式以防止数据丢失 opencv-05 d:\opencv\o
  14. 【视频格式】webm用什么播放
  15. 3dB带宽的简要解释
  16. python flag用法_python flag什么意思
  17. CentOS 8.3.2011 镜像在PC上安装选择安装源时提示:设置基础软件仓库时出错
  18. 超火的微信渐变国旗头像,一键生成!!
  19. android edittext过滤表情,EditText过滤emoji表情
  20. Android App内部防截屏技术

热门文章

  1. 代码整洁之道 读书笔记
  2. Arduino--读取四块G-302(BH1750FVI)的光照强度
  3. 用 java swing 编写的富文本编辑器,成品小程序,拿来即可用
  4. 劳务派遣员工转正制度是怎么规定的
  5. SecureCRT 自定义标签
  6. 下拉选数据查询过来的如何设置默认值为空_如何在某些情况下禁止提交Select下拉框中的默认值或者第一个值(默认选中的就是第一个值啦……)...
  7. 服务器安培 国内销售渠道,国内药妆销售渠道分析
  8. 查询 hp服务器 网卡型号,hp服务器网卡
  9. 如何使用Createjs来编写HTML5游戏(六)完成一个简单的打飞机游戏(上)
  10. ur5+Azure kinect dk 手眼标定