Commit 8f080054ffa6d61aa66aceac4d3b7ae42e48551a
1 parent
0b96f53a
Exists in
master
first submit
Showing
14 changed files
with
1694 additions
and
12 deletions
Show diff stats
build.gradle
@@ -8,23 +8,37 @@ | @@ -8,23 +8,37 @@ | ||
8 | 8 | ||
9 | plugins { | 9 | plugins { |
10 | // Apply the java-library plugin to add support for Java Library | 10 | // Apply the java-library plugin to add support for Java Library |
11 | - id 'java-library' | 11 | + id 'java' |
12 | + id 'eclipse' | ||
13 | + id 'application' | ||
12 | } | 14 | } |
13 | 15 | ||
14 | -dependencies { | ||
15 | - // This dependency is exported to consumers, that is to say found on their compile classpath. | ||
16 | - api 'org.apache.commons:commons-math3:3.6.1' | ||
17 | - | ||
18 | - // This dependency is used internally, and not exposed to consumers on their own compile classpath. | ||
19 | - implementation 'com.google.guava:guava:23.0' | 16 | +jar.enabled = true |
17 | +group = 'com.taover.util' | ||
18 | +mainClassName = 'com.taover.util.UtilString' | ||
20 | 19 | ||
21 | - // Use JUnit test framework | ||
22 | - testImplementation 'junit:junit:4.12' | 20 | +dependencies { |
21 | + compile( | ||
22 | + "org.apache.poi:poi:3.16", | ||
23 | + "org.apache.poi:poi-excelant:3.16", | ||
24 | + "log4j:log4j:1.2.17", | ||
25 | + "commons-logging:commons-logging:1.2", | ||
26 | + "ch.ethz.ganymed:ganymed-ssh2:build210", | ||
27 | + "org.apache.velocity:velocity:1.6.4", | ||
28 | + "com.squareup.okhttp3:okhttp:3.14.1", | ||
29 | + "com.belerweb:pinyin4j:2.5.1" | ||
30 | + ) | ||
23 | } | 31 | } |
24 | 32 | ||
25 | -// In this section you declare where to find the dependencies of your project | ||
26 | repositories { | 33 | repositories { |
27 | - // Use jcenter for resolving your dependencies. | ||
28 | - // You can declare any Maven/Ivy/file repository here. | ||
29 | jcenter() | 34 | jcenter() |
30 | } | 35 | } |
36 | + | ||
37 | +task sourcesJar(type: Jar, dependsOn: classes) { | ||
38 | + classifier = 'sources' | ||
39 | + from sourceSets.main.allSource | ||
40 | +} | ||
41 | + | ||
42 | +artifacts { | ||
43 | + archives sourcesJar | ||
44 | +} |
@@ -0,0 +1,68 @@ | @@ -0,0 +1,68 @@ | ||
1 | +package com.taover.util; | ||
2 | + | ||
3 | +import java.lang.reflect.Array; | ||
4 | +import java.lang.reflect.Field; | ||
5 | +import java.util.ArrayList; | ||
6 | +import java.util.HashMap; | ||
7 | +import java.util.List; | ||
8 | +import java.util.Map; | ||
9 | + | ||
10 | +public class UtilCollection{ | ||
11 | + | ||
12 | + public static Map<String, ?> listToMap(List<?> elements, String keyFieldName){ | ||
13 | + Map<String, Object> result = new HashMap<String, Object>(); | ||
14 | + if(elements==null || keyFieldName==null || keyFieldName.isEmpty()){ | ||
15 | + return null; | ||
16 | + } | ||
17 | + for(int i=0; i<elements.size(); ++i){ | ||
18 | + Object tempObject = elements.get(i); | ||
19 | + try { | ||
20 | + Field tempField = tempObject.getClass().getDeclaredField(keyFieldName); | ||
21 | + tempField.setAccessible(true); | ||
22 | + Object tempValue = tempField.get(tempObject); | ||
23 | + if(tempValue == null){ | ||
24 | + result.put(null, tempObject); | ||
25 | + }else{ | ||
26 | + result.put(tempValue.toString(), tempObject); | ||
27 | + } | ||
28 | + } catch (Exception e) { | ||
29 | + continue; | ||
30 | + } | ||
31 | + } | ||
32 | + return result; | ||
33 | + } | ||
34 | + | ||
35 | + public static String getStringFromRequestParamMap(Map<String, ?> data, String key){ | ||
36 | + if(data == null){ | ||
37 | + return null; | ||
38 | + } | ||
39 | + Object value = data.get(key); | ||
40 | + if(value == null){ | ||
41 | + return null; | ||
42 | + }else{ | ||
43 | + if(value.getClass().isArray()){ | ||
44 | + return String.valueOf(Array.get(value, 0)); | ||
45 | + }else{ | ||
46 | + return String.valueOf(value); | ||
47 | + } | ||
48 | + } | ||
49 | + } | ||
50 | + | ||
51 | + public static void main(String args[]){ | ||
52 | + List<TestColl> data = new ArrayList<TestColl>(); | ||
53 | + data.add(new TestColl("1","2")); | ||
54 | + data.add(new TestColl("3","4")); | ||
55 | + data.add(new TestColl("5","6")); | ||
56 | + Map temp = UtilCollection.listToMap(data, "a"); | ||
57 | + System.out.println(temp.get("1")); | ||
58 | + } | ||
59 | +} | ||
60 | + | ||
61 | +class TestColl { | ||
62 | + String a; | ||
63 | + String b; | ||
64 | + public TestColl(String a, String b){ | ||
65 | + this.a = a; | ||
66 | + this.b = b; | ||
67 | + } | ||
68 | +} |
@@ -0,0 +1,59 @@ | @@ -0,0 +1,59 @@ | ||
1 | +package com.taover.util; | ||
2 | + | ||
3 | +import java.text.DecimalFormat; | ||
4 | +import java.text.SimpleDateFormat; | ||
5 | +import java.util.Calendar; | ||
6 | +import java.util.Date; | ||
7 | + | ||
8 | +public class UtilDate { | ||
9 | + public static String getWeekCnByIndex(int index){ | ||
10 | + String weekDay[] = new String[]{"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"}; | ||
11 | + return weekDay[index]; | ||
12 | + } | ||
13 | + | ||
14 | + /** | ||
15 | + * 计算周开始时间,gap=0表示当前周,gap=-1表示上一周,依次类推 | ||
16 | + * @param gap | ||
17 | + * @return | ||
18 | + */ | ||
19 | + public static Date getDateWeekBegin(int gap){ | ||
20 | + Date result = new Date(); | ||
21 | + Calendar currCal = Calendar.getInstance(); | ||
22 | + currCal.setWeekDate(currCal.getWeekYear(), currCal.get(Calendar.WEEK_OF_YEAR)+gap, Calendar.MONDAY); | ||
23 | + result = currCal.getTime(); | ||
24 | + return result; | ||
25 | + } | ||
26 | + | ||
27 | + /** | ||
28 | + * 计算周开始时间,gap=0表示当前周,gap=-1表示上一周,依次类推 | ||
29 | + * @param gap | ||
30 | + * @return | ||
31 | + */ | ||
32 | + public static Date getDateWeekSome(int gap, int someWeekDay){ | ||
33 | + Date result = new Date(); | ||
34 | + Calendar currCal = Calendar.getInstance(); | ||
35 | + currCal.setWeekDate(currCal.getWeekYear(), currCal.get(Calendar.WEEK_OF_YEAR)+gap, someWeekDay); | ||
36 | + result = currCal.getTime(); | ||
37 | + return result; | ||
38 | + } | ||
39 | + | ||
40 | + /** | ||
41 | + * 计算月开始时间,gap=0表示当前月,gap=-1表示上一月,依次类推 | ||
42 | + * @param gap | ||
43 | + * @return | ||
44 | + */ | ||
45 | + public static Date getDateMonthBegin(int gap){ | ||
46 | + Date result = new Date(); | ||
47 | + Calendar currCal = Calendar.getInstance(); | ||
48 | + currCal.set(currCal.get(Calendar.YEAR), currCal.get(Calendar.MONTH)+gap, 1); | ||
49 | + result = currCal.getTime(); | ||
50 | + return result; | ||
51 | + } | ||
52 | + | ||
53 | + public static void main(String args[]){ | ||
54 | + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); | ||
55 | + //System.out.println(sdf.format(getDateMonthBegin(-1))); | ||
56 | + DecimalFormat dFormat=new DecimalFormat("#.00"); | ||
57 | + System.out.println(dFormat.format((Double.valueOf("2.123123123")/123.212*100))); | ||
58 | + } | ||
59 | +} |
@@ -0,0 +1,56 @@ | @@ -0,0 +1,56 @@ | ||
1 | +package com.taover.util; | ||
2 | + | ||
3 | +import java.security.MessageDigest; | ||
4 | +import java.security.NoSuchAlgorithmException; | ||
5 | + | ||
6 | +import javax.crypto.Mac; | ||
7 | +import javax.crypto.spec.SecretKeySpec; | ||
8 | + | ||
9 | +public class UtilEncrypt { | ||
10 | + | ||
11 | + public static String MD5Lower32(String sourceStr) { | ||
12 | + String result = ""; | ||
13 | + try { | ||
14 | + MessageDigest md = MessageDigest.getInstance("MD5"); | ||
15 | + md.update(sourceStr.getBytes()); | ||
16 | + byte b[] = md.digest(); | ||
17 | + int i; | ||
18 | + StringBuffer buf = new StringBuffer(""); | ||
19 | + for (int offset = 0; offset < b.length; offset++) { | ||
20 | + i = b[offset]; | ||
21 | + if (i < 0) | ||
22 | + i += 256; | ||
23 | + if (i < 16) | ||
24 | + buf.append("0"); | ||
25 | + buf.append(Integer.toHexString(i)); | ||
26 | + } | ||
27 | + result = buf.toString(); | ||
28 | + } catch (NoSuchAlgorithmException e) { | ||
29 | + System.out.println(e); | ||
30 | + } | ||
31 | + return result; | ||
32 | + } | ||
33 | + | ||
34 | + private static final String HMAC_SHA1_ALGORITHM = "HmacSHA1"; | ||
35 | + /** | ||
36 | + * hmac sha1 | ||
37 | + * @param src | ||
38 | + * @param key | ||
39 | + * @return | ||
40 | + */ | ||
41 | + public static byte[] hmacSha1(byte[] key, byte[] src) { | ||
42 | + try { | ||
43 | + SecretKeySpec signingKey = new SecretKeySpec(key, HMAC_SHA1_ALGORITHM); | ||
44 | + Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM); | ||
45 | + mac.init(signingKey); | ||
46 | + return mac.doFinal(src); | ||
47 | + } catch (Exception e) { | ||
48 | + throw new RuntimeException(e); | ||
49 | + } | ||
50 | + } | ||
51 | + | ||
52 | + public static void main(String[] args) { | ||
53 | + System.out.println(MD5Lower32("Lexi@1799")+"6591"); | ||
54 | + System.out.println(MD5Lower32("adf8d8f44072dfae10ca8d11d060bf406591")); | ||
55 | + } | ||
56 | +} |
@@ -0,0 +1,324 @@ | @@ -0,0 +1,324 @@ | ||
1 | +package com.taover.util; | ||
2 | + | ||
3 | +import java.io.File; | ||
4 | +import java.io.FileInputStream; | ||
5 | +import java.io.FileOutputStream; | ||
6 | +import java.text.DecimalFormat; | ||
7 | +import java.util.ArrayList; | ||
8 | +import java.util.Date; | ||
9 | +import java.util.HashMap; | ||
10 | +import java.util.List; | ||
11 | +import java.util.Map; | ||
12 | + | ||
13 | +import org.apache.commons.logging.Log; | ||
14 | +import org.apache.commons.logging.LogFactory; | ||
15 | +import org.apache.poi.hssf.usermodel.HSSFCell; | ||
16 | +import org.apache.poi.hssf.usermodel.HSSFWorkbook; | ||
17 | +import org.apache.poi.hssf.util.HSSFColor; | ||
18 | +import org.apache.poi.ss.usermodel.Cell; | ||
19 | +import org.apache.poi.ss.usermodel.CellStyle; | ||
20 | +import org.apache.poi.ss.usermodel.Row; | ||
21 | +import org.apache.poi.ss.usermodel.Sheet; | ||
22 | +import org.apache.poi.ss.usermodel.Workbook; | ||
23 | +import org.apache.poi.xssf.usermodel.XSSFWorkbook; | ||
24 | + | ||
25 | +public class UtilExcel { | ||
26 | + private final static String excel2003L =".xls"; //2003- 版本的excel | ||
27 | + private final static String excel2007U =".xlsx"; //2007+ 版本的excel | ||
28 | + | ||
29 | + private final static int maxExcelRowNum = 5000; | ||
30 | + private final static int maxExcelColumnNum = 50; | ||
31 | + | ||
32 | + /** | ||
33 | + * 描述:根据文件后缀,自适应上传文件的版本 | ||
34 | + * @param inStr,fileName | ||
35 | + * @return | ||
36 | + * @throws Exception | ||
37 | + */ | ||
38 | + private static Workbook getWorkbook(String filePath, boolean isRead) throws Exception{ | ||
39 | + Workbook wb = null; | ||
40 | + if(isRead){ | ||
41 | + File tempFile = new File(filePath); | ||
42 | + if(!tempFile.exists()){ | ||
43 | + throw new Exception("需要读取的文件不存在!"); | ||
44 | + } | ||
45 | + } | ||
46 | + String fileType = filePath.substring(filePath.lastIndexOf(".")); | ||
47 | + if(excel2003L.equals(fileType)){ | ||
48 | + if(isRead){ | ||
49 | + wb = new HSSFWorkbook(new FileInputStream(filePath)); //2003- | ||
50 | + }else{ | ||
51 | + wb = new HSSFWorkbook(); //2003- | ||
52 | + } | ||
53 | + }else if(excel2007U.equals(fileType)){ | ||
54 | + if(isRead){ | ||
55 | + wb = new XSSFWorkbook(new FileInputStream(filePath)); //2007+ | ||
56 | + }else{ | ||
57 | + wb = new XSSFWorkbook(); //2007+ | ||
58 | + } | ||
59 | + }else{ | ||
60 | + throw new Exception("解析的文件格式有误!"); | ||
61 | + } | ||
62 | + return wb; | ||
63 | + } | ||
64 | + | ||
65 | + /** | ||
66 | + * 创建并保存excel表 | ||
67 | + * @param sheetName | ||
68 | + * @param data | ||
69 | + * @param path | ||
70 | + */ | ||
71 | + public static File saveExcelFromListString(String sheetName, List<String[]> data, String path){ | ||
72 | + Log log = LogFactory.getLog(UtilExcel.class); | ||
73 | + Workbook wb = null; | ||
74 | + try { | ||
75 | + wb = UtilExcel.getWorkbook(path, false); | ||
76 | + } catch (Exception e1) { | ||
77 | + e1.printStackTrace(); | ||
78 | + log.error(e1.getStackTrace().toString()+"保存excel文件失败,失败原因不能获取Workbook对象!msg="+e1.getMessage()); | ||
79 | + return null; | ||
80 | + } | ||
81 | + //创建Excel工作簿对象 | ||
82 | + Sheet sheet = wb.createSheet(sheetName);//创建Excel工作表对象 | ||
83 | + for(int i=0; i<data.size(); ++i){ | ||
84 | + Row row = sheet.createRow(i); //创建Excel工作表的行 | ||
85 | + String[] dataRow = data.get(i); | ||
86 | + if(dataRow != null){ | ||
87 | + for(int j=0; j<dataRow.length; ++j){ | ||
88 | + Cell cell = row.createCell(j); | ||
89 | + String dataCell = dataRow[j]; | ||
90 | + if(dataCell != null){ | ||
91 | + cell.setCellValue(dataCell); | ||
92 | + } | ||
93 | + } | ||
94 | + } | ||
95 | + } | ||
96 | + try { | ||
97 | + FileOutputStream fileOut; | ||
98 | + File tempFile = new File(path); | ||
99 | + if(!tempFile.exists()){ | ||
100 | + File parentFile = tempFile.getParentFile(); | ||
101 | + if(!parentFile.exists()){ | ||
102 | + parentFile.mkdirs(); | ||
103 | + } | ||
104 | + if(!tempFile.createNewFile()){ | ||
105 | + return null; | ||
106 | + } | ||
107 | + } | ||
108 | + fileOut = new FileOutputStream(tempFile); | ||
109 | + wb.write(fileOut); | ||
110 | + wb.close(); | ||
111 | + fileOut.close(); | ||
112 | + | ||
113 | + return tempFile; | ||
114 | + } catch (Exception e) { | ||
115 | + // TODO Auto-generated catch block | ||
116 | + e.printStackTrace(); | ||
117 | + } | ||
118 | + return null; | ||
119 | + } | ||
120 | + | ||
121 | + /** | ||
122 | + * 创建并保存excel表 | ||
123 | + * @param sheetName | ||
124 | + * @param data | ||
125 | + * @param path | ||
126 | + */ | ||
127 | + public static void saveExcel(String sheetName, List<List<Object>> data, String path){ | ||
128 | + Log log =LogFactory.getLog(UtilExcel.class); | ||
129 | + Workbook wb = null; | ||
130 | + try { | ||
131 | + wb = UtilExcel.getWorkbook(path, false); | ||
132 | + } catch (Exception e1) { | ||
133 | + e1.printStackTrace(); | ||
134 | + log.error(e1.getStackTrace().toString()+"保存excel文件失败,失败原因不能获取Workbook对象!msg="+e1.getMessage()); | ||
135 | + return; | ||
136 | + } | ||
137 | + //创建Excel工作簿对象 | ||
138 | + Sheet sheet = wb.createSheet(sheetName);//创建Excel工作表对象 | ||
139 | + for(int i=0; i<data.size(); ++i){ | ||
140 | + Row row = sheet.createRow(i); //创建Excel工作表的行 | ||
141 | + List<Object> dataRow = data.get(i); | ||
142 | + if(dataRow != null){ | ||
143 | + for(int j=0; j<dataRow.size(); ++j){ | ||
144 | + Cell cell = row.createCell(j); | ||
145 | + Object dataCell = dataRow.get(j); | ||
146 | + if(dataCell != null){ | ||
147 | + if(dataCell.getClass().isPrimitive()){ | ||
148 | + cell.setCellValue(Double.valueOf(dataCell.toString())); | ||
149 | + }else if(dataCell.getClass().getSimpleName().equals("Date")){ | ||
150 | + cell.setCellValue((Date)dataCell); | ||
151 | + }else{ | ||
152 | + cell.setCellValue(dataCell.toString()); | ||
153 | + } | ||
154 | + } | ||
155 | + } | ||
156 | + } | ||
157 | + } | ||
158 | + try { | ||
159 | + FileOutputStream fileOut; | ||
160 | + File tempFile = new File(path); | ||
161 | + if(!tempFile.exists()){ | ||
162 | + File parentFile = tempFile.getParentFile(); | ||
163 | + if(!parentFile.exists()){ | ||
164 | + parentFile.mkdirs(); | ||
165 | + } | ||
166 | + if(!tempFile.createNewFile()){ | ||
167 | + return; | ||
168 | + } | ||
169 | + } | ||
170 | + fileOut = new FileOutputStream(tempFile); | ||
171 | + wb.write(fileOut); | ||
172 | + wb.close(); | ||
173 | + fileOut.close(); | ||
174 | + } catch (Exception e) { | ||
175 | + // TODO Auto-generated catch block | ||
176 | + e.printStackTrace(); | ||
177 | + } | ||
178 | + } | ||
179 | + | ||
180 | + /** | ||
181 | + * 创建并保存excel表 | ||
182 | + * @param sheetName | ||
183 | + * @param data | ||
184 | + * @param path | ||
185 | + */ | ||
186 | + public static void saveExcel(String sheetName, List<List<Object>> data, String path, List<Short> backColorList){ | ||
187 | + Log log =LogFactory.getLog(UtilExcel.class); | ||
188 | + Workbook wb = null; | ||
189 | + try { | ||
190 | + wb = UtilExcel.getWorkbook(path, false); | ||
191 | + } catch (Exception e1) { | ||
192 | + e1.printStackTrace(); | ||
193 | + log.error(e1.getStackTrace().toString()+"保存excel文件失败,失败原因不能获取Workbook对象!msg="+e1.getMessage()); | ||
194 | + return; | ||
195 | + } | ||
196 | + Map<Short, CellStyle> cellStyleMap = new HashMap<Short, CellStyle>(); | ||
197 | + Sheet sheet = wb.createSheet(sheetName);//创建Excel工作表对象 | ||
198 | + for(int i=0; i<data.size(); ++i){ | ||
199 | + Row row = sheet.createRow(i); //创建Excel工作表的行 | ||
200 | + List<Object> dataRow = data.get(i); | ||
201 | + Short backColor = backColorList.get(i); | ||
202 | + CellStyle style = cellStyleMap.get(backColor); | ||
203 | + if(style == null){ | ||
204 | + style = wb.createCellStyle(); | ||
205 | + style.setFillPattern(style.SOLID_FOREGROUND); | ||
206 | + cellStyleMap.put(backColor, style); | ||
207 | + } | ||
208 | + if(backColor != null){ | ||
209 | + style.setFillForegroundColor(backColor.shortValue()); | ||
210 | + } | ||
211 | + if(dataRow != null){ | ||
212 | + for(int j=0; j<dataRow.size(); ++j){ | ||
213 | + Cell cell = row.createCell(j); | ||
214 | + Object dataCell = dataRow.get(j); | ||
215 | + if(dataCell != null){ | ||
216 | + if(dataCell.getClass().isPrimitive()){ | ||
217 | + cell.setCellValue(Double.valueOf(dataCell.toString())); | ||
218 | + }else if(dataCell.getClass().getSimpleName().equals("Date")){ | ||
219 | + cell.setCellValue((Date)dataCell); | ||
220 | + }else{ | ||
221 | + cell.setCellValue(dataCell.toString()); | ||
222 | + } | ||
223 | + } | ||
224 | + if(backColor != null){ | ||
225 | + cell.setCellStyle(style); | ||
226 | + } | ||
227 | + } | ||
228 | + } | ||
229 | + } | ||
230 | + try { | ||
231 | + FileOutputStream fileOut; | ||
232 | + File tempFile = new File(path); | ||
233 | + if(!tempFile.exists()){ | ||
234 | + File parentFile = tempFile.getParentFile(); | ||
235 | + if(!parentFile.exists()){ | ||
236 | + parentFile.mkdirs(); | ||
237 | + } | ||
238 | + if(!tempFile.createNewFile()){ | ||
239 | + return; | ||
240 | + } | ||
241 | + } | ||
242 | + fileOut = new FileOutputStream(tempFile); | ||
243 | + wb.write(fileOut); | ||
244 | + wb.close(); | ||
245 | + fileOut.close(); | ||
246 | + } catch (Exception e) { | ||
247 | + // TODO Auto-generated catch block | ||
248 | + e.printStackTrace(); | ||
249 | + } | ||
250 | + } | ||
251 | + | ||
252 | + /** | ||
253 | + * 读取excel表 | ||
254 | + * @param path | ||
255 | + */ | ||
256 | + public static List<List<Object>> readExcel(String filepath){ | ||
257 | + File file = new File(filepath); | ||
258 | + List<List<Object>> result = new ArrayList<List<Object>>(); | ||
259 | + if(!file.exists()){ | ||
260 | + return result; | ||
261 | + } | ||
262 | + Log log = LogFactory.getLog(UtilExcel.class); | ||
263 | + Workbook wb = null; | ||
264 | + try { | ||
265 | + wb = UtilExcel.getWorkbook(filepath, true); | ||
266 | + } catch (Exception e1) { | ||
267 | + e1.printStackTrace(); | ||
268 | + log.error(e1.getStackTrace().toString()+"保存excel文件失败,失败原因不能获取Workbook对象!msg="+e1.getMessage()); | ||
269 | + return new ArrayList<List<Object>>(); | ||
270 | + } | ||
271 | + //创建Excel工作簿对象 | ||
272 | + DecimalFormat df = new DecimalFormat("0"); | ||
273 | + Sheet sheet = wb.getSheetAt(wb.getActiveSheetIndex()); | ||
274 | + int start = sheet.getFirstRowNum(); | ||
275 | + int end = sheet.getLastRowNum(); | ||
276 | + if(end > UtilExcel.maxExcelRowNum){ | ||
277 | + end = UtilExcel.maxExcelRowNum; | ||
278 | + } | ||
279 | + for(int i=start; i<end+1; ++i){ | ||
280 | + Row row = sheet.getRow(i); | ||
281 | + if(row == null){ | ||
282 | + continue; | ||
283 | + } | ||
284 | + List<Object> dataRow = new ArrayList<Object>(); | ||
285 | + int lastCellNum = row.getLastCellNum(); | ||
286 | + if(lastCellNum > UtilExcel.maxExcelColumnNum){ | ||
287 | + lastCellNum = UtilExcel.maxExcelColumnNum; | ||
288 | + } | ||
289 | + for(int j=0; j<lastCellNum; ++j){ | ||
290 | + Cell cell = row.getCell(j); | ||
291 | + if(cell != null){ | ||
292 | + if(cell.getCellType() == HSSFCell.CELL_TYPE_STRING){ | ||
293 | + dataRow.add(cell.getStringCellValue()); | ||
294 | + }else if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){ | ||
295 | + dataRow.add(df.format(cell.getNumericCellValue())); | ||
296 | + }else if(cell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN){ | ||
297 | + dataRow.add(Boolean.valueOf(cell.getBooleanCellValue())); | ||
298 | + }else{ | ||
299 | + dataRow.add(cell.getStringCellValue()); | ||
300 | + } | ||
301 | + }else{ | ||
302 | + dataRow.add(""); | ||
303 | + } | ||
304 | + } | ||
305 | + result.add(dataRow); | ||
306 | + } | ||
307 | + return result; | ||
308 | + } | ||
309 | + | ||
310 | + public static void main(String args[]){ | ||
311 | + String filepath = "C:\\Users\\root\\Desktop\\千丁-6.27.xlsx"; | ||
312 | + List<List<Object>> data = UtilExcel.readExcel(filepath); | ||
313 | + List<Short> styleList = new ArrayList<Short>(); | ||
314 | + for(int i=0; i<data.size(); ++i){ | ||
315 | + if(i == 1)styleList.add(Short.valueOf(HSSFColor.RED.index)); | ||
316 | + else styleList.add(null); | ||
317 | + for(int j=0; j<data.get(i).size(); ++j){ | ||
318 | + System.out.print(data.get(i).get(j).toString()+" : "); | ||
319 | + } | ||
320 | + System.out.println(""); | ||
321 | + } | ||
322 | + UtilExcel.saveExcel("测试", data, "D:\\12345.xlsx", styleList); | ||
323 | + } | ||
324 | +} |
@@ -0,0 +1,189 @@ | @@ -0,0 +1,189 @@ | ||
1 | +package com.taover.util; | ||
2 | + | ||
3 | +import java.io.UnsupportedEncodingException; | ||
4 | +import java.net.URLEncoder; | ||
5 | +import java.nio.charset.Charset; | ||
6 | +import java.security.KeyManagementException; | ||
7 | +import java.security.NoSuchAlgorithmException; | ||
8 | +import java.security.cert.X509Certificate; | ||
9 | +import java.util.HashMap; | ||
10 | +import java.util.Map; | ||
11 | +import java.util.concurrent.TimeUnit; | ||
12 | + | ||
13 | +import javax.net.ssl.HostnameVerifier; | ||
14 | +import javax.net.ssl.SSLContext; | ||
15 | +import javax.net.ssl.SSLSession; | ||
16 | +import javax.net.ssl.SSLSocketFactory; | ||
17 | +import javax.net.ssl.TrustManager; | ||
18 | +import javax.net.ssl.X509TrustManager; | ||
19 | + | ||
20 | +import okhttp3.FormBody; | ||
21 | +import okhttp3.MediaType; | ||
22 | +import okhttp3.OkHttpClient; | ||
23 | +import okhttp3.Request; | ||
24 | +import okhttp3.RequestBody; | ||
25 | + | ||
26 | +public class UtilHttpByOkHttp { | ||
27 | + public static final String METHOD_GET = "GET"; | ||
28 | + public static final String METHOD_POST = "POST"; | ||
29 | + public static final String METHOD_DELETE = "DELETE"; | ||
30 | + public static final String METHOD_PUT = "PUT"; | ||
31 | + | ||
32 | + public static String sendGet(String url, final Map<String, String> headers) throws Exception { | ||
33 | + //http头信息拼装 | ||
34 | + Request request = getRequestBuilder(url, headers).get().build(); | ||
35 | + | ||
36 | + return getHttpClient(url).newCall(request).execute().body().string(); | ||
37 | + } | ||
38 | + | ||
39 | + public static String sendPostForm(String url, final Map<String, String> headers, final Map<String, Object> params) throws Exception { | ||
40 | + //请求体信息 | ||
41 | + RequestBody requestBody = getReqeustBody(params); | ||
42 | + | ||
43 | + //http头信息拼装 | ||
44 | + Request request = getRequestBuilder(url, headers).post(requestBody).build(); | ||
45 | + | ||
46 | + return getHttpClient(url).newCall(request).execute().body().string(); | ||
47 | + } | ||
48 | + | ||
49 | + public static String sendPutForm(String url, final Map<String, String> headers, final Map<String, Object> params) throws Exception { | ||
50 | + //请求体信息 | ||
51 | + RequestBody requestBody = getReqeustBody(params); | ||
52 | + | ||
53 | + //http头信息拼装 | ||
54 | + Request request = getRequestBuilder(url, headers).put(requestBody).build(); | ||
55 | + | ||
56 | + return getHttpClient(url).newCall(request).execute().body().string(); | ||
57 | + } | ||
58 | + | ||
59 | + public static String sendPostString(String url, final Map<String, String> headers, final String content) throws Exception{ | ||
60 | + //请求体 | ||
61 | + RequestBody requestBody = getReqeustBody(content); | ||
62 | + | ||
63 | + //http头信息拼装 | ||
64 | + Request request = getRequestBuilder(url, headers).post(requestBody).build(); | ||
65 | + | ||
66 | + return getHttpClient(url).newCall(request).execute().body().string(); | ||
67 | + } | ||
68 | + | ||
69 | + public static String sendDelete(String url, final Map<String, String> headers) throws Exception { | ||
70 | + //http头信息拼装 | ||
71 | + Request request = getRequestBuilder(url, headers).delete().build(); | ||
72 | + | ||
73 | + return getHttpClient(url).newCall(request).execute().body().string(); | ||
74 | + } | ||
75 | + | ||
76 | + private static OkHttpClient getHttpClient(String url){ | ||
77 | + if(url.trim().toLowerCase().startsWith("https")){ | ||
78 | + return buildOkHttpClientForHttps(); | ||
79 | + }else{ | ||
80 | + return new OkHttpClient(); | ||
81 | + } | ||
82 | + } | ||
83 | + | ||
84 | + private static RequestBody getReqeustBody(Map<String, Object> params){ | ||
85 | + //表单信息拼装 | ||
86 | + FormBody.Builder builder = new FormBody.Builder(Charset.forName("UTF-8")); | ||
87 | + if(params != null) { | ||
88 | + for(Map.Entry<String, Object> entry: params.entrySet()) { | ||
89 | + Object tempValue = entry.getValue(); | ||
90 | + if(tempValue.getClass().isArray()){ | ||
91 | + String[] tempValueArr = (String[])tempValue; | ||
92 | + for(int i=0; i<tempValueArr.length; ++i){ | ||
93 | + builder.add(entry.getKey()+"[]", tempValueArr[i]); | ||
94 | + } | ||
95 | + }else{ | ||
96 | + builder.add(entry.getKey(), entry.getValue().toString()); | ||
97 | + } | ||
98 | + } | ||
99 | + } | ||
100 | + return builder.build(); | ||
101 | + } | ||
102 | + | ||
103 | + private static RequestBody getReqeustBody(String content){ | ||
104 | + return RequestBody.create(MediaType.parse("text/plain;charse=utf-8"), content); | ||
105 | + } | ||
106 | + | ||
107 | + private static Request.Builder getRequestBuilder(String url, Map<String, String> headers){ | ||
108 | + //http头信息拼装 | ||
109 | + Request.Builder requestBuilder = new Request.Builder().url(url); | ||
110 | + if(headers != null){ | ||
111 | + for(Map.Entry<String, String> entry: headers.entrySet()) { | ||
112 | + requestBuilder.addHeader(entry.getKey(), entry.getValue()); | ||
113 | + } | ||
114 | + } | ||
115 | + return requestBuilder; | ||
116 | + } | ||
117 | + | ||
118 | + private static OkHttpClient buildOkHttpClientForHttps() { | ||
119 | + OkHttpClient.Builder builder = new OkHttpClient.Builder(); | ||
120 | + builder.connectTimeout(30, TimeUnit.SECONDS) | ||
121 | + .readTimeout(30, TimeUnit.SECONDS) | ||
122 | + .writeTimeout(30,TimeUnit.SECONDS) | ||
123 | + .retryOnConnectionFailure(true) | ||
124 | + .sslSocketFactory(getTrustedSSLSocketFactory()) | ||
125 | + .hostnameVerifier(DO_NOT_VERIFY); | ||
126 | + return builder.build(); | ||
127 | + } | ||
128 | + | ||
129 | + static TrustManager[] trustAllCerts = new TrustManager[]{ | ||
130 | + new X509TrustManager() { | ||
131 | + public java.security.cert.X509Certificate[] getAcceptedIssuers() { | ||
132 | + X509Certificate[] x509Certificates = new X509Certificate[0]; | ||
133 | + return x509Certificates; | ||
134 | + } | ||
135 | + | ||
136 | + public void checkClientTrusted( | ||
137 | + java.security.cert.X509Certificate[] certs, String authType) { | ||
138 | + } | ||
139 | + | ||
140 | + public void checkServerTrusted( | ||
141 | + java.security.cert.X509Certificate[] certs, String authType) { | ||
142 | + } | ||
143 | + } | ||
144 | + }; | ||
145 | + | ||
146 | + static HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier() { | ||
147 | + @Override | ||
148 | + public boolean verify(String hostname, SSLSession session) { | ||
149 | + return true; | ||
150 | + } | ||
151 | + }; | ||
152 | + | ||
153 | + private static SSLSocketFactory getTrustedSSLSocketFactory() { | ||
154 | + try { | ||
155 | + SSLContext sc = SSLContext.getInstance("SSL"); | ||
156 | + sc.init(null, trustAllCerts, new java.security.SecureRandom()); | ||
157 | + return sc.getSocketFactory(); | ||
158 | + } catch (KeyManagementException | NoSuchAlgorithmException e) { | ||
159 | + e.printStackTrace(); | ||
160 | + return null; | ||
161 | + } | ||
162 | + } | ||
163 | + | ||
164 | + public static void main(String args[]){ | ||
165 | + String contactSsid = "lM4AeZSizgFROR/OAHmUos4BUTkf"; | ||
166 | + try { | ||
167 | + contactSsid = URLEncoder.encode(contactSsid, "UTF-8"); | ||
168 | + } catch (UnsupportedEncodingException e1) { | ||
169 | + e1.printStackTrace(); | ||
170 | + } | ||
171 | + String url = "https://ssep.umsapi.com/api/v1/contacts/"+contactSsid+"/messages"; | ||
172 | + System.out.println(url); | ||
173 | + | ||
174 | + Map<String, String> headerData = new HashMap<String, String>(); | ||
175 | + headerData.put("ss-token", "lsQUPZA2rxyckfmOD5gfnPAo9Qa6N1/EEESotlOah3t2392ZZkrhOofNAZF5zlyUrDS1Y29tLnNvbmdzaHUud2ViYXBwLnYx"); | ||
176 | + | ||
177 | + Map<String, Object> paramData = new HashMap<String, Object>(); | ||
178 | + paramData.put("kind", "text"); | ||
179 | + paramData.put("content", "小伙子 干得漂亮"); | ||
180 | + | ||
181 | + try { | ||
182 | + //System.out.println(UtilHttpByOkHttp.sendPostForm(url, headerData, paramData)); | ||
183 | + System.out.println(UtilHttpByOkHttp.sendGet("https://ssep.umsapi.com/api/v2/openapi/querycontacts?appid=songshuyun-ifun×tamp=1562239191&signature=967fd77a24cbbce872bc1506a0187983", null)); | ||
184 | + } catch (Exception e) { | ||
185 | + // TODO Auto-generated catch block | ||
186 | + e.printStackTrace(); | ||
187 | + } | ||
188 | + } | ||
189 | +} |
@@ -0,0 +1,74 @@ | @@ -0,0 +1,74 @@ | ||
1 | +package com.taover.util; | ||
2 | + | ||
3 | +import java.io.PrintWriter; | ||
4 | +import java.io.StringWriter; | ||
5 | +import java.util.HashMap; | ||
6 | +import java.util.Map; | ||
7 | + | ||
8 | +import org.apache.commons.logging.Log; | ||
9 | +import org.apache.commons.logging.LogFactory; | ||
10 | + | ||
11 | + | ||
12 | +public class UtilLog { | ||
13 | + private static Map<String, Log> logByClassName = new HashMap<String, Log>(); | ||
14 | + | ||
15 | + /** | ||
16 | + * 输出日志文本 | ||
17 | + * @param message | ||
18 | + * @param infoClass | ||
19 | + */ | ||
20 | + public static void infoForMessage(String message, Class infoClass){ | ||
21 | + String className = infoClass.getName(); | ||
22 | + Log log = logByClassName.get(infoClass.getName()); | ||
23 | + if(log == null){ | ||
24 | + log = LogFactory.getLog(infoClass); | ||
25 | + if(log == null){ | ||
26 | + return; | ||
27 | + } | ||
28 | + logByClassName.put(className, log); | ||
29 | + } | ||
30 | + log.info(message); | ||
31 | + } | ||
32 | + | ||
33 | + /** | ||
34 | + * 输出日志Exception内容 | ||
35 | + * @param e | ||
36 | + * @param infoClass | ||
37 | + */ | ||
38 | + public static String infoForException(Exception e, Class infoClass){ | ||
39 | + StringWriter sw = new StringWriter(); | ||
40 | + e.printStackTrace(new PrintWriter(sw)); | ||
41 | + UtilLog.infoForMessage(sw.toString(), infoClass); | ||
42 | + return sw.toString(); | ||
43 | + } | ||
44 | + | ||
45 | + /** | ||
46 | + * 输出文本内容 | ||
47 | + * @param message | ||
48 | + * @param infoClass | ||
49 | + */ | ||
50 | + public static void errorForMessage(String message, Class infoClass){ | ||
51 | + String className = infoClass.getName(); | ||
52 | + Log log = logByClassName.get(infoClass.getName()); | ||
53 | + if(log == null){ | ||
54 | + log = LogFactory.getLog(infoClass); | ||
55 | + if(log == null){ | ||
56 | + return; | ||
57 | + } | ||
58 | + logByClassName.put(className, log); | ||
59 | + } | ||
60 | + log.error(message); | ||
61 | + } | ||
62 | + | ||
63 | + /** | ||
64 | + * 输出日志Exception信息 | ||
65 | + * @param e | ||
66 | + * @param infoClass | ||
67 | + */ | ||
68 | + public static String errorForException(Exception e, Class infoClass){ | ||
69 | + StringWriter sw = new StringWriter(); | ||
70 | + e.printStackTrace(new PrintWriter(sw)); | ||
71 | + UtilLog.errorForMessage(sw.toString(), infoClass); | ||
72 | + return sw.toString(); | ||
73 | + } | ||
74 | +} |
@@ -0,0 +1,245 @@ | @@ -0,0 +1,245 @@ | ||
1 | +package com.taover.util; | ||
2 | + | ||
3 | +import java.lang.reflect.Field; | ||
4 | +import java.util.ArrayList; | ||
5 | +import java.util.List; | ||
6 | + | ||
7 | +public class UtilObject { | ||
8 | + /** | ||
9 | + * 将源对象source字段赋值到对象dest字段,条件1字段名相同,条件2字段类型相同; | ||
10 | + * 注:source与dest可以是两个没有关系的类,不接受基本封装类型,例如Integer | ||
11 | + * @param source | ||
12 | + * @param dest | ||
13 | + * @param allowNull | ||
14 | + * @param exceptFieldNameArray | ||
15 | + * @return 如果source==dest或者source==null或者dest==null,则返回false | ||
16 | + */ | ||
17 | + public static Object fieldCopy(Object source, Object dest, boolean allowNull, String exceptFieldNameArray[], boolean isNewInstance){ | ||
18 | + //引用检查 | ||
19 | + if(source==null || dest==null || source==dest){ | ||
20 | + return null; | ||
21 | + } | ||
22 | + | ||
23 | + //获取Field数组 | ||
24 | + Field[] sourceField = source.getClass().getDeclaredFields(); | ||
25 | + Field[] destField = dest.getClass().getDeclaredFields(); | ||
26 | + | ||
27 | + //获取目标对象 | ||
28 | + String[] destFieldNameArray = new String[destField.length]; | ||
29 | + String[] destFieldTypeArray = new String[destField.length]; | ||
30 | + for(int i=0; i<destField.length; ++i){ | ||
31 | + destFieldNameArray[i] = destField[i].getName(); | ||
32 | + destFieldTypeArray[i] = destField[i].getType().getName(); | ||
33 | + } | ||
34 | + | ||
35 | + Object newDest = null; | ||
36 | + //注入属性 | ||
37 | + if(isNewInstance){ | ||
38 | + try { | ||
39 | + newDest = dest.getClass().newInstance(); | ||
40 | + for(int i=0; i<destField.length; ++i){ | ||
41 | + destField[i].setAccessible(true); | ||
42 | + Object tempValue = destField[i].get(dest); | ||
43 | + destField[i].set(newDest, tempValue); | ||
44 | + } | ||
45 | + } catch (Exception e2) { | ||
46 | + // TODO Auto-generated catch block | ||
47 | + e2.printStackTrace(); | ||
48 | + } | ||
49 | + } | ||
50 | + | ||
51 | + //遍历源field数组,处理源field数组 | ||
52 | + for(int i=0; i<sourceField.length; ++i){ | ||
53 | + //获取源field相关信息 | ||
54 | + Object sourceFieldValue = null; | ||
55 | + String sourceFieldName = sourceField[i].getName(); | ||
56 | + String sourceFieldType = sourceField[i].getType().getName(); | ||
57 | + | ||
58 | + //获取sourceFieldValue值 | ||
59 | + sourceField[i].setAccessible(true); | ||
60 | + try { | ||
61 | + sourceFieldValue = sourceField[i].get(source); | ||
62 | + } catch (Exception e1) { | ||
63 | + // TODO Auto-generated catch block | ||
64 | + e1.printStackTrace(); | ||
65 | + } | ||
66 | + | ||
67 | + //检查是否在exceptFieldName中 | ||
68 | + if(-1 != UtilString.getStringIndex(sourceFieldName, exceptFieldNameArray)){ | ||
69 | + continue; | ||
70 | + } | ||
71 | + | ||
72 | + //检验sourceFieldValue是否为NULL | ||
73 | + if(!allowNull && sourceFieldValue == null){ | ||
74 | + continue; | ||
75 | + } | ||
76 | + | ||
77 | + //检查源field字段名是否在目标field组中存在 | ||
78 | + int nameIndex = UtilString.getStringIndex(sourceFieldName, destFieldNameArray); | ||
79 | + if(nameIndex == -1){ | ||
80 | + continue; | ||
81 | + }else{ | ||
82 | + //如果源field与目标field数据类型相同,则将sourceFieldValue赋值给目标对象 | ||
83 | + if(sourceFieldType.equals(destFieldTypeArray[nameIndex])){ | ||
84 | + destField[nameIndex].setAccessible(true); | ||
85 | + try { | ||
86 | + destField[nameIndex].set(dest, sourceFieldValue); | ||
87 | + if(isNewInstance){ | ||
88 | + destField[nameIndex].set(newDest, sourceFieldValue); | ||
89 | + } | ||
90 | + } catch (Exception e) { | ||
91 | + // TODO Auto-generated catch block | ||
92 | + e.printStackTrace(); | ||
93 | + } | ||
94 | + } | ||
95 | + } | ||
96 | + } | ||
97 | + | ||
98 | + return newDest; | ||
99 | + } | ||
100 | + | ||
101 | + /** | ||
102 | + * 将对象的所有属性转换到list数组中以objce数组存储,数组长度为2,第一个为属性名,第二个为属性值,如果对象属性值为null,则跳过该属性 | ||
103 | + * 在字符串驼峰命名转化成下划线形式 | ||
104 | + * 如 helloWord -> hello_word | ||
105 | + * @param source | ||
106 | + * @param isCopyNull | ||
107 | + * @param exceptFieldNameArray | ||
108 | + * @return | ||
109 | + */ | ||
110 | + public static List<Object[]> objecFieldToListToLowerCase(Object source, boolean isCopyNull, String exceptFieldNameArray[]){ | ||
111 | + List<Object[]> result = new ArrayList<Object[]>(); | ||
112 | + //参数检验 | ||
113 | + if(source == null){ | ||
114 | + return result; | ||
115 | + } | ||
116 | + | ||
117 | + //获取Field数组 | ||
118 | + Field[] sourceField = source.getClass().getDeclaredFields(); | ||
119 | + //遍历源field数组,处理源field数组 | ||
120 | + for(int i=0; i<sourceField.length; ++i){ | ||
121 | + //获取源field相关信息 | ||
122 | + Object sourceFieldValue = null; | ||
123 | + String sourceFieldName = sourceField[i].getName(); | ||
124 | + | ||
125 | + //获取sourceFieldValue值 | ||
126 | + sourceField[i].setAccessible(true); | ||
127 | + try { | ||
128 | + sourceFieldValue = sourceField[i].get(source); | ||
129 | + } catch (Exception e1) { | ||
130 | + // TODO Auto-generated catch block | ||
131 | + e1.printStackTrace(); | ||
132 | + } | ||
133 | + | ||
134 | + //检查是否在exceptFieldName中 | ||
135 | + if(-1 != UtilString.getStringIndex(sourceFieldName, exceptFieldNameArray)){ | ||
136 | + continue; | ||
137 | + } | ||
138 | + | ||
139 | + //检验sourceFieldValue是否为NULL | ||
140 | + if(!isCopyNull && sourceFieldValue == null){ | ||
141 | + continue; | ||
142 | + } | ||
143 | + sourceFieldName = UtilString.underscoreName(sourceFieldName).toLowerCase(); | ||
144 | + //添加属性 | ||
145 | + result.add(new Object[]{sourceFieldName, sourceFieldValue}); | ||
146 | + } | ||
147 | + | ||
148 | + return result; | ||
149 | + } | ||
150 | + | ||
151 | + /** | ||
152 | + * 将对象的所有属性转换到list数组中以objce数组存储,数组长度为2,第一个为属性名,第二个为属性值,如果对象属性值为null,则跳过该属性 | ||
153 | + * @param source | ||
154 | + * @param allowNull | ||
155 | + * @param exceptFieldNameArray | ||
156 | + * @return | ||
157 | + */ | ||
158 | + public static List<Object[]> fieldToArray(Object source, boolean allowNull, String exceptFieldNameArray[]){ | ||
159 | + List<Object[]> result = new ArrayList<Object[]>(); | ||
160 | + //参数检验 | ||
161 | + if(source == null){ | ||
162 | + return result; | ||
163 | + } | ||
164 | + | ||
165 | + //获取Field数组 | ||
166 | + Field[] sourceField = source.getClass().getDeclaredFields(); | ||
167 | + //遍历源field数组,处理源field数组 | ||
168 | + for(int i=0; i<sourceField.length; ++i){ | ||
169 | + //获取源field相关信息 | ||
170 | + Object sourceFieldValue = null; | ||
171 | + String sourceFieldName = sourceField[i].getName(); | ||
172 | + | ||
173 | + //获取sourceFieldValue值 | ||
174 | + sourceField[i].setAccessible(true); | ||
175 | + try { | ||
176 | + sourceFieldValue = sourceField[i].get(source); | ||
177 | + } catch (Exception e1) { | ||
178 | + // TODO Auto-generated catch block | ||
179 | + e1.printStackTrace(); | ||
180 | + } | ||
181 | + | ||
182 | + //检查是否在exceptFieldName中 | ||
183 | + if(-1 != UtilString.getStringIndex(sourceFieldName, exceptFieldNameArray)){ | ||
184 | + continue; | ||
185 | + } | ||
186 | + | ||
187 | + //检验sourceFieldValue是否为NULL | ||
188 | + if(!allowNull && sourceFieldValue == null){ | ||
189 | + continue; | ||
190 | + } | ||
191 | + | ||
192 | + //添加属性 | ||
193 | + result.add(new Object[]{sourceFieldName, sourceFieldValue}); | ||
194 | + } | ||
195 | + | ||
196 | + return result; | ||
197 | + } | ||
198 | + | ||
199 | + public static void main(String args[]){ | ||
200 | + | ||
201 | + class Temp2{ | ||
202 | + Integer a; | ||
203 | + Integer c; | ||
204 | + public Temp2(Integer a, Integer c){ | ||
205 | + this.a = a; | ||
206 | + this.c = c; | ||
207 | + } | ||
208 | + @Override | ||
209 | + public String toString(){ | ||
210 | + return "a="+a+":c="+c; | ||
211 | + } | ||
212 | + } | ||
213 | + | ||
214 | + class Temp{ | ||
215 | + Integer a; | ||
216 | + Integer b; | ||
217 | + public Temp(Integer a, Integer b){ | ||
218 | + this.a = a; | ||
219 | + this.b = b; | ||
220 | + } | ||
221 | + @Override | ||
222 | + public String toString(){ | ||
223 | + return "a="+a+":b="+b; | ||
224 | + } | ||
225 | + } | ||
226 | + | ||
227 | + Temp temp1 = new Temp(null,2); | ||
228 | + Temp temp2 = new Temp(3,4); | ||
229 | + Temp temp3 = new Temp(5,6); | ||
230 | + Temp2 temp4 = new Temp2(7,8); | ||
231 | + | ||
232 | + fieldCopy(temp1, temp2, false, null,false); | ||
233 | + System.out.println(temp1.toString()); | ||
234 | + System.out.println(temp2.toString()); | ||
235 | + | ||
236 | + fieldCopy(temp3, temp4, false, null,false); | ||
237 | + System.out.println(temp3.toString()); | ||
238 | + System.out.println(temp4.toString()); | ||
239 | + | ||
240 | + List<Object[]> temp = UtilObject.fieldToArray(temp1, false, new String[]{"b"}); | ||
241 | + for(int i=0; i<temp.size(); ++i){ | ||
242 | + System.out.println(temp.get(i)[0]+":"+temp.get(i)[1]); | ||
243 | + } | ||
244 | + } | ||
245 | +} | ||
0 | \ No newline at end of file | 246 | \ No newline at end of file |
@@ -0,0 +1,63 @@ | @@ -0,0 +1,63 @@ | ||
1 | +package com.taover.util; | ||
2 | + | ||
3 | +import net.sourceforge.pinyin4j.PinyinHelper; | ||
4 | +import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat; | ||
5 | +import net.sourceforge.pinyin4j.format.HanyuPinyinToneType; | ||
6 | +import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination; | ||
7 | + | ||
8 | +public class UtilPinyin { | ||
9 | + /** | ||
10 | + * 对单个字进行转换 | ||
11 | + * @param pinYinStr 需转换的汉字字符串 | ||
12 | + * @return 拼音字符串数组 | ||
13 | + */ | ||
14 | + public static String getCharPinYin(char pinYinStr){ | ||
15 | + String[] pinyin = null; | ||
16 | + try{ | ||
17 | + //执行转换 | ||
18 | + //拼音字符串数组 | ||
19 | + HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat(); | ||
20 | + /* | ||
21 | + * 设置需要转换的拼音格式 | ||
22 | + * 以天为例 | ||
23 | + * HanyuPinyinToneType.WITHOUT_TONE 转换为tian | ||
24 | + * HanyuPinyinToneType.WITH_TONE_MARK 转换为tian1 | ||
25 | + * HanyuPinyinVCharType.WITH_U_UNICODE 转换为tiān | ||
26 | + */ | ||
27 | + format.setToneType(HanyuPinyinToneType.WITHOUT_TONE); | ||
28 | + pinyin = PinyinHelper.toHanyuPinyinStringArray(pinYinStr, format); | ||
29 | + } catch (BadHanyuPinyinOutputFormatCombination e) { | ||
30 | + // TODO Auto-generated catch block | ||
31 | + e.printStackTrace(); | ||
32 | + } | ||
33 | + | ||
34 | + //pinyin4j规则,当转换的符串不是汉字,就返回null | ||
35 | + if(pinyin == null || pinyin.length<1){ | ||
36 | + return null; | ||
37 | + } | ||
38 | + | ||
39 | + //多音字会返回一个多音字拼音的数组,pinyiin4j并不能有效判断该字的读音 | ||
40 | + return pinyin[0]; | ||
41 | + } | ||
42 | + | ||
43 | + /** | ||
44 | + * 对单个字进行转换 | ||
45 | + * @param pinYinStr | ||
46 | + * @return | ||
47 | + */ | ||
48 | + public String getStringPinYin(String pinYinStr){ | ||
49 | + StringBuffer sb = new StringBuffer(); | ||
50 | + String tempStr = null; | ||
51 | + //循环字符串 | ||
52 | + for(int i = 0; i<pinYinStr.length(); i++){ | ||
53 | + tempStr = this.getCharPinYin(pinYinStr.charAt(i)); | ||
54 | + if(tempStr == null){ | ||
55 | + //非汉字直接拼接 | ||
56 | + sb.append(pinYinStr.charAt(i)); | ||
57 | + }else{ | ||
58 | + sb.append(tempStr); | ||
59 | + } | ||
60 | + } | ||
61 | + return sb.toString(); | ||
62 | + } | ||
63 | +} |
@@ -0,0 +1,162 @@ | @@ -0,0 +1,162 @@ | ||
1 | +package com.taover.util; | ||
2 | + | ||
3 | +import java.util.regex.Matcher; | ||
4 | +import java.util.regex.Pattern; | ||
5 | + | ||
6 | +/** | ||
7 | + * @project baidamei | ||
8 | + * @author cevencheng <cevencheng@gmail.com> www.zuidaima.com | ||
9 | + * @create 2012-11-15 下午4:54:42 | ||
10 | + */ | ||
11 | +public class UtilRegex { | ||
12 | + | ||
13 | + /** | ||
14 | + * 验证Email | ||
15 | + * @param email email地址,格式:zhangsan@zuidaima.com,zhangsan@xxx.com.cn,xxx代表邮件服务商 | ||
16 | + * @return 验证成功返回true,验证失败返回false | ||
17 | + */ | ||
18 | + public static boolean checkEmail(String email) { | ||
19 | + String regex = "[\\w!#$%&'*+/=?^_`{|}~-]+(?:\\.[\\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\\w](?:[\\w-]*[\\w])?\\.)+[\\w](?:[\\w-]*[\\w])?"; | ||
20 | + return Pattern.matches(regex, email); | ||
21 | + } | ||
22 | + | ||
23 | + /** | ||
24 | + * 验证身份证号码 | ||
25 | + * @param idCard 居民身份证号码15位或18位,最后一位可能是数字或字母 | ||
26 | + * @return 验证成功返回true,验证失败返回false | ||
27 | + */ | ||
28 | + public static boolean checkIdCard(String idCard) { | ||
29 | + String regex = "^(\\d{6})(\\d{4})(\\d{2})(\\d{2})(\\d{3})([0-9]|X)$"; | ||
30 | + return Pattern.matches(regex,idCard); | ||
31 | + } | ||
32 | + | ||
33 | + /** | ||
34 | + * 验证手机号码(支持国际格式,+86135xxxx...(中国内地),+00852137xxxx...(中国香港)) | ||
35 | + * @param mobile 移动、联通、电信运营商的号码段 | ||
36 | + *<p>移动的号段:134(0-8)、135、136、137、138、139、147(预计用于TD上网卡) | ||
37 | + *、150、151、152、157(TD专用)、158、159、187(未启用)、188(TD专用)</p> | ||
38 | + *<p>联通的号段:130、131、132、155、156(世界风专用)、185(未启用)、186(3g)</p> | ||
39 | + *<p>电信的号段:133、153、180(未启用)、189</p> | ||
40 | + * @return 验证成功返回true,验证失败返回false | ||
41 | + */ | ||
42 | + public static boolean checkMobile(String mobile) { | ||
43 | + String regex = "\\d{3}-\\d{8}|\\d{4}-\\{7,8}|\\d{11}"; | ||
44 | + return Pattern.matches(regex,mobile); | ||
45 | + } | ||
46 | + | ||
47 | + /** | ||
48 | + * 验证固定电话号码 | ||
49 | + * @param phone 电话号码,格式:国家(地区)电话代码 + 区号(城市代码) + 电话号码,如:+8602085588447 | ||
50 | + * <p><b>国家(地区) 代码 :</b>标识电话号码的国家(地区)的标准国家(地区)代码。它包含从 0 到 9 的一位或多位数字, | ||
51 | + * 数字之后是空格分隔的国家(地区)代码。</p> | ||
52 | + * <p><b>区号(城市代码):</b>这可能包含一个或多个从 0 到 9 的数字,地区或城市代码放在圆括号—— | ||
53 | + * 对不使用地区或城市代码的国家(地区),则省略该组件。</p> | ||
54 | + * <p><b>电话号码:</b>这包含从 0 到 9 的一个或多个数字 </p> | ||
55 | + * @return 验证成功返回true,验证失败返回false | ||
56 | + */ | ||
57 | + public static boolean checkPhone(String phone) { | ||
58 | + String regex = "(\\+\\d+)?(\\d{3,4}\\-?)?\\d{7,8}$"; | ||
59 | + return Pattern.matches(regex, phone); | ||
60 | + } | ||
61 | + | ||
62 | + /** | ||
63 | + * 验证整数(正整数和负整数) | ||
64 | + * @param digit 一位或多位0-9之间的整数 | ||
65 | + * @return 验证成功返回true,验证失败返回false | ||
66 | + */ | ||
67 | + public static boolean checkDigit(String digit) { | ||
68 | + String regex = "^-?[1-9]\\d*$"; | ||
69 | + return Pattern.matches(regex,digit); | ||
70 | + } | ||
71 | + | ||
72 | + /** | ||
73 | + * 验证整数和浮点数(正负整数和正负浮点数) | ||
74 | + * @param decimals 一位或多位0-9之间的浮点数,如:1.23,233.30 | ||
75 | + * @return 验证成功返回true,验证失败返回false | ||
76 | + */ | ||
77 | + public static boolean checkDecimals(String decimals) { | ||
78 | + String regex = "\\-?[1-9]\\d+(\\.\\d+)?"; | ||
79 | + return Pattern.matches(regex,decimals); | ||
80 | + } | ||
81 | + | ||
82 | + /** | ||
83 | + * 验证空白字符 | ||
84 | + * @param blankSpace 空白字符,包括:空格、\t、\n、\r、\f、\x0B | ||
85 | + * @return 验证成功返回true,验证失败返回false | ||
86 | + */ | ||
87 | + public static boolean checkBlankSpace(String blankSpace) { | ||
88 | + String regex = "\\s+"; | ||
89 | + return Pattern.matches(regex,blankSpace); | ||
90 | + } | ||
91 | + | ||
92 | + /** | ||
93 | + * 验证中文 | ||
94 | + * @param chinese 中文字符 | ||
95 | + * @return 验证成功返回true,验证失败返回false | ||
96 | + */ | ||
97 | + public static boolean checkChinese(String chinese) { | ||
98 | + String regex = "^[\u4e00-\u9fa5]+$"; | ||
99 | + return Pattern.matches(regex,chinese); | ||
100 | + } | ||
101 | + | ||
102 | + /** | ||
103 | + * 验证日期(年月日) | ||
104 | + * @param birthday 日期,格式:1992-09-03,或1992.09.03 | ||
105 | + * @return 验证成功返回true,验证失败返回false | ||
106 | + */ | ||
107 | + public static boolean checkBirthday(String birthday) { | ||
108 | + String regex = "[1-9]{4}([-./])\\d{1,2}\\1\\d{1,2}"; | ||
109 | + return Pattern.matches(regex,birthday); | ||
110 | + } | ||
111 | + | ||
112 | + /** | ||
113 | + * 验证URL地址 | ||
114 | + * @param url 格式:http://blog.csdn.net:80/xyang81/article/details/7705960? 或 http://www.csdn.net:80 | ||
115 | + * @return 验证成功返回true,验证失败返回false | ||
116 | + */ | ||
117 | + public static boolean checkURL(String url) { | ||
118 | + String regex = "[a-zA-z]+://[^\\s]*"; | ||
119 | + return Pattern.matches(regex, url); | ||
120 | + } | ||
121 | + | ||
122 | + /** | ||
123 | + * <pre> | ||
124 | + * 获取网址 URL 的一级域名 | ||
125 | + * http://www.zuidaima.com/share/1550463379442688.htm ->> zuidaima.com | ||
126 | + * </pre> | ||
127 | + * | ||
128 | + * @param url | ||
129 | + * @return | ||
130 | + */ | ||
131 | + public static String getDomain(String url) { | ||
132 | + Pattern p = Pattern.compile("(?<=http://|\\.)[^.]*?\\.(com|cn|net|org|biz|info|cc|tv)", Pattern.CASE_INSENSITIVE); | ||
133 | + // 获取完整的域名 | ||
134 | + // Pattern p=Pattern.compile("[^//]*?\\.(com|cn|net|org|biz|info|cc|tv)", Pattern.CASE_INSENSITIVE); | ||
135 | + Matcher matcher = p.matcher(url); | ||
136 | + matcher.find(); | ||
137 | + return matcher.group(); | ||
138 | + } | ||
139 | + /** | ||
140 | + * 匹配中国邮政编码 | ||
141 | + * @param postcode 邮政编码 | ||
142 | + * @return 验证成功返回true,验证失败返回false | ||
143 | + */ | ||
144 | + public static boolean checkPostcode(String postcode) { | ||
145 | + String regex = "[1-9]\\d{5}(?!\\d)"; | ||
146 | + return Pattern.matches(regex, postcode); | ||
147 | + } | ||
148 | + | ||
149 | + /** | ||
150 | + * 匹配IP地址(简单匹配,格式,如:192.168.1.1,127.0.0.1,没有匹配IP段的大小) | ||
151 | + * @param ipAddress IPv4标准地址 | ||
152 | + * @return 验证成功返回true,验证失败返回false | ||
153 | + */ | ||
154 | + public static boolean checkIpAddress(String ipAddress) { | ||
155 | + String regex = "[1-9](\\d{1,2})?\\.(0|([1-9](\\d{1,2})?))\\.(0|([1-9](\\d{1,2})?))\\.(0|([1-9](\\d{1,2})?))"; | ||
156 | + return Pattern.matches(regex, ipAddress); | ||
157 | + } | ||
158 | + | ||
159 | + public static void main(String args[]){ | ||
160 | + System.out.println(checkMobile("13621051230")); | ||
161 | + } | ||
162 | +} |
@@ -0,0 +1,121 @@ | @@ -0,0 +1,121 @@ | ||
1 | +package com.taover.util; | ||
2 | + | ||
3 | +import java.io.IOException; | ||
4 | +import java.io.InputStream; | ||
5 | +import java.io.UnsupportedEncodingException; | ||
6 | +import java.nio.charset.Charset; | ||
7 | + | ||
8 | +import ch.ethz.ssh2.ChannelCondition; | ||
9 | +import ch.ethz.ssh2.Connection; | ||
10 | +import ch.ethz.ssh2.Session; | ||
11 | +import ch.ethz.ssh2.StreamGobbler; | ||
12 | + | ||
13 | +public class UtilRemoteShellExe { | ||
14 | + | ||
15 | + private Connection conn; | ||
16 | + /** 远程机器IP */ | ||
17 | + private String ip; | ||
18 | + /** 用户名 */ | ||
19 | + private String osUsername; | ||
20 | + /** 密码 */ | ||
21 | + private String password; | ||
22 | + private String charset = Charset.defaultCharset().toString(); | ||
23 | + | ||
24 | + private static final int TIME_OUT = 1000 * 5; | ||
25 | + | ||
26 | + /** | ||
27 | + * 构造函数 | ||
28 | + * @param ip | ||
29 | + * @param usr | ||
30 | + * @param pasword | ||
31 | + */ | ||
32 | + public UtilRemoteShellExe(String ip, String usr, String pasword) { | ||
33 | + this.ip = ip; | ||
34 | + this.osUsername = usr; | ||
35 | + this.password = pasword; | ||
36 | + } | ||
37 | + | ||
38 | + | ||
39 | + /** | ||
40 | + * 登录 | ||
41 | + * @return | ||
42 | + * @throws IOException | ||
43 | + */ | ||
44 | + private boolean login() throws IOException { | ||
45 | + conn = new Connection(ip); | ||
46 | + conn.connect(); | ||
47 | + return conn.authenticateWithPassword(osUsername, password); | ||
48 | + } | ||
49 | + | ||
50 | + /** | ||
51 | + * 执行脚本 | ||
52 | + * | ||
53 | + * @param cmds | ||
54 | + * @return | ||
55 | + * @throws Exception | ||
56 | + */ | ||
57 | + public int exec(String cmds) throws Exception { | ||
58 | + InputStream stdOut = null; | ||
59 | + InputStream stdErr = null; | ||
60 | + String outStr = ""; | ||
61 | + String outErr = ""; | ||
62 | + int ret = -1; | ||
63 | + try { | ||
64 | + if (login()) { | ||
65 | + // Open a new {@link Session} on this connection | ||
66 | + Session session = conn.openSession(); | ||
67 | + | ||
68 | + // Execute a command on the remote machine. | ||
69 | + session.execCommand(cmds); | ||
70 | + | ||
71 | + stdOut = new StreamGobbler(session.getStdout()); | ||
72 | + outStr = processStream(stdOut, charset); | ||
73 | + | ||
74 | + stdErr = new StreamGobbler(session.getStderr()); | ||
75 | + outErr = processStream(stdErr, charset); | ||
76 | + | ||
77 | + session.waitForCondition(ChannelCondition.EXIT_STATUS, TIME_OUT); | ||
78 | + | ||
79 | + System.out.println("SHELL PRINT INFO=" + outStr); | ||
80 | + System.out.println("SHELL PRINT ERROR=" + outErr); | ||
81 | + | ||
82 | + ret = session.getExitStatus(); | ||
83 | + | ||
84 | + session.close(); | ||
85 | + } else { | ||
86 | + throw new Exception("登录远程机器失败" + ip); // 自定义异常类 实现略 | ||
87 | + } | ||
88 | + } finally { | ||
89 | + if (conn != null) { | ||
90 | + conn.close(); | ||
91 | + } | ||
92 | + stdOut.close(); | ||
93 | + stdErr.close(); | ||
94 | + } | ||
95 | + return ret; | ||
96 | + } | ||
97 | + | ||
98 | + /** | ||
99 | + * @param in | ||
100 | + * @param charset | ||
101 | + * @return | ||
102 | + * @throws IOException | ||
103 | + * @throws UnsupportedEncodingException | ||
104 | + */ | ||
105 | + private String processStream(InputStream in, String charset) throws Exception { | ||
106 | + byte[] buf = new byte[1024]; | ||
107 | + StringBuilder sb = new StringBuilder(); | ||
108 | + while (in.read(buf) != -1) { | ||
109 | + sb.append(new String(buf, charset)); | ||
110 | + } | ||
111 | + return sb.toString(); | ||
112 | + } | ||
113 | + | ||
114 | + public static void main(String args[]) throws Exception { | ||
115 | + UtilRemoteShellExe executor = new UtilRemoteShellExe("47.93.114.120", "wangbin", "wangbin"); | ||
116 | + // 执行myTest.sh 参数为java Know dummy | ||
117 | + System.out.println(executor.exec("sh /home/wangbin/taover-vbote/boot.sh")); | ||
118 | +// System.out.println(executor.exec("cd taover-vbote")); | ||
119 | +// System.out.println(executor.exec("pwd")); | ||
120 | + } | ||
121 | +} | ||
0 | \ No newline at end of file | 122 | \ No newline at end of file |
@@ -0,0 +1,55 @@ | @@ -0,0 +1,55 @@ | ||
1 | +package com.taover.util; | ||
2 | + | ||
3 | +public class UtilResultInfo { | ||
4 | + public static final String SUCCESS ="success"; | ||
5 | + public static final String FAIL ="fail"; | ||
6 | + public static final String NOT_AUTHORIZED ="not_authorized"; | ||
7 | + | ||
8 | + public static ResultInfo getResultSuccessMessage(String error){ | ||
9 | + return getResult(UtilResultInfo.SUCCESS, error, null); | ||
10 | + } | ||
11 | + | ||
12 | + public static ResultInfo getResultFailureMessage(String error){ | ||
13 | + return getResult(UtilResultInfo.FAIL, error, null); | ||
14 | + } | ||
15 | + | ||
16 | + public static ResultInfo getResultSuccessMessage(String error, Object data){ | ||
17 | + return getResult(UtilResultInfo.SUCCESS, error, data); | ||
18 | + } | ||
19 | + | ||
20 | + public static ResultInfo getResultFailureMessage(String error, Object data){ | ||
21 | + return getResult(UtilResultInfo.FAIL, error, data); | ||
22 | + } | ||
23 | + | ||
24 | + public static ResultInfo getResult(String code, String error, Object data){ | ||
25 | + ResultInfo result = new ResultInfo(); | ||
26 | + result.setCode(code); | ||
27 | + result.setData(data); | ||
28 | + result.setError(error); | ||
29 | + return result; | ||
30 | + } | ||
31 | +} | ||
32 | + | ||
33 | +class ResultInfo { | ||
34 | + private String code; | ||
35 | + private String error; | ||
36 | + private Object data; | ||
37 | + public String getCode() { | ||
38 | + return code; | ||
39 | + } | ||
40 | + public void setCode(String code) { | ||
41 | + this.code = code; | ||
42 | + } | ||
43 | + public String getError() { | ||
44 | + return error; | ||
45 | + } | ||
46 | + public void setError(String error) { | ||
47 | + this.error = error; | ||
48 | + } | ||
49 | + public Object getData() { | ||
50 | + return data; | ||
51 | + } | ||
52 | + public void setData(Object data) { | ||
53 | + this.data = data; | ||
54 | + } | ||
55 | +} |
@@ -0,0 +1,180 @@ | @@ -0,0 +1,180 @@ | ||
1 | +package com.taover.util; | ||
2 | + | ||
3 | +import java.text.SimpleDateFormat; | ||
4 | +import java.util.ArrayList; | ||
5 | +import java.util.Date; | ||
6 | +import java.util.List; | ||
7 | + | ||
8 | +public class UtilString { | ||
9 | + /** | ||
10 | + * 在compares字符数组查找pattern字符串,找到则返回字串在数组中的索引,未找到返回-1 | ||
11 | + * @param pattern | ||
12 | + * @param compares | ||
13 | + * @return | ||
14 | + */ | ||
15 | + public static int getStringIndex(String pattern, String compares[]){ | ||
16 | + //参数检验 | ||
17 | + if(pattern==null || compares==null){ | ||
18 | + return -1; | ||
19 | + } | ||
20 | + | ||
21 | + //循环遍历compares | ||
22 | + for(int i=0; i<compares.length; ++i){ | ||
23 | + if(compares[i].equals(pattern)){ | ||
24 | + return i; | ||
25 | + } | ||
26 | + } | ||
27 | + return -1; | ||
28 | + } | ||
29 | + | ||
30 | + /** | ||
31 | + * 生成发货单的发货单内部序号, 格式:'D'.date('Ymd') +8位数字 | ||
32 | + * @return | ||
33 | + */ | ||
34 | + public static String getDeliverySn(){ | ||
35 | + String result = "D"; | ||
36 | + String date = new SimpleDateFormat("yyyyMMdd").format(new Date()); | ||
37 | + long temp = System.currentTimeMillis()%100000000; | ||
38 | + result = result + date + temp; | ||
39 | + return result; | ||
40 | + } | ||
41 | + | ||
42 | + public static String getBeanNameFormTableName(String tableName){ | ||
43 | + if (tableName == null) return null; | ||
44 | + if (tableName.length() == 0) return ""; | ||
45 | + byte[] ss = tableName.toLowerCase().getBytes(); | ||
46 | + int len = ss.length; | ||
47 | + byte[] ss1 = new byte[len]; | ||
48 | + ss1[0] = new String(ss, 0).toUpperCase().getBytes()[0]; | ||
49 | + int k = 1; | ||
50 | + for (int i = 1; i < ss.length; i++) { | ||
51 | + if (ss[i] == '_') { | ||
52 | + if (i < ss.length - 1) { | ||
53 | + ss1[k] = (byte)Character.toUpperCase(ss[(i + 1)]); | ||
54 | + i++; | ||
55 | + } | ||
56 | + len--; | ||
57 | + } else { | ||
58 | + ss1[k] = ss[i]; | ||
59 | + } | ||
60 | + k++; | ||
61 | + } | ||
62 | + return new String(ss1, 0, len); | ||
63 | + } | ||
64 | + | ||
65 | + public static String getCodeWithPreffix(long value, int digitNumber, char preffixChar){ | ||
66 | + String result = ""; | ||
67 | + if(value>Long.MAX_VALUE || value<Long.MIN_VALUE){ | ||
68 | + return null; | ||
69 | + } | ||
70 | + for(int i=digitNumber; i>0; --i){ | ||
71 | + long baseDiv = (long) Math.pow(10, i-1); | ||
72 | + long baseRand = (long) Math.pow(10, i); | ||
73 | + long currentDigit = value % baseRand / baseDiv; | ||
74 | + if(currentDigit == 0){ | ||
75 | + result = result + preffixChar; | ||
76 | + }else{ | ||
77 | + result = result + currentDigit; | ||
78 | + } | ||
79 | + } | ||
80 | + return result; | ||
81 | + } | ||
82 | + | ||
83 | + /** | ||
84 | + * 将驼峰式命名的字符串转换为下划线大写方式。如果转换前的驼峰式命名的字符串为空,则返回空字符串。</br> | ||
85 | + * 例如:HelloWorld->HELLO_WORLD | ||
86 | + * @param name 转换前的驼峰式命名的字符串 | ||
87 | + * @return 转换后下划线大写方式命名的字符串 | ||
88 | + */ | ||
89 | + public static String underscoreName(String name) { | ||
90 | + StringBuilder result = new StringBuilder(); | ||
91 | + if (name != null && name.length() > 0) { | ||
92 | + // 将第一个字符处理成大写 | ||
93 | + result.append(name.substring(0, 1).toUpperCase()); | ||
94 | + // 循环处理其余字符 | ||
95 | + for (int i = 1; i < name.length(); i++) { | ||
96 | + String s = name.substring(i, i + 1); | ||
97 | + // 在大写字母前添加下划线 | ||
98 | + if (Character.isUpperCase(s.charAt(0)) && Character.isLetter(s.charAt(0))) { | ||
99 | + result.append("_"); | ||
100 | + } | ||
101 | + // 其他字符直接转成大写 | ||
102 | + result.append(s.toUpperCase()); | ||
103 | + } | ||
104 | + } | ||
105 | + return result.toString(); | ||
106 | + } | ||
107 | + | ||
108 | + /** | ||
109 | + * 将下划线大写方式命名的字符串转换为驼峰式。如果转换前的下划线大写方式命名的字符串为空,则返回空字符串。</br> | ||
110 | + * 例如:HELLO_WORLD->HelloWorld | ||
111 | + * @param name 转换前的下划线大写方式命名的字符串 | ||
112 | + * @return 转换后的驼峰式命名的字符串 | ||
113 | + */ | ||
114 | + public static String camelName(String name) { | ||
115 | + StringBuilder result = new StringBuilder(); | ||
116 | + // 快速检查 | ||
117 | + if (name == null || name.isEmpty()) { | ||
118 | + // 没必要转换 | ||
119 | + return ""; | ||
120 | + } else if (!name.contains("_")) { | ||
121 | + // 不含下划线,仅将首字母小写 | ||
122 | + return name.substring(0, 1).toLowerCase() + name.substring(1); | ||
123 | + } | ||
124 | + // 用下划线将原始字符串分割 | ||
125 | + String camels[] = name.split("_"); | ||
126 | + for (String camel : camels) { | ||
127 | + // 跳过原始字符串中开头、结尾的下换线或双重下划线 | ||
128 | + if (camel.isEmpty()) { | ||
129 | + continue; | ||
130 | + } | ||
131 | + // 处理真正的驼峰片段 | ||
132 | + if (result.length() == 0) { | ||
133 | + // 第一个驼峰片段,全部字母都小写 | ||
134 | + result.append(camel.toLowerCase()); | ||
135 | + } else { | ||
136 | + // 其他的驼峰片段,首字母大写 | ||
137 | + result.append(camel.substring(0, 1).toUpperCase()); | ||
138 | + result.append(camel.substring(1).toLowerCase()); | ||
139 | + } | ||
140 | + } | ||
141 | + return result.toString(); | ||
142 | + } | ||
143 | + | ||
144 | + /** | ||
145 | + * 将idstring转换为integer数组 | ||
146 | + * @param ids | ||
147 | + * @param splitStr | ||
148 | + * @return | ||
149 | + */ | ||
150 | + public static Integer[] transIdStringToArray(String ids, String splitStr){ | ||
151 | + String idArray[] = ids.split(splitStr); | ||
152 | + List<Integer> idList = new ArrayList<Integer>(); | ||
153 | + for(int i=0; i<idArray.length; ++i){ | ||
154 | + String tempIdStr = idArray[i].trim(); | ||
155 | + if(!tempIdStr.isEmpty()){ | ||
156 | + try{ | ||
157 | + idList.add(Integer.valueOf(tempIdStr)); | ||
158 | + }catch(Exception e){ | ||
159 | + e.printStackTrace(); | ||
160 | + } | ||
161 | + } | ||
162 | + } | ||
163 | + Integer idArrayInt[] = new Integer[idList.size()]; | ||
164 | + for(int i=0; i<idList.size(); ++i){ | ||
165 | + idArrayInt[i] = idList.get(i); | ||
166 | + } | ||
167 | + return idArrayInt; | ||
168 | + } | ||
169 | + | ||
170 | + public static void main(String args[]){ | ||
171 | +// System.out.println(getBeanNameFormTableName("asdf_asdf")); | ||
172 | +// System.out.println(System.currentTimeMillis()); | ||
173 | +// System.out.println(getCodeWithPreffix(1231212, 10, '-')); | ||
174 | + String dd = "\"{\"success\":true,\"code\":1,\"printedorder_id\":\"1654\",\"error_message\":\"\"}\""; | ||
175 | + dd = dd.substring(1, dd.length()-1); | ||
176 | + System.out.println(dd); | ||
177 | + //JSONObject temp = JSONObject.fromObject(dd); | ||
178 | + //System.out.println(temp.getInt("code")); | ||
179 | + } | ||
180 | +} | ||
0 | \ No newline at end of file | 181 | \ No newline at end of file |
@@ -0,0 +1,72 @@ | @@ -0,0 +1,72 @@ | ||
1 | +package com.taover.util; | ||
2 | + | ||
3 | +import java.io.PrintWriter; | ||
4 | +import java.io.StringWriter; | ||
5 | +import java.util.Iterator; | ||
6 | +import java.util.Map; | ||
7 | +import java.util.Properties; | ||
8 | +import java.util.Set; | ||
9 | + | ||
10 | +import org.apache.commons.logging.Log; | ||
11 | +import org.apache.commons.logging.LogFactory; | ||
12 | +import org.apache.velocity.Template; | ||
13 | +import org.apache.velocity.VelocityContext; | ||
14 | +import org.apache.velocity.app.VelocityEngine; | ||
15 | + | ||
16 | +public class UtilTemplate { | ||
17 | + | ||
18 | + /** | ||
19 | + * 通过velocity引擎渲染模板文件,模板所在类路径 | ||
20 | + * @param templateClassPath | ||
21 | + * @param data | ||
22 | + * @return | ||
23 | + */ | ||
24 | + public static String renderTemplateClass(String templateClassPath, Map<String, Object> data){ | ||
25 | + //初始化 | ||
26 | + VelocityEngine ve = new VelocityEngine(); | ||
27 | + Properties p = new Properties(); | ||
28 | + p.put("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.SimpleLog4JLogSystem"); | ||
29 | + p.put("runtime.log.logsystem.log4j.category", "velocity"); | ||
30 | + p.put("runtime.log.logsystem.log4j.logger", "velocity"); | ||
31 | + p.put(VelocityEngine.INPUT_ENCODING, "UTF-8"); | ||
32 | + p.put(VelocityEngine.OUTPUT_ENCODING, "UTF-8"); | ||
33 | + p.put(VelocityEngine.ENCODING_DEFAULT, "UTF-8"); | ||
34 | + p.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); | ||
35 | + try { | ||
36 | + ve.init(p); | ||
37 | + } catch (Exception e) { | ||
38 | + StringWriter sw = new StringWriter(); | ||
39 | + e.printStackTrace(new PrintWriter(sw, true)); | ||
40 | + Log log = LogFactory.getLog(UtilTemplate.class); | ||
41 | + log.error(sw.toString()); | ||
42 | + } | ||
43 | + //取得VelocityContext对象 | ||
44 | + VelocityContext context = new VelocityContext(); | ||
45 | + //向context中放入要在模板中用到的数据对象 | ||
46 | + if(data != null){ | ||
47 | + Set<String> keySet = data.keySet(); | ||
48 | + Iterator<String> iter = keySet.iterator(); | ||
49 | + while(iter.hasNext()){ | ||
50 | + String key = iter.next(); | ||
51 | + context.put(key, data.get(key)); | ||
52 | + } | ||
53 | + } | ||
54 | + //选择要用到的模板 | ||
55 | + if( !ve.resourceExists(templateClassPath) ){ | ||
56 | + return null; | ||
57 | + } | ||
58 | + Template template; | ||
59 | + StringWriter sw = new StringWriter(); | ||
60 | + try { | ||
61 | + template = ve.getTemplate(templateClassPath, "UTF-8"); | ||
62 | + //合并输出 | ||
63 | + template.merge( context, sw); | ||
64 | + } catch (Exception e) { | ||
65 | + StringWriter swE = new StringWriter(); | ||
66 | + e.printStackTrace(new PrintWriter(swE, true)); | ||
67 | + Log log = LogFactory.getLog(UtilTemplate.class); | ||
68 | + log.error(sw.toString()); | ||
69 | + } | ||
70 | + return sw.toString(); | ||
71 | + } | ||
72 | +} |