();
33 | //设置字符编码
34 | hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
35 | // 指定纠错等级
36 | hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
37 | hints.put(EncodeHintType.MARGIN, 1);
38 | BitMatrix matrix = null;
39 | try {
40 | matrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
41 | } catch (WriterException e) {
42 | e.printStackTrace();
43 | }
44 |
45 | return matrix;
46 | }
47 |
48 | /**
49 | * 写入二维码、以及将照片logo写入二维码中
50 | * @param matrix 要写入的二维码
51 | * @param format 二维码照片格式
52 | * @param imagePath 二维码照片保存路径
53 | * @param logoPath logo路径
54 | * @throws java.io.IOException
55 | */
56 | public static void writeToFile(BitMatrix matrix, String format, String imagePath, String logoPath) throws IOException {
57 | MatrixToImageWriter.writeToFile(matrix, format, new File(imagePath), new MatrixToImageConfig());
58 |
59 | //添加logo图片, 此处一定需要重新进行读取,而不能直接使用二维码的BufferedImage 对象
60 | BufferedImage img = ImageIO.read(new File(imagePath));
61 | MatrixToImageWriterEx.overlapImage(img, format, imagePath, logoPath, DEFAULT_CONFIG);
62 | }
63 |
64 | /**
65 | * 写入二维码、以及将照片logo写入二维码中
66 | * @param matrix 要写入的二维码
67 | * @param format 二维码照片格式
68 | * @param imagePath 二维码照片保存路径
69 | * @param logoPath logo路径
70 | * @param logoConfig logo配置对象
71 | * @throws java.io.IOException
72 | */
73 | public static void writeToFile(BitMatrix matrix, String format, String imagePath, String logoPath, MatrixToLogoImageConfig logoConfig) throws IOException {
74 | MatrixToImageWriter.writeToFile(matrix, format, new File(imagePath), new MatrixToImageConfig());
75 |
76 | //添加logo图片, 此处一定需要重新进行读取,而不能直接使用二维码的BufferedImage 对象
77 | BufferedImage img = ImageIO.read(new File(imagePath));
78 | MatrixToImageWriterEx.overlapImage(img, format, imagePath, logoPath, logoConfig);
79 | }
80 |
81 | /**
82 | * 将照片logo添加到二维码中间
83 | * @param image 生成的二维码照片对象
84 | * @param imagePath 照片保存路径
85 | * @param logoPath logo照片路径
86 | * @param formate 照片格式
87 | */
88 | public static void overlapImage(BufferedImage image, String formate, String imagePath, String logoPath, MatrixToLogoImageConfig logoConfig) {
89 | try {
90 | //将logo写入二维码中
91 | drawImage(logoPath, image, logoConfig);
92 |
93 | //写入logo照片到二维码
94 | ImageIO.write(image, formate, new File(imagePath));
95 | } catch (Exception e) {
96 | e.printStackTrace();
97 | }
98 | }
99 |
100 | /**
101 | * 将照片添加到二维码中间,并生成流
102 | * @author:chenming
103 | * @date:2014年12月31日
104 | *
105 | * @param matrix 要写入的二维码
106 | * @param formate 照片格式
107 | * @param logoPath 要写入照片的路径
108 | * @param logoConfig logo配置对象 可以为null,为 null 默认 DEFAULT_CONFIG
109 | * @throws java.io.IOException
110 | */
111 | public static void overlapImage(BitMatrix matrix,String formate,String logoPath,MatrixToLogoImageConfig logoConfig,OutputStream out) throws IOException{
112 | //将matrix转换为bufferImage
113 | BufferedImage image = MatrixToImageWriter.toBufferedImage(matrix);
114 |
115 | //将logo照片绘制到二维码中间
116 | drawImage(logoPath, image, logoConfig);
117 |
118 | //输出
119 | ImageIO.write(image, formate, out);
120 | }
121 |
122 | /**
123 | * 将照片添加到二维码中间,并生成流
124 | * @author:chenming
125 | * @date:2014年12月31日
126 | *
127 | * @param image 要写入的二维码
128 | * @param formate 照片格式
129 | * @param logoPath 要写入照片的路径
130 | * @param logoConfig logo配置对象 可以为null,为 null 默认 DEFAULT_CONFIG
131 | * @throws java.io.IOException
132 | */
133 | public static void overlapImage(BufferedImage image,String formate,String logoPath,MatrixToLogoImageConfig logoConfig,OutputStream out) throws IOException{
134 | //将logo照片绘制到二维码中间
135 | drawImage(logoPath, image, logoConfig);
136 |
137 | //输出
138 | ImageIO.write(image, formate, out);
139 | }
140 |
141 | /**
142 | * 将logo添加到二维码中间
143 | * @author:chenming
144 | * @date:2014年12月31日
145 | *
146 | * @param logoPath logo路径
147 | * @param image 需要绘制的二维码图片
148 | * @param logoConfig 配置参数
149 | * @throws java.io.IOException
150 | */
151 | private static void drawImage(String logoPath,BufferedImage image,MatrixToLogoImageConfig logoConfig) throws IOException{
152 | if(logoConfig == null){
153 | logoConfig = DEFAULT_CONFIG;
154 | }
155 |
156 | try {
157 | BufferedImage logo = ImageIO.read(new File(logoPath));
158 | logo.setRGB(0, 0, BufferedImage.TYPE_INT_BGR);
159 | Graphics2D g = image.createGraphics();
160 |
161 | //考虑到logo照片贴到二维码中,建议大小不要超过二维码的1/5;
162 | int width = image.getWidth() / logoConfig.getLogoPart();
163 | int height = image.getHeight() / logoConfig.getLogoPart();
164 |
165 | //logo起始位置,此目的是为logo居中显示
166 | int x = (image.getWidth() - width) / 2;
167 | int y = (image.getHeight() - height) / 2;
168 |
169 | //绘制图
170 | g.drawImage(logo, x, y, width, height, null);
171 |
172 | //给logo画边框
173 | //构造一个具有指定线条宽度以及 cap 和 join 风格的默认值的实心 BasicStroke
174 | // g.setStroke(new BasicStroke(logoConfig.getBorder()));
175 | // g.setColor(logoConfig.getBorderColor());
176 | // g.drawRect(x, y, width, height);
177 |
178 | g.dispose();
179 | } catch (Exception e) { //捕捉异常后不做任何处理,防止图片路径错误而导致二维码生成失败
180 |
181 | }
182 | }
183 |
184 | }
185 |
--------------------------------------------------------------------------------
/src/main/java/com/JUtils/QRCode/MatrixToLogoImageConfig.java:
--------------------------------------------------------------------------------
1 | package com.JUtils.QRCode;
2 |
3 | import java.awt.Color;
4 |
5 | public class MatrixToLogoImageConfig {
6 | //logo默认边框颜色
7 | public static final Color DEFAULT_BORDERCOLOR = Color.RED;
8 | //logo默认边框宽度
9 | public static final int DEFAULT_BORDER = 2;
10 | //logo大小默认为照片的1/5
11 | public static final int DEFAULT_LOGOPART = 5;
12 |
13 | private final int border = DEFAULT_BORDER;
14 | private final Color borderColor;
15 | private final int logoPart;
16 |
17 | /**
18 | * Creates a default config with on color {@link #BLACK} and off color
19 | * {@link #WHITE}, generating normal black-on-white barcodes.
20 | */
21 | public MatrixToLogoImageConfig() {
22 | this(DEFAULT_BORDERCOLOR, DEFAULT_LOGOPART);
23 | }
24 |
25 |
26 | public MatrixToLogoImageConfig(Color borderColor, int logoPart) {
27 | this.borderColor = borderColor;
28 | this.logoPart = logoPart;
29 | }
30 |
31 |
32 | public Color getBorderColor() {
33 | return borderColor;
34 | }
35 |
36 |
37 | public int getBorder() {
38 | return border;
39 | }
40 |
41 |
42 | public int getLogoPart() {
43 | return logoPart;
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/src/main/java/com/JUtils/base/ConvertUtils.java:
--------------------------------------------------------------------------------
1 | package com.JUtils.base;
2 |
3 | import java.sql.Timestamp;
4 | import java.text.SimpleDateFormat;
5 |
6 | /**
7 | * 转换工具类
8 | * 若待转换值为null或者出现异常,则使用默认值
9 | *
10 | * @Author:chenssy
11 | * @date:2016年5月21日 上午10:18:12
12 | */
13 | public class ConvertUtils {
14 |
15 | /**
16 | * 字符串转换为int
17 | *
18 | * @author:chenssy
19 | * @date : 2016年5月21日 上午10:16:27
20 | *
21 | * @param str
22 | * 待转换的字符串
23 | * @param defaultValue
24 | * 默认值
25 | * @return
26 | */
27 | public static int strToInt(String str, int defaultValue) {
28 | try {
29 | defaultValue = Integer.parseInt(str);
30 | } catch (Exception localException) {
31 | }
32 | return defaultValue;
33 | }
34 |
35 | /**
36 | * String转换为long
37 | *
38 | * @author:chenssy
39 | * @date : 2016年5月21日 上午10:18:44
40 | *
41 | * @param str
42 | * 待转换字符串
43 | * @param defaultValue
44 | * 默认值
45 | * @return
46 | */
47 | public static long strToLong(String str, long defaultValue) {
48 | try {
49 | defaultValue = Long.parseLong(str);
50 | } catch (Exception localException) {
51 | }
52 | return defaultValue;
53 | }
54 |
55 | /**
56 | * 字符串转换为float
57 | *
58 | * @author:chenssy
59 | * @date : 2016年5月21日 上午10:19:12
60 | *
61 | * @param str
62 | *
63 | * @param defaultValue
64 | * @return
65 | */
66 | public static float strToFloat(String str, float defaultValue) {
67 | try {
68 | defaultValue = Float.parseFloat(str);
69 | } catch (Exception localException) {
70 | }
71 | return defaultValue;
72 | }
73 |
74 | /**
75 | * String转换为Double
76 | *
77 | * @author:chenssy
78 | * @date : 2016年5月21日 上午10:21:59
79 | *
80 | * @param str
81 | * 待转换字符串
82 | * @param defaultValue
83 | * 默认值
84 | * @return
85 | */
86 | public static double strToDouble(String str, double defaultValue) {
87 | try {
88 | defaultValue = Double.parseDouble(str);
89 | } catch (Exception localException) {
90 | }
91 | return defaultValue;
92 | }
93 |
94 | /**
95 | * 字符串转换日期
96 | *
97 | * @author:chenssy
98 | * @date : 2016年5月21日 上午10:27:01
99 | *
100 | * @param str
101 | * 待转换的字符串
102 | * @param defaultValue
103 | * 默认日期
104 | * @return
105 | */
106 | public static java.util.Date strToDate(String str,java.util.Date defaultValue) {
107 | return strToDate(str, "yyyy-MM-dd HH:mm:ss", defaultValue);
108 | }
109 |
110 | /**
111 | * 字符串转换为指定格式的日期
112 | *
113 | * @author:chenssy
114 | * @date : 2016年5月21日 上午10:27:24
115 | *
116 | * @param str
117 | * 待转换的字符串
118 | * @param format
119 | * 日期格式
120 | * @param defaultValue
121 | * 默认日期
122 | * @return
123 | */
124 | public static java.util.Date strToDate(String str, String format,java.util.Date defaultValue) {
125 | SimpleDateFormat fmt = new SimpleDateFormat(format);
126 | try {
127 | defaultValue = fmt.parse(str);
128 | } catch (Exception localException) {
129 | }
130 | return defaultValue;
131 | }
132 |
133 | /**
134 | * 日期转换为字符串
135 | *
136 | * @author:chenssy
137 | * @date : 2016年5月21日 上午10:28:05
138 | *
139 | * @param date
140 | * 待转换的日期
141 | * @param defaultValue
142 | * 默认字符串
143 | * @return
144 | */
145 | public static String dateToStr(java.util.Date date, String defaultValue) {
146 | return dateToStr(date, "yyyy-MM-dd HH:mm:ss", defaultValue);
147 | }
148 |
149 | /**
150 | * 日期转换为指定格式的字符串
151 | *
152 | * @author:chenssy
153 | * @date : 2016年5月21日 上午10:28:51
154 | *
155 | * @param date
156 | * 待转换的日期
157 | * @param format
158 | * 指定格式
159 | * @param defaultValue
160 | * 默认值
161 | * @return
162 | */
163 | public static String dateToStr(java.util.Date date, String format, String defaultValue) {
164 | SimpleDateFormat sdf = new SimpleDateFormat(format);
165 | try {
166 | defaultValue = sdf.format(date);
167 | } catch (Exception localException) {
168 | }
169 | return defaultValue;
170 | }
171 |
172 | /**
173 | * 如果字符串为空则使用默认字符串
174 | *
175 | * @author:chenssy
176 | * @date : 2016年5月21日 上午10:29:35
177 | *
178 | * @param str
179 | * 字符串
180 | * @param defaultValue
181 | * 默认值
182 | * @return
183 | */
184 | public static String strToStr(String str, String defaultValue) {
185 | if ((str != null) && (!(str.isEmpty())))
186 | defaultValue = str;
187 | return defaultValue;
188 | }
189 |
190 | /**
191 | * util date 转换为 sqldate
192 | *
193 | * @author:chenssy
194 | * @date : 2016年5月21日 上午10:30:09
195 | *
196 | * @param date
197 | * @return
198 | */
199 | public static java.sql.Date dateToSqlDate(java.util.Date date) {
200 | return new java.sql.Date(date.getTime());
201 | }
202 |
203 | /**
204 | * sql date 转换为 util date
205 | *
206 | * @author:chenssy
207 | * @date : 2016年5月21日 上午10:30:26
208 | *
209 | * @param date
210 | * @return
211 | */
212 | public static java.util.Date sqlDateToDate(java.sql.Date date) {
213 | return new java.util.Date(date.getTime());
214 | }
215 |
216 | /**
217 | * date 转换为 timestamp
218 | *
219 | * @author:chenssy
220 | * @date : 2016年5月21日 上午10:30:51
221 | *
222 | * @param date
223 | * @return
224 | */
225 | public static Timestamp dateToSqlTimestamp(java.util.Date date) {
226 | return new Timestamp(date.getTime());
227 | }
228 |
229 | /**
230 | * timestamp 转换为date
231 | *
232 | * @author:chenssy
233 | * @date : 2016年5月21日 上午10:31:13
234 | *
235 | * @param date
236 | * @return
237 | */
238 | public static java.util.Date qlTimestampToDate(Timestamp date) {
239 | return new java.util.Date(date.getTime());
240 | }
241 | }
242 |
--------------------------------------------------------------------------------
/src/main/java/com/JUtils/base/IdcardValidator.java:
--------------------------------------------------------------------------------
1 | package com.JUtils.base;
2 |
3 | import java.util.regex.Pattern;
4 |
5 | /**
6 | * --15位身份证号码:第7、8位为出生年份(两位数),第9、10位为出生月份,第11、12位代表出生日期,第15位代表性别,奇数为男,偶数为女。
7 | * --18位身份证号码
8 | * :第7、8、9、10位为出生年份(四位数),第11、第12位为出生月份,第13、14位代表出生日期,第17位代表性别,奇数为男,偶数为女。
9 | *
10 | * @Author:chenssy
11 | * @date:2016年6月1日 下午12:29:41
12 | *
13 | */
14 | public class IdcardValidator {
15 |
16 | /**
17 | * 省,直辖市代码表: { 11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古",
18 | * 21:"辽宁",22:"吉林",23:"黑龙江",31:"上海",32:"江苏",
19 | * 33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山东",41:"河南",
20 | * 42:"湖北",43:"湖南",44:"广东",45:"广西",46:"海南",50:"重庆",
21 | * 51:"四川",52:"贵州",53:"云南",54:"西藏",61:"陕西",62:"甘肃",
22 | * 63:"青海",64:"宁夏",65:"新疆",71:"台湾",81:"香港",82:"澳门",91:"国外"}
23 | */
24 |
25 | // 每位加权因子
26 | private static int power[] = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
27 |
28 | /**
29 | * 验证身份证是否合法
30 | *
31 | * @author : chenssy
32 | * @date : 2016年6月1日 下午12:30:03
33 | *
34 | * @param idcard
35 | * @return
36 | */
37 | @SuppressWarnings("static-access")
38 | public boolean isValidatedAllIdcard(String idcard) {
39 | return this.isValidate18Idcard(idcard);
40 | }
41 |
42 | /**
43 | *
44 |
45 | * 判断18位身份证的合法性
46 | *
47 | * 根据〖中华人民共和国国家标准GB11643-1999〗中有关公民身份号码的规定,公民身份号码是特征组合码,由十七位数字本体码和一位数字校验码组成。
48 | * 排列顺序从左至右依次为:六位数字地址码,八位数字出生日期码,三位数字顺序码和一位数字校验码。
49 | *
50 | * 顺序码: 表示在同一地址码所标识的区域范围内,对同年、同月、同 日出生的人编定的顺序号,顺序码的奇数分配给男性,偶数分配 给女性。
51 | *
52 | *
53 | * 1.前1、2位数字表示:所在省份的代码; 2.第3、4位数字表示:所在城市的代码; 3.第5、6位数字表示:所在区县的代码;
54 | * 4.第7~14位数字表示:出生年、月、日; 5.第15、16位数字表示:所在地的派出所的代码;
55 | * 6.第17位数字表示性别:奇数表示男性,偶数表示女性;
56 | * 7.第18位数字是校检码:也有的说是个人信息码,一般是随计算机的随机产生,用来检验身份证的正确性。校检码可以是0~9的数字,有时也用x表示。
57 | *
58 | *
59 | * 第十八位数字(校验码)的计算方法为: 1.将前面的身份证号码17位数分别乘以不同的系数。从第一位到第十七位的系数分别为:7 9 10 5 8 4
60 | * 2 1 6 3 7 9 10 5 8 4 2
61 | *
62 | *
63 | * 2.将这17位数字和系数相乘的结果相加。
64 | *
65 | *
66 | * 3.用加出来和除以11,看余数是多少?
67 | *
68 | * 4.余数只可能有0 1 2 3 4 5 6 7 8 9 10这11个数字。其分别对应的最后一位身份证的号码为1 0 X 9 8 7 6 5 4 3
69 | * 2。
70 | *
71 | * 5.通过上面得知如果余数是2,就会在身份证的第18位数字上出现罗马数字的Ⅹ。如果余数是10,身份证的最后一位号码就是2。
72 | *
73 | *
74 | * @author : chenssy
75 | * @date : 2016年6月1日 下午12:31:10
76 | *
77 | * @param idcard
78 | * 待验证的身份证
79 | * @return
80 | */
81 | public static boolean isValidate18Idcard(String idcard) {
82 | // 非18位为假
83 | if (idcard.length() != 18) {
84 | return false;
85 | }
86 | // 获取前17位
87 | String idcard17 = idcard.substring(0, 17);
88 | // 获取第18位
89 | String idcard18Code = idcard.substring(17, 18);
90 | char c[] = null;
91 | String checkCode = "";
92 | // 是否都为数字
93 | if (isDigital(idcard17)) {
94 | c = idcard17.toCharArray();
95 | } else {
96 | return false;
97 | }
98 |
99 | if (null != c) {
100 | int bit[] = new int[idcard17.length()];
101 | bit = converCharToInt(c);
102 | int sum17 = 0;
103 | sum17 = getPowerSum(bit);
104 | // 将和值与11取模得到余数进行校验码判断
105 | checkCode = getCheckCodeBySum(sum17);
106 | if (null == checkCode) {
107 | return false;
108 | }
109 | // 将身份证的第18位与算出来的校码进行匹配,不相等就为假
110 | if (!idcard18Code.equalsIgnoreCase(checkCode)) {
111 | return false;
112 | }
113 | }
114 |
115 | return true;
116 | }
117 |
118 | /**
119 | * 18位身份证号码的基本数字和位数验校
120 | *
121 | * @author : chenssy
122 | * @date : 2016年6月1日 下午12:31:49
123 | *
124 | * @param idcard
125 | * 待验证的身份证
126 | * @return
127 | */
128 | public static boolean is18Idcard(String idcard) {
129 | return Pattern.matches("^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([\\d|x|X]{1})$", idcard);
130 | }
131 |
132 | /**
133 | * 数字验证
134 | *
135 | * @author : chenssy
136 | * @date : 2016年6月1日 下午12:32:18
137 | *
138 | * @param str
139 | * @return
140 | */
141 | private static boolean isDigital(String str) {
142 | return str == null || "".equals(str) ? false : str.matches("^[0-9]*$");
143 | }
144 |
145 | /**
146 | * 将身份证的每位和对应位的加权因子相乘之后,再得到和值
147 | *
148 | * @author : chenssy
149 | * @date : 2016年6月1日 下午12:32:34
150 | *
151 | * @param bit
152 | * @return
153 | */
154 | private static int getPowerSum(int[] bit) {
155 | int sum = 0;
156 | if (power.length != bit.length) {
157 | return sum;
158 | }
159 |
160 | for (int i = 0; i < bit.length; i++) {
161 | for (int j = 0; j < power.length; j++) {
162 | if (i == j) {
163 | sum = sum + bit[i] * power[j];
164 | }
165 | }
166 | }
167 |
168 | return sum;
169 | }
170 |
171 | /**
172 | * 将和值与11取模得到余数进行校验码判断
173 | *
174 | * @author : chenssy
175 | * @date : 2016年6月1日 下午12:32:51
176 | *
177 | * @param sum17
178 | * @return
179 | */
180 | private static String getCheckCodeBySum(int sum17) {
181 | String checkCode = null;
182 | switch (sum17 % 11) {
183 | case 10:
184 | checkCode = "2";
185 | break;
186 | case 9:
187 | checkCode = "3";
188 | break;
189 | case 8:
190 | checkCode = "4";
191 | break;
192 | case 7:
193 | checkCode = "5";
194 | break;
195 | case 6:
196 | checkCode = "6";
197 | break;
198 | case 5:
199 | checkCode = "7";
200 | break;
201 | case 4:
202 | checkCode = "8";
203 | break;
204 | case 3:
205 | checkCode = "9";
206 | break;
207 | case 2:
208 | checkCode = "x";
209 | break;
210 | case 1:
211 | checkCode = "0";
212 | break;
213 | case 0:
214 | checkCode = "1";
215 | break;
216 | }
217 | return checkCode;
218 | }
219 |
220 | /**
221 | * 将字符数组转为整型数组
222 | *
223 | * @author : chenssy
224 | * @date : 2016年6月1日 下午12:33:22
225 | *
226 | * @param c
227 | * @return
228 | * @throws NumberFormatException
229 | */
230 | private static int[] converCharToInt(char[] c) throws NumberFormatException {
231 | int[] a = new int[c.length];
232 | int k = 0;
233 | for (char temp : c) {
234 | a[k++] = Integer.parseInt(String.valueOf(temp));
235 | }
236 | return a;
237 | }
238 |
239 | /**
240 | *
241 | * @param idno
242 | * @return 身份证信息中代表性别的数值
243 | */
244 | public static int getUserSex(String idno) {
245 | String sex="1";
246 | if(idno!=null){
247 | if(idno.length()>15){
248 | sex = idno.substring(16, 17);
249 | }
250 | }
251 |
252 | return Integer.parseInt(sex) % 2==0 ? 0:1;
253 | }
254 | }
255 |
--------------------------------------------------------------------------------
/src/main/java/com/JUtils/base/MoneyUtils.java:
--------------------------------------------------------------------------------
1 | package com.JUtils.base;
2 |
3 | import java.math.BigDecimal;
4 | import java.math.RoundingMode;
5 |
6 | /**
7 | * 金钱处理工具类
8 | *
9 | * @Author:chenssy
10 | * @date:2014年8月7日
11 | */
12 | public class MoneyUtils {
13 |
14 | /**
15 | * 汉语中数字大写
16 | */
17 | private static final String[] CN_UPPER_NUMBER = {"零","壹","贰","叁","肆","伍","陆","柒","捌","玖" };
18 |
19 | /**
20 | * 汉语中货币单位大写
21 | */
22 | private static final String[] CN_UPPER_MONETRAY_UNIT = { "分", "角", "元","拾", "佰", "仟", "万", "拾",
23 | "佰", "仟", "亿", "拾", "佰", "仟", "兆", "拾",
24 | "佰", "仟" };
25 | /**
26 | * 特殊字符:整
27 | */
28 | private static final String CN_FULL = "";
29 |
30 | /**
31 | * 特殊字符:负
32 | */
33 | private static final String CN_NEGATIVE = "负";
34 | /**
35 | * 零元整
36 | */
37 | private static final String CN_ZEOR_FULL = "零元整";
38 |
39 | /**
40 | * 金额的精度,默认值为2
41 | */
42 | private static final int MONEY_PRECISION = 2;
43 |
44 | /**
45 | * 人民币转换为大写,格式为:x万x千x百x十x元x角x分
46 | *
47 | * @autor:chenssy
48 | * @date:2014年8月7日
49 | *
50 | * @param numberOfMoney 传入的金额
51 | * @return
52 | */
53 | public static String number2CNMontray(String numberOfMoney) {
54 | return number2CNMontray(new BigDecimal(numberOfMoney));
55 | }
56 |
57 |
58 | /**
59 | * 人民币转换为大写,格式为:x万x千x百x十x元x角x分
60 | * @autor:chenssy
61 | * @date:2014年8月7日
62 | *
63 | * @param numberOfMoney
64 | * 传入的金额
65 | * @return
66 | */
67 | public static String number2CNMontray(BigDecimal numberOfMoney) {
68 | StringBuffer sb = new StringBuffer();
69 | int signum = numberOfMoney.signum();
70 | // 零元整的情况
71 | if (signum == 0) {
72 | return CN_ZEOR_FULL;
73 | }
74 | //这里会进行金额的四舍五入
75 | long number = numberOfMoney.movePointRight(MONEY_PRECISION).setScale(0, 4).abs().longValue();
76 | // 得到小数点后两位值
77 | long scale = number % 100;
78 | int numUnit = 0;
79 | int numIndex = 0;
80 | boolean getZero = false;
81 | // 判断最后两位数,一共有四中情况:00 = 0, 01 = 1, 10, 11
82 | if (!(scale > 0)) {
83 | numIndex = 2;
84 | number = number / 100;
85 | getZero = true;
86 | }
87 | if ((scale > 0) && (!(scale % 10 > 0))) {
88 | numIndex = 1;
89 | number = number / 10;
90 | getZero = true;
91 | }
92 | int zeroSize = 0;
93 | while (true) {
94 | if (number <= 0) {
95 | break;
96 | }
97 | // 每次获取到最后一个数
98 | numUnit = (int) (number % 10);
99 | if (numUnit > 0) {
100 | if ((numIndex == 9) && (zeroSize >= 3)) {
101 | sb.insert(0, CN_UPPER_MONETRAY_UNIT[6]);
102 | }
103 | if ((numIndex == 13) && (zeroSize >= 3)) {
104 | sb.insert(0, CN_UPPER_MONETRAY_UNIT[10]);
105 | }
106 | sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
107 | sb.insert(0, CN_UPPER_NUMBER[numUnit]);
108 | getZero = false;
109 | zeroSize = 0;
110 | } else {
111 | ++zeroSize;
112 | if (!(getZero)) {
113 | sb.insert(0, CN_UPPER_NUMBER[numUnit]);
114 | }
115 | if (numIndex == 2) {
116 | if (number > 0) {
117 | sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
118 | }
119 | } else if (((numIndex - 2) % 4 == 0) && (number % 1000 > 0)) {
120 | sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
121 | }
122 | getZero = true;
123 | }
124 | // 让number每次都去掉最后一个数
125 | number = number / 10;
126 | ++numIndex;
127 | }
128 | // 如果signum == -1,则说明输入的数字为负数,就在最前面追加特殊字符:负
129 | if (signum == -1) {
130 | sb.insert(0, CN_NEGATIVE);
131 | }
132 | // 输入的数字小数点后两位为"00"的情况,则要在最后追加特殊字符:整
133 | if (!(scale > 0)) {
134 | sb.append(CN_FULL);
135 | }
136 | return sb.toString();
137 | }
138 |
139 | /**
140 | * 将人民币转换为会计格式金额(xxxx,xxxx,xxxx.xx),保留两位小数
141 | * @autor:chenssy
142 | * @date:2014年8月7日
143 | *
144 | * @param money
145 | * 待转换的金额
146 | * @return
147 | */
148 | public static String accountantMoney(BigDecimal money){
149 | return accountantMoney(money, 2, 1);
150 | }
151 |
152 | /**
153 | * 格式化金额,显示为xxx万元,xxx百万,xxx亿
154 | * @autor:chenssy
155 | * @date:2014年8月7日
156 | *
157 | * @param money
158 | * 待处理的金额
159 | * @param scale
160 | * 小数点后保留的位数
161 | * @param divisor
162 | * 格式化值(10:十元、100:百元,1000千元,10000万元......)
163 | * @return
164 | */
165 | public static String getFormatMoney(BigDecimal money,int scale,double divisor){
166 | return formatMoney(money, scale, divisor) + getCellFormat(divisor);
167 | }
168 |
169 | /**
170 | * 获取会计格式的人民币(格式为:xxxx,xxxx,xxxx.xx)
171 | * @autor:chenssy
172 | * @date:2014年8月7日
173 | *
174 | * @param money
175 | * 待处理的金额
176 | * @param scale
177 | * 小数点后保留的位数
178 | * @param divisor
179 | * 格式化值(10:十元、100:百元,1000千元,10000万元......)
180 | * @return
181 | */
182 | public static String getAccountantMoney(BigDecimal money, int scale, double divisor){
183 | return accountantMoney(money, scale, divisor) + getCellFormat(divisor);
184 | }
185 |
186 | /**
187 | * 将人民币转换为会计格式金额(xxxx,xxxx,xxxx.xx)
188 | * @autor:chenssy
189 | * @date:2014年8月7日
190 | *
191 | * @param money
192 | * 待处理的金额
193 | * @param scale
194 | * 小数点后保留的位数
195 | * @param divisor
196 | * 格式化值
197 | * @return
198 | */
199 | private static String accountantMoney(BigDecimal money,int scale,double divisor){
200 | String disposeMoneyStr = formatMoney(money, scale, divisor);
201 | //小数点处理
202 | int dotPosition = disposeMoneyStr.indexOf(".");
203 | String exceptDotMoeny = null;//小数点之前的字符串
204 | String dotMeony = null;//小数点之后的字符串
205 | if(dotPosition > 0){
206 | exceptDotMoeny = disposeMoneyStr.substring(0,dotPosition);
207 | dotMeony = disposeMoneyStr.substring(dotPosition);
208 | }else{
209 | exceptDotMoeny = disposeMoneyStr;
210 | }
211 | //负数处理
212 | int negativePosition = exceptDotMoeny.indexOf("-");
213 | if(negativePosition == 0){
214 | exceptDotMoeny = exceptDotMoeny.substring(1);
215 | }
216 | StringBuffer reverseExceptDotMoney = new StringBuffer(exceptDotMoeny);
217 | reverseExceptDotMoney.reverse();//字符串倒转
218 | char[] moneyChar = reverseExceptDotMoney.toString().toCharArray();
219 | StringBuffer returnMeony = new StringBuffer();//返回值
220 | for(int i = 0; i < moneyChar.length; i++){
221 | if(i != 0 && i % 3 == 0){
222 | returnMeony.append(",");//每隔3位加','
223 | }
224 | returnMeony.append(moneyChar[i]);
225 | }
226 | returnMeony.reverse();//字符串倒转
227 | if(dotPosition > 0){
228 | returnMeony.append(dotMeony);
229 | }
230 | if(negativePosition == 0){
231 | return "-" + returnMeony.toString();
232 | }else{
233 | return returnMeony.toString();
234 | }
235 | }
236 |
237 | /**
238 | * 格式化金额,显示为xxx万元,xxx百万,xxx亿
239 | * @autor:chenssy
240 | * @date:2014年8月7日
241 | *
242 | * @param money
243 | * 待处理的金额
244 | * @param scale
245 | * 小数点后保留的位数
246 | * @param divisor
247 | * 格式化值
248 | * @return
249 | */
250 | private static String formatMoney(BigDecimal money,int scale,double divisor){
251 | if (divisor == 0) {
252 | return "0.00";
253 | }
254 | if (scale < 0) {
255 | return "0.00";
256 | }
257 | BigDecimal divisorBD = new BigDecimal(divisor);
258 | return money.divide(divisorBD, scale, RoundingMode.HALF_UP).toString();
259 | }
260 |
261 | private static String getCellFormat(double divisor){
262 | String str = String.valueOf(divisor);
263 | int len = str.substring(0,str.indexOf(".")).length();
264 | String cell = "";
265 | switch(len){
266 | case 1:
267 | cell = "元";
268 | break;
269 | case 2:
270 | cell = "十元";
271 | break;
272 | case 3:
273 | cell = "百元";
274 | break;
275 | case 4:
276 | cell = "千元";
277 | break;
278 | case 5:
279 | cell = "万元";
280 | break;
281 | case 6:
282 | cell = "十万元";
283 | break;
284 | case 7:
285 | cell = "百万元";
286 | break;
287 | case 8:
288 | cell = "千万元";
289 | break;
290 | case 9:
291 | cell = "亿元";
292 | break;
293 | case 10:
294 | cell = "十亿元";
295 | break;
296 | }
297 | return cell;
298 | }
299 | }
300 |
--------------------------------------------------------------------------------
/src/main/java/com/JUtils/base/RegexUtils.java:
--------------------------------------------------------------------------------
1 | package com.JUtils.base;
2 |
3 | import java.util.regex.Matcher;
4 | import java.util.regex.Pattern;
5 |
6 | /**
7 | * 正则表达式工具类,验证数据是否符合规范
8 | *
9 | * @Author:chenssy
10 | * @date:2014年8月7日
11 | */
12 | public class RegexUtils {
13 |
14 | /**
15 | * 判断字符串是否符合正则表达式
16 | *
17 | * @author : chenssy
18 | * @date : 2016年6月1日 下午12:43:05
19 | *
20 | * @param str
21 | * @param regex
22 | * @return
23 | */
24 | public static boolean find(String str, String regex) {
25 | Pattern p = Pattern.compile(regex);
26 | Matcher m = p.matcher(str);
27 | boolean b = m.find();
28 | return b;
29 | }
30 |
31 | /**
32 | * 判断输入的字符串是否符合Email格式.
33 | * @autor:chenssy
34 | * @date:2014年8月7日
35 | *
36 | * @param email
37 | * 传入的字符串
38 | * @return 符合Email格式返回true,否则返回false
39 | */
40 | public static boolean isEmail(String email) {
41 | if (email == null || email.length() < 1 || email.length() > 256) {
42 | return false;
43 | }
44 | Pattern pattern = Pattern.compile("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$");
45 | return pattern.matcher(email).matches();
46 | }
47 |
48 | /**
49 | * 判断输入的字符串是否为纯汉字
50 | * @autor:chenssy
51 | * @date:2014年8月7日
52 | *
53 | * @param value
54 | * 传入的字符串
55 | * @return
56 | */
57 | public static boolean isChinese(String value) {
58 | Pattern pattern = Pattern.compile("[\u0391-\uFFE5]+$");
59 | return pattern.matcher(value).matches();
60 | }
61 |
62 | /**
63 | * 判断是否为浮点数,包括double和float
64 | * @autor:chenssy
65 | * @date:2014年8月7日
66 | *
67 | * @param value
68 | * 传入的字符串
69 | * @return
70 | */
71 | public static boolean isDouble(String value) {
72 | Pattern pattern = Pattern.compile("^[-\\+]?\\d+\\.\\d+$");
73 | return pattern.matcher(value).matches();
74 | }
75 |
76 | /**
77 | * 判断是否为整数
78 | * @autor:chenssy
79 | * @date:2014年8月7日
80 | *
81 | * @param value
82 | * 传入的字符串
83 | * @return
84 | */
85 | public static boolean isInteger(String value) {
86 | Pattern pattern = Pattern.compile("^[-\\+]?[\\d]+$");
87 | return pattern.matcher(value).matches();
88 | }
89 | }
90 |
--------------------------------------------------------------------------------
/src/main/java/com/JUtils/base/StringUtils.java:
--------------------------------------------------------------------------------
1 | package com.JUtils.base;
2 |
3 | import java.util.regex.Matcher;
4 | import java.util.regex.Pattern;
5 |
6 | /**
7 | *
8 | * 字符串工具类,对字符串进行常规的处理
9 | *
10 | * @Author:chenssy
11 | * @date:2014年8月5日
12 | */
13 | public class StringUtils {
14 |
15 | /**
16 | * 将半角的符号转换成全角符号.(即英文字符转中文字符)
17 | * @autor:chenssy
18 | * @date:2014年8月7日
19 | *
20 | * @param str
21 | * 要转换的字符
22 | * @return
23 | */
24 | public static String changeToFull(String str) {
25 | String source = "1234567890!@#$%^&*()abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_=+\\|[];:'\",<.>/?";
26 | String[] decode = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "0",
27 | "!", "@", "#", "$", "%", "︿", "&", "*", "(", ")", "a", "b",
28 | "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n",
29 | "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
30 | "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L",
31 | "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X",
32 | "Y", "Z", "-", "_", "=", "+", "\", "|", "【", "】", ";", ":",
33 | "'", "\"", ",", "〈", "。", "〉", "/", "?" };
34 | String result = "";
35 | for (int i = 0; i < str.length(); i++) {
36 | int pos = source.indexOf(str.charAt(i));
37 | if (pos != -1) {
38 | result += decode[pos];
39 | } else {
40 | result += str.charAt(i);
41 | }
42 | }
43 | return result;
44 | }
45 |
46 | /**
47 | * 将字符转换为编码为Unicode,格式 为'\u0020'
48 | * unicodeEscaped(' ') = "\u0020"
49 | * unicodeEscaped('A') = "\u0041"
50 | * @autor:chenssy
51 | * @date:2014年8月7日
52 | *
53 | * @param ch
54 | * 待转换的char 字符
55 | * @return
56 | */
57 | public static String unicodeEscaped(char ch) {
58 | if (ch < 0x10) {
59 | return "\\u000" + Integer.toHexString(ch);
60 | } else if (ch < 0x100) {
61 | return "\\u00" + Integer.toHexString(ch);
62 | } else if (ch < 0x1000) {
63 | return "\\u0" + Integer.toHexString(ch);
64 | }
65 | return "\\u" + Integer.toHexString(ch);
66 | }
67 |
68 | /**
69 | * 进行toString操作,若为空,返回默认值
70 | * @autor:chenssy
71 | * @date:2014年8月9日
72 | *
73 | * @param object
74 | * 要进行toString操作的对象
75 | * @param nullStr
76 | * 返回的默认值
77 | * @return
78 | */
79 | public static String toString(Object object,String nullStr){
80 | return object == null ? nullStr : object.toString();
81 | }
82 |
83 | /**
84 | * 将字符串重复N次,null、""不在循环次数里面
85 | * 当value == null || value == "" return value;
86 | * 当count <= 1 返回 value
87 | * @autor:chenssy
88 | * @date:2014年8月9日
89 | *
90 | * @param value
91 | * 需要循环的字符串
92 | * @param count
93 | * 循环的次数
94 | * @return
95 | */
96 | public static String repeatString(String value,int count){
97 | if(value == null || "".equals(value) || count <= 1){
98 | return value;
99 | }
100 |
101 | int length = value.length();
102 | if(length == 1){ //长度为1,存在字符
103 | return repeatChar(value.charAt(0), count);
104 | }
105 |
106 | int outputLength = length * count;
107 | switch (length) {
108 | case 1:
109 | return repeatChar(value.charAt(0), count);
110 | case 2:
111 | char ch0 = value.charAt(0);
112 | char ch1 = value.charAt(1);
113 | char[] output2 = new char[outputLength];
114 | for (int i = count * 2 - 2; i >= 0; i--, i--) {
115 | output2[i] = ch0;
116 | output2[i + 1] = ch1;
117 | }
118 | return new String(output2);
119 | default:
120 | StringBuilder buf = new StringBuilder(outputLength);
121 | for (int i = 0; i < count; i++) {
122 | buf.append(value);
123 | }
124 | return buf.toString();
125 | }
126 |
127 | }
128 |
129 | /**
130 | * 将某个字符重复N次
131 | * @autor:chenssy
132 | * @date:2014年8月9日
133 | *
134 | * @param ch
135 | * 需要循环的字符
136 | * @param count
137 | * 循环的次数
138 | * @return
139 | */
140 | public static String repeatChar(char ch, int count) {
141 | char[] buf = new char[count];
142 | for (int i = count - 1; i >= 0; i--) {
143 | buf[i] = ch;
144 | }
145 | return new String(buf);
146 | }
147 |
148 | /**
149 | * 判断字符串是否全部都为小写
150 | * @autor:chenssy
151 | * @date:2014年8月9日
152 | *
153 | * @param value
154 | * 待判断的字符串
155 | * @return
156 | */
157 | public static boolean isAllLowerCase(String value){
158 | if(value == null || "".equals(value)){
159 | return false;
160 | }
161 | for (int i = 0; i < value.length(); i++) {
162 | if (Character.isLowerCase(value.charAt(i)) == false) {
163 | return false;
164 | }
165 | }
166 | return true;
167 | }
168 |
169 | /**
170 | * 判断字符串是否全部大写
171 | * @autor:chenssy
172 | * @date:2014年8月9日
173 | *
174 | * @param value
175 | * 待判断的字符串
176 | * @return
177 | */
178 | public static boolean isAllUpperCase(String value){
179 | if(value == null || "".equals(value)){
180 | return false;
181 | }
182 | for (int i = 0; i < value.length(); i++) {
183 | if (Character.isUpperCase(value.charAt(i)) == false) {
184 | return false;
185 | }
186 | }
187 | return true;
188 | }
189 |
190 | /**
191 | * 反转字符串
192 | * @autor:chenssy
193 | * @date:2014年8月9日
194 | *
195 | * @param value
196 | * 待反转的字符串
197 | * @return
198 | */
199 | public static String reverse(String value){
200 | if(value == null){
201 | return null;
202 | }
203 | return new StringBuffer(value).reverse().toString();
204 | }
205 |
206 | /**
207 | * @desc:截取字符串,支持中英文混乱,其中中文当做两位处理
208 | * @autor:chenssy
209 | * @date:2014年8月10日
210 | *
211 | * @param resourceString
212 | * @param length
213 | * @return
214 | */
215 | public static String subString(String resourceString,int length){
216 | String resultString = "";
217 | if (resourceString == null || "".equals(resourceString) || length < 1) {
218 | return resourceString;
219 | }
220 |
221 | if (resourceString.length() < length) {
222 | return resourceString;
223 | }
224 |
225 | char[] chr = resourceString.toCharArray();
226 | int strNum = 0;
227 | int strGBKNum = 0;
228 | boolean isHaveDot = false;
229 |
230 | for (int i = 0; i < resourceString.length(); i++) {
231 | if (chr[i] >= 0xa1){// 0xa1汉字最小位开始
232 | strNum = strNum + 2;
233 | strGBKNum++;
234 | } else {
235 | strNum++;
236 | }
237 |
238 | if (strNum == length || strNum == length + 1) {
239 | if (i + 1 < resourceString.length()) {
240 | isHaveDot = true;
241 | }
242 | break;
243 | }
244 | }
245 | resultString = resourceString.substring(0, strNum - strGBKNum);
246 | if (isHaveDot) {
247 | resultString = resultString + "...";
248 | }
249 |
250 | return resultString;
251 | }
252 |
253 | /**
254 | *
255 | * @autor:chenssy
256 | * @date:2014年8月10日
257 | *
258 | * @param htmlString
259 | * @param length
260 | * @return
261 | */
262 | public static String subHTMLString(String htmlString,int length){
263 | return subString(delHTMLTag(htmlString), length);
264 | }
265 |
266 | /**
267 | * 过滤html标签,包括script、style、html、空格、回车标签
268 | * @autor:chenssy
269 | * @date:2014年8月10日
270 | *
271 | * @param htmlStr
272 | * @return
273 | */
274 | public static String delHTMLTag(String htmlStr){
275 | String regEx_script = "