TestMain.java
3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package com.subsidy.util;
import org.apache.commons.codec.binary.Base64;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.security.MessageDigest;
import java.util.Random;
public class TestMain {
/**
* 对字节数组字符串进行Base64解码并生成图片
* @param imgStr 图片数据
* @param imgFilePath 保存图片全路径地址
* @return
*/
public static boolean generateImage(String imgStr,String imgFilePath){
//
if (imgStr == null) //图像数据为空
{
return false;
}
try
{
//Base64解码
byte[] b = Base64.decodeBase64(imgStr);
for(int i=0;i<b.length;++i)
{
if(b[i]<0)
{//调整异常数据
b[i]+=256;
}
}
//生成jpeg图片
OutputStream out = new FileOutputStream(imgFilePath);
out.write(b);
out.flush();
out.close();
return true;
}
catch (Exception e)
{
return false;
}
}
public static void main(String[] args) throws Exception{
// String password = "123456";
// String saltCode = getRandomCharAndNumr(5);
// String encryptString = encrypt("91ebn" + password);
// System.out.println(encryptString);
// Thread thread1 = new Thread(() -> {
// System.out.println("thread1");
// });
// Thread thread2 = new Thread(() -> {
// System.out.println("thread2");
// });
// Thread thread3 = new Thread(() -> {
// System.out.println("thread3");
// });
// thread1.start();
// thread1.join();
// thread2.start();
// thread2.join();
// thread3.start();
// thread3.join();
}
public static String encrypt(String string) {
String encryptedString = string;
char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
try {
byte[] btInput = string.getBytes();
MessageDigest mdInst = MessageDigest.getInstance("MD5");
mdInst.update(btInput);
byte[] md = mdInst.digest();
int j = md.length;
char[] str = new char[j * 2];
int k = 0;
for (int i = 0; i < j; i++) {
byte byte0 = md[i];
str[k++] = hexDigits[byte0 >>> 4 & 0xf];
str[k++] = hexDigits[byte0 & 0xf];
}
encryptedString = new String(str);
return encryptedString;
} catch (Exception e) {
e.printStackTrace();
return encryptedString;
}
}
/**
* 获取随机字母数字组合
*
* @param length
* 字符串长度
* @return
*/
public static String getRandomCharAndNumr(Integer length) {
String str = "";
Random random = new Random();
for (int i = 0; i < length; i++) {
boolean b = random.nextBoolean();
if (b) { // 字符串
// int choice = random.nextBoolean() ? 65 : 97; 取得65大写字母还是97小写字母
str += (char) (97 + random.nextInt(26));// 取得大写字母
} else { // 数字
str += String.valueOf(random.nextInt(10));
}
}
return str;
}
}