mirror of
https://gitee.com/jzsw-it/yexuejc-base.git
synced 2025-06-06 22:04:04 +08:00
1.1.7
This commit is contained in:
parent
fd2d7a51bb
commit
a2a247bbff
@ -1,6 +1,15 @@
|
||||
yexuejc-base 更新记录
|
||||
------------------
|
||||
|
||||
#### version :1.1.8
|
||||
**time:2018-8-17 11:22:50** <br/>
|
||||
**branch:** master <br/>
|
||||
**update:** <br/>
|
||||
>1. 增肌图片处理工具类
|
||||
>2. 增肌3des工具类
|
||||
>3. 增肌RSA工具类
|
||||
>4. 优化其他工具类
|
||||
#
|
||||
#### version :1.1.7
|
||||
**time:2018-8-17 11:22:50** <br/>
|
||||
**branch:** master <br/>
|
||||
|
13
pom.xml
13
pom.xml
@ -6,7 +6,7 @@
|
||||
|
||||
<groupId>com.yexuejc.base</groupId>
|
||||
<artifactId>yexuejc-base</artifactId>
|
||||
<version>1.1.7</version>
|
||||
<version>1.1.8</version>
|
||||
|
||||
<name>${project.artifactId}</name>
|
||||
|
||||
@ -20,6 +20,7 @@
|
||||
<validation-api.version>1.1.0.Final</validation-api.version>
|
||||
<commons-codec.version>1.10</commons-codec.version>
|
||||
<commons-io.version>2.6</commons-io.version>
|
||||
<bcprov-jdk15on.version>1.60</bcprov-jdk15on.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
@ -48,6 +49,12 @@
|
||||
<artifactId>commons-io</artifactId>
|
||||
<version>${commons-io.version}</version>
|
||||
</dependency>
|
||||
<!--支持大量的密码术算法,并提供JCE 1.2.1的实现-->
|
||||
<dependency>
|
||||
<groupId>org.bouncycastle</groupId>
|
||||
<artifactId>bcprov-jdk15on</artifactId>
|
||||
<version>${bcprov-jdk15on.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
||||
@ -59,8 +66,8 @@
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<encoding>UTF-8</encoding>
|
||||
<source>${java.version}</source>
|
||||
<target>${java.version}</target>
|
||||
<source>8</source>
|
||||
<target>8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<!-- 打包源码 -->
|
||||
|
@ -107,7 +107,6 @@ public class RSA {
|
||||
* @param privateKey
|
||||
* @return
|
||||
*/
|
||||
|
||||
public static String privateDecrypt(String data, RSAPrivateKey privateKey) {
|
||||
try {
|
||||
Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);
|
||||
|
200
src/main/java/com/yexuejc/base/encrypt/RSACoder.java
Normal file
200
src/main/java/com/yexuejc/base/encrypt/RSACoder.java
Normal file
@ -0,0 +1,200 @@
|
||||
package com.yexuejc.base.encrypt;
|
||||
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
|
||||
import javax.crypto.Cipher;
|
||||
import java.security.Key;
|
||||
import java.security.KeyFactory;
|
||||
import java.security.spec.PKCS8EncodedKeySpec;
|
||||
import java.security.spec.X509EncodedKeySpec;
|
||||
|
||||
/**
|
||||
* RSA 加解密 工具模式
|
||||
*
|
||||
* @author maxf
|
||||
* @ClassName RSACoder
|
||||
* @Description
|
||||
* @date 2018/9/3 16:13
|
||||
*/
|
||||
public class RSACoder {
|
||||
|
||||
public static final String KEY_ALGORITHM = "RSA";
|
||||
|
||||
/**
|
||||
* 解密<br>
|
||||
* 用公钥解密
|
||||
*
|
||||
* @param data
|
||||
* @param key
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static byte[] decryptByPublic(String data, String key)
|
||||
throws Exception {
|
||||
return getDataByPublicKey(data, key, Cipher.DECRYPT_MODE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 解密<br>
|
||||
* 用私钥解密
|
||||
*
|
||||
* @param data
|
||||
* @param key
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static byte[] decryptByPrivateKey(String data, String key)
|
||||
throws Exception {
|
||||
return getDataByPrivateKey(data, key, Cipher.DECRYPT_MODE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 加密<br>
|
||||
* 用公钥加密
|
||||
*
|
||||
* @param data
|
||||
* @param key
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static byte[] encryptByPublicKey(String data, String key)
|
||||
throws Exception {
|
||||
return getDataByPublicKey(data, key, Cipher.ENCRYPT_MODE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 加密<br>
|
||||
* 用私钥加密
|
||||
*
|
||||
* @param data
|
||||
* @param key
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static byte[] encryptByPrivateKey(String data, String key)
|
||||
throws Exception {
|
||||
return getDataByPrivateKey(data, key, Cipher.ENCRYPT_MODE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 解密<br>
|
||||
* 用公钥解密
|
||||
*
|
||||
* @param data
|
||||
* @param key
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static byte[] decryptByPublic(byte[] data, String key)
|
||||
throws Exception {
|
||||
return getDataByPublicKey(data, key, Cipher.DECRYPT_MODE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 解密<br>
|
||||
* 用私钥解密
|
||||
*
|
||||
* @param data
|
||||
* @param key
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static byte[] decryptByPrivateKey(byte[] data, String key)
|
||||
throws Exception {
|
||||
return getDataByPrivateKey(data, key, Cipher.DECRYPT_MODE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 加密<br>
|
||||
* 用公钥加密
|
||||
*
|
||||
* @param data
|
||||
* @param key
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static byte[] encryptByPublicKey(byte[] data, String key)
|
||||
throws Exception {
|
||||
return getDataByPublicKey(data, key, Cipher.ENCRYPT_MODE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 加密<br>
|
||||
* 用私钥加密
|
||||
*
|
||||
* @param data
|
||||
* @param key
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static byte[] encryptByPrivateKey(byte[] data, String key)
|
||||
throws Exception {
|
||||
return getDataByPrivateKey(data, key, Cipher.ENCRYPT_MODE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过公钥获得加解密数据
|
||||
*
|
||||
* @param data String
|
||||
* @param key String
|
||||
* @param mode int
|
||||
* @return
|
||||
*/
|
||||
public static byte[] getDataByPublicKey(String data, String key, int mode)
|
||||
throws Exception {
|
||||
return getDataByPublicKey(data.getBytes(), key, mode);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过私钥获得加解密数据
|
||||
*
|
||||
* @param data String
|
||||
* @param key String
|
||||
* @param mode 加密或解密
|
||||
* @return
|
||||
*/
|
||||
public static byte[] getDataByPrivateKey(String data, String key, int mode)
|
||||
throws Exception {
|
||||
return getDataByPrivateKey(data.getBytes(), key, mode);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过公钥获得加解密数据
|
||||
*
|
||||
* @param data String
|
||||
* @param key String
|
||||
* @param mode int
|
||||
* @return
|
||||
*/
|
||||
public static byte[] getDataByPublicKey(byte[] data, String key, int mode)
|
||||
throws Exception {
|
||||
// 取得私钥
|
||||
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(Base64.decodeBase64(key.getBytes("UTF-8")));
|
||||
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
|
||||
Key privateKey = keyFactory.generatePublic(x509KeySpec);
|
||||
// 对数据进行加密或解密
|
||||
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
|
||||
cipher.init(mode, privateKey);
|
||||
return cipher.doFinal(data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过私钥获得加解密数据
|
||||
*
|
||||
* @param data String
|
||||
* @param key String
|
||||
* @param mode 加密或解密
|
||||
* @return
|
||||
*/
|
||||
public static byte[] getDataByPrivateKey(byte[] data, String key, int mode)
|
||||
throws Exception {
|
||||
// 取得私钥
|
||||
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(Base64.decodeBase64(key.getBytes("UTF-8")));
|
||||
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
|
||||
Key privateKey = keyFactory.generatePrivate(pkcs8KeySpec);
|
||||
// 对数据解密
|
||||
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
|
||||
cipher.init(mode, privateKey);
|
||||
return cipher.doFinal(data);
|
||||
}
|
||||
}
|
@ -1,6 +1,8 @@
|
||||
package com.yexuejc.base.util;
|
||||
|
||||
|
||||
import sun.misc.BASE64Decoder;
|
||||
|
||||
/**
|
||||
* 算法工具类
|
||||
*
|
||||
@ -107,4 +109,47 @@ public class AlgorithmUtils {
|
||||
static class NextCodeException extends Exception {
|
||||
private static final long serialVersionUID = 8956943499144648985L;
|
||||
}
|
||||
|
||||
/**
|
||||
* 16进制转为2进制
|
||||
*
|
||||
* @param hexString 16进制字符串
|
||||
*/
|
||||
public static String x16ConvertTo2(String hexString) {
|
||||
if (hexString == null || hexString.length() % 2 != 0) {
|
||||
return null;
|
||||
}
|
||||
String bString = "", tmp;
|
||||
for (int i = 0; i < hexString.length(); i++) {
|
||||
tmp = "0000" + Integer.toBinaryString(Integer.parseInt(hexString.substring(i, i + 1), 16));
|
||||
bString += tmp.substring(tmp.length() - 4);
|
||||
}
|
||||
return bString;
|
||||
}
|
||||
|
||||
/**
|
||||
* 16进制转为2进制byte[]
|
||||
*
|
||||
* @param hexString 16进制字符串
|
||||
* @return
|
||||
*/
|
||||
public static byte[] hexStringToBytes(String hexString) {
|
||||
if (hexString == null || hexString.equals("")) {
|
||||
return null;
|
||||
}
|
||||
hexString = hexString.toUpperCase();
|
||||
int length = hexString.length() / 2;
|
||||
char[] hexChars = hexString.toCharArray();
|
||||
byte[] d = new byte[length];
|
||||
for (int i = 0; i < length; i++) {
|
||||
int pos = i * 2;
|
||||
d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
|
||||
}
|
||||
return d;
|
||||
}
|
||||
|
||||
private static byte charToByte(char c) {
|
||||
return (byte) "0123456789ABCDEF".indexOf(c);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -6,7 +6,13 @@ import java.text.SimpleDateFormat;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* java.util.Date 时间工具类
|
||||
* @ClassName DateUtil
|
||||
* @Description
|
||||
* @author maxf
|
||||
* @date 2018/9/3 15:27
|
||||
*/
|
||||
public class DateUtil {
|
||||
private DateUtil() {
|
||||
}
|
||||
@ -71,7 +77,7 @@ public class DateUtil {
|
||||
/**
|
||||
* 日期字符串转date
|
||||
*
|
||||
* @param date
|
||||
* @param dateStr
|
||||
* @return Date
|
||||
* @throws ParseException
|
||||
*/
|
||||
|
419
src/main/java/com/yexuejc/base/util/ImgUtil.java
Normal file
419
src/main/java/com/yexuejc/base/util/ImgUtil.java
Normal file
@ -0,0 +1,419 @@
|
||||
package com.yexuejc.base.util;
|
||||
|
||||
import sun.misc.BASE64Decoder;
|
||||
import sun.misc.BASE64Encoder;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.imageio.ImageReader;
|
||||
import javax.imageio.stream.ImageInputStream;
|
||||
import java.io.*;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.Proxy;
|
||||
import java.net.URL;
|
||||
import java.util.Iterator;
|
||||
|
||||
/**
|
||||
* 图片处理工具类
|
||||
*
|
||||
* @author maxf
|
||||
* @ClassName ImageUtil
|
||||
* @Description
|
||||
* @date 2018/9/3 15:25
|
||||
*/
|
||||
public class ImgUtil {
|
||||
|
||||
/**
|
||||
* 将一张网络图片转化成Base64字符串
|
||||
*
|
||||
* @param imgURL 网络图片url
|
||||
* @return String Base64字符串
|
||||
*/
|
||||
public static String getImageStrFromUrl(String imgURL) {
|
||||
ByteArrayOutputStream data = new ByteArrayOutputStream();
|
||||
try {
|
||||
byte[] by = new byte[1024];
|
||||
InputStream is = getImageInputStreamFromUrl(null, imgURL);
|
||||
// 将内容读取内存中
|
||||
int len = -1;
|
||||
while ((len = is.read(by)) != -1) {
|
||||
data.write(by, 0, len);
|
||||
}
|
||||
// 关闭流
|
||||
is.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
// 对字节数组Base64编码
|
||||
BASE64Encoder encoder = new BASE64Encoder();
|
||||
return encoder.encode(data.toByteArray());
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取网络图片url为输入流
|
||||
* <p>本方法未关闭io流,请自行关闭io流</p>
|
||||
* <p>is.close();</p>
|
||||
*
|
||||
* @param imgURL 网络图片url
|
||||
* @return InputStream 输入流
|
||||
* @throws IOException
|
||||
*/
|
||||
public static InputStream getImageInputStreamFromUrl(Proxy proxy, String imgURL) throws IOException {
|
||||
// 创建URL
|
||||
URL url = new URL(imgURL);
|
||||
// 创建链接
|
||||
HttpURLConnection conn;
|
||||
if (null == proxy) {
|
||||
conn = (HttpURLConnection) url.openConnection();
|
||||
} else {
|
||||
// 如果有代理则通过代理下载
|
||||
conn = (HttpURLConnection) url.openConnection(proxy);
|
||||
}
|
||||
conn.setRequestMethod("GET");
|
||||
conn.setConnectTimeout(5000);
|
||||
InputStream is = conn.getInputStream();
|
||||
return is;
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取网络图片url为输入流
|
||||
* <p>本方法未关闭io流,请自行关闭io流</p>
|
||||
* <p>is.close();</p>
|
||||
*
|
||||
* @param imgURL
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
public static InputStream getImageInputStreamFromUrl(String imgURL) throws IOException {
|
||||
return getImageInputStreamFromUrl(null, imgURL);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 将图片文件流转换成字节数组
|
||||
* <p>
|
||||
* 好处就是字节数组可以多次利用,而流一旦读取过一次之后就不能再使用了
|
||||
*
|
||||
* @param proxy
|
||||
* @param targetUrl
|
||||
* @return
|
||||
* @throws MalformedURLException
|
||||
* @throws IOException
|
||||
*/
|
||||
public static byte[] getByteArrayFromInputStream(Proxy proxy, String targetUrl)
|
||||
throws MalformedURLException, IOException {
|
||||
InputStream is = getImageInputStreamFromUrl(proxy, targetUrl);
|
||||
return getByteArray(is);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将图片文件流转换成字节数组
|
||||
* <p>
|
||||
* 好处就是字节数组可以多次利用,而流一旦读取过一次之后就不能再使用了
|
||||
*
|
||||
* @param targetUrl
|
||||
* @return
|
||||
* @throws MalformedURLException
|
||||
* @throws IOException
|
||||
*/
|
||||
public static byte[] getByteArrayFromInputStream(String targetUrl)
|
||||
throws MalformedURLException, IOException {
|
||||
return getByteArrayFromInputStream(null, targetUrl);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将图片文件流转换成字节数组
|
||||
*
|
||||
* @return
|
||||
* @throws MalformedURLException
|
||||
* @throws IOException
|
||||
*/
|
||||
public static byte[] getByteArray(InputStream is)
|
||||
throws MalformedURLException, IOException {
|
||||
// 把文件写到字节数组保存起来
|
||||
ByteArrayOutputStream fos = getByteArrayFromInputStream(is);
|
||||
byte[] bytes = fos.toByteArray();
|
||||
fos.close();
|
||||
return bytes;
|
||||
}
|
||||
|
||||
/**
|
||||
* ByteArrayOutputStream转换成字节数组
|
||||
*
|
||||
* @param fos
|
||||
* @return
|
||||
* @throws MalformedURLException
|
||||
* @throws IOException
|
||||
*/
|
||||
public static byte[] getByteArray(ByteArrayOutputStream fos)
|
||||
throws MalformedURLException, IOException {
|
||||
// 把文件写到字节数组保存起来
|
||||
byte[] bytes = fos.toByteArray();
|
||||
fos.close();
|
||||
return bytes;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将图片文件流转换成ByteArrayOutputStream
|
||||
* <p>本方法未关闭io流,请自行关闭io流</p>
|
||||
* <p>is.close();</p>
|
||||
*
|
||||
* @param is
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
public static ByteArrayOutputStream getByteArrayFromInputStream(InputStream is) throws IOException {
|
||||
// 把文件写到字节数组保存起来
|
||||
BufferedInputStream bis = new BufferedInputStream(is);
|
||||
ByteArrayOutputStream fos = new ByteArrayOutputStream();
|
||||
byte[] buffer = new byte[1024];
|
||||
int len = 0;
|
||||
while ((len = bis.read(buffer)) != -1) {
|
||||
fos.write(buffer, 0, len);
|
||||
}
|
||||
bis.close();
|
||||
return fos;
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果你已经将图片文件流InputStream读取出来放到一个字节数组
|
||||
* <p>
|
||||
* 那么根据这个字节数组也是可以转换成对应的图片流,并再次获取图片基本信息
|
||||
*
|
||||
* @param imgBytes
|
||||
* @return
|
||||
*/
|
||||
public static ImageInfo getImageInfoFromInputStream(byte[] imgBytes) {
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(imgBytes);
|
||||
ImageInfo image = getImageInfoFromInputStream(in);
|
||||
return image;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从图片文件流读取图片文件的基本信息
|
||||
*
|
||||
* @param inputStream
|
||||
* @return
|
||||
*/
|
||||
public static ImageInfo getImageInfoFromInputStream(InputStream inputStream) {
|
||||
ImageInputStream imgStream = null;
|
||||
try {
|
||||
// 创建Image流
|
||||
imgStream = ImageIO.createImageInputStream(inputStream);
|
||||
Iterator<ImageReader> iter = ImageIO.getImageReaders(imgStream);
|
||||
if (!iter.hasNext()) {
|
||||
return null;
|
||||
}
|
||||
// 读取流中一帧就可以获取到高宽以及各式
|
||||
ImageReader reader = iter.next();
|
||||
reader.setInput(imgStream, true);
|
||||
int width = reader.getWidth(0);
|
||||
int height = reader.getHeight(0);
|
||||
String type = reader.getFormatName();
|
||||
ImageInfo bean = new ImageInfo();
|
||||
bean.setWidth(width);
|
||||
bean.setHeight(height);
|
||||
bean.setType(type);
|
||||
return bean;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
} finally {
|
||||
try {
|
||||
imgStream.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 将byte数组以Base64方式编码为字符串
|
||||
*
|
||||
* @param bytes 待编码的byte数组
|
||||
* @return String 编码后的字符串
|
||||
*/
|
||||
public static String encode(byte[] bytes) {
|
||||
return new BASE64Encoder().encode(bytes);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将以Base64方式编码的字符串解码为byte数组
|
||||
*
|
||||
* @param encodeStr 待解码的字符串
|
||||
* @return byte[] 解码后的byte数组
|
||||
* @throws IOException
|
||||
*/
|
||||
public static byte[] decode(String encodeStr) throws IOException {
|
||||
byte[] bt = null;
|
||||
BASE64Decoder decoder = new BASE64Decoder();
|
||||
bt = decoder.decodeBuffer(encodeStr);
|
||||
return bt;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将两个byte数组连接起来后,返回连接后的Byte数组
|
||||
*
|
||||
* @param front 拼接后在前面的数组
|
||||
* @param after 拼接后在后面的数组
|
||||
* @return byte[] 拼接后的数组
|
||||
*/
|
||||
public static byte[] connectBytes(byte[] front, byte[] after) {
|
||||
byte[] result = new byte[front.length + after.length];
|
||||
System.arraycopy(front, 0, result, 0, after.length);
|
||||
System.arraycopy(after, 0, result, front.length, after.length);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将流保存至本机或者图片服务器
|
||||
*
|
||||
* @param in 图片流
|
||||
* @param saveUrl 目标地址
|
||||
* @throws IOException
|
||||
*/
|
||||
public static void saveImgFromInputStream(InputStream in, String saveUrl) throws IOException {
|
||||
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(saveUrl));
|
||||
byte[] buffer = new byte[1024];
|
||||
int len = 0;
|
||||
while ((len = in.read(buffer)) != -1) {
|
||||
bos.write(buffer, 0, len);
|
||||
}
|
||||
bos.close();
|
||||
in.close();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 将图片以Base64方式编码为字符串
|
||||
*
|
||||
* @param imgUrl 图片的绝对路径(例如:D:\\1.jpg)
|
||||
* @return String 编码后的字符串
|
||||
* @throws IOException
|
||||
*/
|
||||
public static String encodeImage(String imgUrl) throws IOException {
|
||||
FileInputStream fis = new FileInputStream(imgUrl);
|
||||
byte[] rs = new byte[fis.available()];
|
||||
fis.read(rs);
|
||||
fis.close();
|
||||
return encode(rs);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取图片返回bate[]
|
||||
*
|
||||
* @param imgUrl 图片的绝对路径(例如:D:\\1.jpg)
|
||||
* @return byte[]
|
||||
* @throws IOException
|
||||
*/
|
||||
public static byte[] byteImage(String imgUrl) throws IOException {
|
||||
FileInputStream fis = new FileInputStream(imgUrl);
|
||||
byte[] rs = new byte[fis.available()];
|
||||
return rs;
|
||||
}
|
||||
|
||||
public static class ImageInfo {
|
||||
/**
|
||||
* 图片大小
|
||||
*/
|
||||
private long size = 0;
|
||||
/**
|
||||
* 图片宽度
|
||||
*/
|
||||
private int width = 0;
|
||||
/**
|
||||
* 图片高度
|
||||
*/
|
||||
private int height = 0;
|
||||
/**
|
||||
* 图片类型
|
||||
*/
|
||||
private String type = "jpg";
|
||||
/**
|
||||
* 符号要求的图片类型
|
||||
*/
|
||||
private String[] validTypes;
|
||||
|
||||
/**
|
||||
* 定义一个简单方法描述该图片是否符合要求
|
||||
*
|
||||
* @param validImg
|
||||
* @return
|
||||
*/
|
||||
public boolean isValidImg(ImageInfo validImg) {
|
||||
if (null == validImg) {
|
||||
return true;
|
||||
}
|
||||
return (this.getSize() <= validImg.getSize()) && (this.getWidth() <= validImg.getWidth())
|
||||
&& (this.getHeight() <= validImg.getHeight()) && isValidType(validImg);
|
||||
|
||||
}
|
||||
|
||||
private boolean isValidType(ImageInfo validImg) {
|
||||
if (null == validImg.getValidTypes()) {
|
||||
return true;
|
||||
}
|
||||
boolean isValid = false;
|
||||
for (String validType : validImg.getValidTypes()) {
|
||||
if (type.equalsIgnoreCase(validType)) {
|
||||
isValid = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return isValid;
|
||||
}
|
||||
|
||||
private String validTypeToString() {
|
||||
if (null == validTypes) {
|
||||
return "";
|
||||
}
|
||||
StringBuffer sb = new StringBuffer("[");
|
||||
for (String type : validTypes) {
|
||||
sb.append(type).append(" ");
|
||||
}
|
||||
sb.append("]");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public long getSize() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public void setSize(long size) {
|
||||
this.size = size;
|
||||
}
|
||||
|
||||
public int getWidth() {
|
||||
return width;
|
||||
}
|
||||
|
||||
public void setWidth(int width) {
|
||||
this.width = width;
|
||||
}
|
||||
|
||||
public int getHeight() {
|
||||
return height;
|
||||
}
|
||||
|
||||
public void setHeight(int height) {
|
||||
this.height = height;
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(String type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public String[] getValidTypes() {
|
||||
return validTypes;
|
||||
}
|
||||
|
||||
public void setValidTypes(String[] validTypes) {
|
||||
this.validTypes = validTypes;
|
||||
}
|
||||
}
|
||||
}
|
@ -9,6 +9,14 @@ import com.fasterxml.jackson.databind.*;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* json工具类,基于jackson
|
||||
*
|
||||
* @author maxf
|
||||
* @ClassName JsonUtil
|
||||
* @Description
|
||||
* @date 2018/9/3 15:28
|
||||
*/
|
||||
public class JsonUtil {
|
||||
private JsonUtil() {
|
||||
}
|
||||
|
@ -6,17 +6,71 @@ import io.jsonwebtoken.SignatureAlgorithm;
|
||||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* jwt工具类
|
||||
*
|
||||
* @author maxf
|
||||
* @ClassName JwtUtil
|
||||
* @Description
|
||||
* @date 2018/9/3 15:28
|
||||
*/
|
||||
public class JwtUtil {
|
||||
/** 加密用KEY */
|
||||
/**
|
||||
* 加密用KEY
|
||||
*/
|
||||
private static String JWT_SIGNATURE_KEY = "h%OG8Y3WgA5AN7&6Ke7I#C1XvneW0N8a";
|
||||
/** token类型 */
|
||||
/**
|
||||
* token类型
|
||||
*/
|
||||
private static String JWT_HEADER_TYP = "JWT";
|
||||
/** token发行商 */
|
||||
/**
|
||||
* token发行商
|
||||
*/
|
||||
private static String JWT_CLAIMS_ISS = "yexuejc.com";
|
||||
|
||||
/**
|
||||
* 设置配置
|
||||
*
|
||||
* @param key
|
||||
* @param type
|
||||
* @param iss
|
||||
*/
|
||||
public static void setConf(String key, String type, String iss) {
|
||||
JWT_SIGNATURE_KEY = key;
|
||||
JWT_HEADER_TYP = type;
|
||||
JWT_CLAIMS_ISS = iss;
|
||||
}
|
||||
|
||||
/**
|
||||
* 加密用KEY
|
||||
*
|
||||
* @param key
|
||||
*/
|
||||
public static void setSignatureKey(String key) {
|
||||
JWT_SIGNATURE_KEY = key;
|
||||
}
|
||||
|
||||
/**
|
||||
* token类型
|
||||
*
|
||||
* @param type
|
||||
*/
|
||||
public static void setHeaderType(String type) {
|
||||
JWT_HEADER_TYP = type;
|
||||
}
|
||||
|
||||
/**
|
||||
* token发行商
|
||||
*
|
||||
* @param iss
|
||||
*/
|
||||
public static void setClaimsIss(String iss) {
|
||||
JWT_CLAIMS_ISS = iss;
|
||||
}
|
||||
|
||||
/**
|
||||
* 加密内容生成token
|
||||
*
|
||||
*
|
||||
* @param subjectObj
|
||||
* @return
|
||||
*/
|
||||
@ -48,7 +102,7 @@ public class JwtUtil {
|
||||
|
||||
/**
|
||||
* 解密token为一个Map
|
||||
*
|
||||
*
|
||||
* @param token
|
||||
* @return
|
||||
*/
|
||||
@ -58,7 +112,7 @@ public class JwtUtil {
|
||||
|
||||
/**
|
||||
* 解密token为一个指定对象
|
||||
*
|
||||
*
|
||||
* @param token
|
||||
* @param cls
|
||||
* @return
|
||||
|
@ -5,6 +5,14 @@ import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* map相关工具
|
||||
*
|
||||
* @author maxf
|
||||
* @ClassName MapRemoveNullUtil
|
||||
* @Description
|
||||
* @date 2018/9/3 15:32
|
||||
*/
|
||||
public class MapRemoveNullUtil {
|
||||
private MapRemoveNullUtil() {
|
||||
}
|
||||
|
@ -4,7 +4,7 @@ import java.math.BigDecimal;
|
||||
import java.text.DecimalFormat;
|
||||
|
||||
/**
|
||||
* q钱相关工具类
|
||||
* 钱相关工具类
|
||||
*
|
||||
* @ClassName: MoneyUtils
|
||||
* @Description:
|
||||
|
101
src/main/java/com/yexuejc/base/util/ThreeDES.java
Normal file
101
src/main/java/com/yexuejc/base/util/ThreeDES.java
Normal file
@ -0,0 +1,101 @@
|
||||
package com.yexuejc.base.util;
|
||||
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
|
||||
import javax.crypto.Cipher;
|
||||
import javax.crypto.SecretKeyFactory;
|
||||
import javax.crypto.spec.DESedeKeySpec;
|
||||
import javax.crypto.spec.IvParameterSpec;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.security.InvalidKeyException;
|
||||
import java.security.Key;
|
||||
|
||||
|
||||
/**
|
||||
* 3DES加解密
|
||||
*
|
||||
* @author maxf
|
||||
* @ClassName ThreeDES
|
||||
* @Description
|
||||
* @date 2018/9/3 17:09
|
||||
*/
|
||||
public class ThreeDES {
|
||||
|
||||
private static final String IV = "1234567-";
|
||||
private final static String encoding = "utf-8";
|
||||
|
||||
/**
|
||||
* DESCBC加密
|
||||
*
|
||||
* @param src 数据源
|
||||
* @param key 密钥
|
||||
* @return 返回加密后的数据
|
||||
* @throws Exception
|
||||
*/
|
||||
public static String encryptDESCBC(final String src, final String key) throws Exception {
|
||||
if (key.length() < 24) {
|
||||
throw new InvalidKeyException("key的length不得小于24");
|
||||
}
|
||||
Key deskey = null;
|
||||
DESedeKeySpec spec = new DESedeKeySpec(key.getBytes());
|
||||
SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
|
||||
deskey = keyfactory.generateSecret(spec);
|
||||
|
||||
Cipher cipher = Cipher.getInstance("desede/CBC/PKCS5Padding");
|
||||
IvParameterSpec ips = new IvParameterSpec(IV.getBytes());
|
||||
cipher.init(Cipher.ENCRYPT_MODE, deskey, ips);
|
||||
byte[] encryptData = cipher.doFinal(src.getBytes(encoding));
|
||||
return Base64.encodeBase64URLSafeString(encryptData);
|
||||
}
|
||||
|
||||
/**
|
||||
* DESCBC解密
|
||||
*
|
||||
* @param src 数据源
|
||||
* @param key 密钥
|
||||
* @return 返回解密后的原始数据
|
||||
* @throws Exception
|
||||
*/
|
||||
public static String decryptDESCBC(final String src, final String key) throws Exception {
|
||||
if (key.length() < 24) {
|
||||
throw new InvalidKeyException("key的length不得小于24");
|
||||
}
|
||||
Key deskey = null;
|
||||
DESedeKeySpec spec = new DESedeKeySpec(key.getBytes());
|
||||
SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
|
||||
deskey = keyfactory.generateSecret(spec);
|
||||
Cipher cipher = Cipher.getInstance("desede/CBC/PKCS5Padding");
|
||||
IvParameterSpec ips = new IvParameterSpec(IV.getBytes());
|
||||
cipher.init(Cipher.DECRYPT_MODE, deskey, ips);
|
||||
|
||||
byte[] decryptData = cipher.doFinal(Base64.decodeBase64(src));
|
||||
|
||||
return new String(decryptData, encoding);
|
||||
}
|
||||
|
||||
/**
|
||||
* 填充,不是8的倍数会填充成8的倍数
|
||||
*
|
||||
* @param str
|
||||
* @return
|
||||
*/
|
||||
public static String padding(String str) {
|
||||
byte[] oldByteArray;
|
||||
try {
|
||||
oldByteArray = str.getBytes("UTF8");
|
||||
int numberToPad = 8 - oldByteArray.length % 8;
|
||||
byte[] newByteArray = new byte[oldByteArray.length + numberToPad];
|
||||
System.arraycopy(oldByteArray, 0, newByteArray, 0,
|
||||
oldByteArray.length);
|
||||
for (int i = oldByteArray.length; i < newByteArray.length; ++i) {
|
||||
newByteArray[i] = 0;
|
||||
}
|
||||
return new String(newByteArray, "UTF8");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
System.out.println("Crypter.padding UnsupportedEncodingException");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user