|
@@ -52,6 +52,7 @@ import org.springframework.util.ResourceUtils;
|
|
|
|
|
|
public class CommonUtil {
|
|
public class CommonUtil {
|
|
private static final double INCH_TO_CM = 2.54;
|
|
private static final double INCH_TO_CM = 2.54;
|
|
|
|
+
|
|
public static Boolean checkBigDecimal(Object value) {
|
|
public static Boolean checkBigDecimal(Object value) {
|
|
try {
|
|
try {
|
|
if (value != null && StringUtils.isNotEmpty(value.toString())) {
|
|
if (value != null && StringUtils.isNotEmpty(value.toString())) {
|
|
@@ -140,17 +141,20 @@ public class CommonUtil {
|
|
/**
|
|
/**
|
|
* 根据OSS文件路径获取文件输入流
|
|
* 根据OSS文件路径获取文件输入流
|
|
*/
|
|
*/
|
|
- public static synchronized InputStream getOSSInputStream(String urlStr) throws Exception {
|
|
|
|
- urlStr = replaceOssUrl(urlStr);
|
|
|
|
- //获取OSS文件流
|
|
|
|
- URL url =new URL(urlStr);
|
|
|
|
- final URLConnection conn = url.openConnection();
|
|
|
|
- conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
|
|
|
|
|
|
+ public static InputStream getOSSInputStream(String urlStr) {
|
|
try {
|
|
try {
|
|
|
|
+ urlStr = replaceOssUrl(urlStr);
|
|
|
|
+ //获取OSS文件流
|
|
|
|
+ URL url = new URL(urlStr);
|
|
|
|
+ URLConnection conn = url.openConnection();
|
|
|
|
+ // 设置连接超时时间
|
|
|
|
+ conn.setConnectTimeout(10000); // 5秒
|
|
|
|
+ // 设置读取超时时间
|
|
|
|
+ conn.setReadTimeout(10000); // 5秒
|
|
|
|
+ conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
|
|
return conn.getInputStream();
|
|
return conn.getInputStream();
|
|
- } catch (Exception e){
|
|
|
|
|
|
+ } catch (Exception e) {
|
|
System.out.println("zw-----------");
|
|
System.out.println("zw-----------");
|
|
- e.printStackTrace();
|
|
|
|
return null;
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -179,7 +183,7 @@ public class CommonUtil {
|
|
/**
|
|
/**
|
|
* 获取字节数组
|
|
* 获取字节数组
|
|
*/
|
|
*/
|
|
- public static synchronized byte[] InputStreamToBytes(InputStream is){
|
|
|
|
|
|
+ public static synchronized byte[] InputStreamToBytes(InputStream is) {
|
|
BufferedInputStream bis = new BufferedInputStream(is);
|
|
BufferedInputStream bis = new BufferedInputStream(is);
|
|
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
|
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
|
int date = -1;
|
|
int date = -1;
|
|
@@ -296,29 +300,30 @@ public class CommonUtil {
|
|
|
|
|
|
/**
|
|
/**
|
|
* Map拆分 (指定分组大小)
|
|
* Map拆分 (指定分组大小)
|
|
- * @param map Map
|
|
|
|
|
|
+ *
|
|
|
|
+ * @param map Map
|
|
* @param chunkSize 每个分组的大小 (>=1)
|
|
* @param chunkSize 每个分组的大小 (>=1)
|
|
- * @param <K> Key
|
|
|
|
- * @param <V> Value
|
|
|
|
|
|
+ * @param <K> Key
|
|
|
|
+ * @param <V> Value
|
|
* @return 子Map列表
|
|
* @return 子Map列表
|
|
*/
|
|
*/
|
|
- public static <K,V> List<Map<K,V>> splitByChunkSize(Map<K,V> map, int chunkSize){
|
|
|
|
- if(Objects.isNull(map) || map.isEmpty() || chunkSize<1){
|
|
|
|
|
|
+ public static <K, V> List<Map<K, V>> splitByChunkSize(Map<K, V> map, int chunkSize) {
|
|
|
|
+ if (Objects.isNull(map) || map.isEmpty() || chunkSize < 1) {
|
|
//空map或者分组大小<1,无法拆分
|
|
//空map或者分组大小<1,无法拆分
|
|
return Collections.emptyList();
|
|
return Collections.emptyList();
|
|
}
|
|
}
|
|
|
|
|
|
int mapSize = map.size(); //键值对总数
|
|
int mapSize = map.size(); //键值对总数
|
|
- int groupSize = mapSize/chunkSize + (mapSize%chunkSize==0?0:1); //计算分组个数
|
|
|
|
- List<Map<K,V>> list = Lists.newArrayListWithCapacity(groupSize); //子Map列表
|
|
|
|
|
|
+ int groupSize = mapSize / chunkSize + (mapSize % chunkSize == 0 ? 0 : 1); //计算分组个数
|
|
|
|
+ List<Map<K, V>> list = Lists.newArrayListWithCapacity(groupSize); //子Map列表
|
|
|
|
|
|
- if(chunkSize >= mapSize){ //只能分1组的情况
|
|
|
|
|
|
+ if (chunkSize >= mapSize) { //只能分1组的情况
|
|
list.add(map);
|
|
list.add(map);
|
|
return list;
|
|
return list;
|
|
}
|
|
}
|
|
|
|
|
|
int count = 0; //每个分组的组内计数
|
|
int count = 0; //每个分组的组内计数
|
|
- Map<K,V> subMap = Maps.newHashMapWithExpectedSize(chunkSize); //子Map
|
|
|
|
|
|
+ Map<K, V> subMap = Maps.newHashMapWithExpectedSize(chunkSize); //子Map
|
|
|
|
|
|
for (Map.Entry<K, V> entry : map.entrySet()) {
|
|
for (Map.Entry<K, V> entry : map.entrySet()) {
|
|
if (count < chunkSize) {
|
|
if (count < chunkSize) {
|
|
@@ -342,20 +347,21 @@ public class CommonUtil {
|
|
|
|
|
|
/**
|
|
/**
|
|
* Map拆分(指定分组个数)
|
|
* Map拆分(指定分组个数)
|
|
- * @param map Map
|
|
|
|
|
|
+ *
|
|
|
|
+ * @param map Map
|
|
* @param groupSize 分组个数 (>=1)
|
|
* @param groupSize 分组个数 (>=1)
|
|
- * @param <K> Key
|
|
|
|
- * @param <V> Value
|
|
|
|
|
|
+ * @param <K> Key
|
|
|
|
+ * @param <V> Value
|
|
* @return 子Map列表
|
|
* @return 子Map列表
|
|
*/
|
|
*/
|
|
- public static <K,V> List<Map<K,V>> splitByGroupSize(Map<K,V> map, int groupSize){
|
|
|
|
- if(Objects.isNull(map) || map.isEmpty() || groupSize<1){
|
|
|
|
|
|
+ public static <K, V> List<Map<K, V>> splitByGroupSize(Map<K, V> map, int groupSize) {
|
|
|
|
+ if (Objects.isNull(map) || map.isEmpty() || groupSize < 1) {
|
|
//空map或者分组数<1,无法拆分
|
|
//空map或者分组数<1,无法拆分
|
|
return Collections.emptyList();
|
|
return Collections.emptyList();
|
|
}
|
|
}
|
|
|
|
|
|
- List<Map<K,V>> list = Lists.newArrayListWithCapacity(groupSize);
|
|
|
|
- if(groupSize == 1){ //只有1个分组的情况
|
|
|
|
|
|
+ List<Map<K, V>> list = Lists.newArrayListWithCapacity(groupSize);
|
|
|
|
+ if (groupSize == 1) { //只有1个分组的情况
|
|
list.add(map);
|
|
list.add(map);
|
|
return list;
|
|
return list;
|
|
}
|
|
}
|
|
@@ -365,22 +371,22 @@ public class CommonUtil {
|
|
int restCount = mapSize % groupSize; //平均后剩余的键值对数
|
|
int restCount = mapSize % groupSize; //平均后剩余的键值对数
|
|
int chunkSize0 = mapSize / groupSize; //每个分组键值对数量
|
|
int chunkSize0 = mapSize / groupSize; //每个分组键值对数量
|
|
int chunkSize1 = chunkSize0 + 1; //多分一个
|
|
int chunkSize1 = chunkSize0 + 1; //多分一个
|
|
- int chunkSize = chunkIndex<restCount ? chunkSize1 : chunkSize0; //实际每组的大小(前面的部分分组可能会多分1个)
|
|
|
|
|
|
+ int chunkSize = chunkIndex < restCount ? chunkSize1 : chunkSize0; //实际每组的大小(前面的部分分组可能会多分1个)
|
|
int count = 0; //每个分组的组内计数
|
|
int count = 0; //每个分组的组内计数
|
|
- Map<K,V> subMap = Maps.newHashMapWithExpectedSize(chunkSize);//子Map
|
|
|
|
|
|
+ Map<K, V> subMap = Maps.newHashMapWithExpectedSize(chunkSize);//子Map
|
|
|
|
|
|
for (Map.Entry<K, V> entry : map.entrySet()) {
|
|
for (Map.Entry<K, V> entry : map.entrySet()) {
|
|
- if(count < chunkSize){
|
|
|
|
|
|
+ if (count < chunkSize) {
|
|
//每个分组按实际分组大小(chunkSize)加入键值对
|
|
//每个分组按实际分组大小(chunkSize)加入键值对
|
|
subMap.put(entry.getKey(), entry.getValue());
|
|
subMap.put(entry.getKey(), entry.getValue());
|
|
- count ++; //组内计数+1
|
|
|
|
- }else{
|
|
|
|
|
|
+ count++; //组内计数+1
|
|
|
|
+ } else {
|
|
//结束上一个分组
|
|
//结束上一个分组
|
|
list.add(subMap); //当前分组装满了->加入列表
|
|
list.add(subMap); //当前分组装满了->加入列表
|
|
- chunkIndex ++; //分组个数+1
|
|
|
|
|
|
+ chunkIndex++; //分组个数+1
|
|
|
|
|
|
//开始下一个分组
|
|
//开始下一个分组
|
|
- chunkSize = chunkIndex<restCount ? chunkSize1 : chunkSize0; //重新计算分组大小
|
|
|
|
|
|
+ chunkSize = chunkIndex < restCount ? chunkSize1 : chunkSize0; //重新计算分组大小
|
|
subMap = Maps.newHashMapWithExpectedSize(chunkSize); //新的分组
|
|
subMap = Maps.newHashMapWithExpectedSize(chunkSize); //新的分组
|
|
subMap.put(entry.getKey(), entry.getValue()); //添加当前键值对
|
|
subMap.put(entry.getKey(), entry.getValue()); //添加当前键值对
|
|
count = 1; //组内计数重置为1
|
|
count = 1; //组内计数重置为1
|
|
@@ -447,14 +453,14 @@ public class CommonUtil {
|
|
* @param filesPath
|
|
* @param filesPath
|
|
* @throws Exception
|
|
* @throws Exception
|
|
*/
|
|
*/
|
|
- public static void packageZip(String filesPath,String zipUrl) throws Exception {
|
|
|
|
|
|
+ public static void packageZip(String filesPath, String zipUrl) throws Exception {
|
|
// 要被压缩的文件夹
|
|
// 要被压缩的文件夹
|
|
File file = new File(filesPath); //需要压缩的文件夹
|
|
File file = new File(filesPath); //需要压缩的文件夹
|
|
File folder = new File(zipUrl);
|
|
File folder = new File(zipUrl);
|
|
if (!folder.exists() && !folder.isDirectory()) {
|
|
if (!folder.exists() && !folder.isDirectory()) {
|
|
folder.mkdirs();
|
|
folder.mkdirs();
|
|
}
|
|
}
|
|
- File zipFile = new File(zipUrl+"/" + "localArchive.zip"); //放于和需要压缩的文件夹同级目录
|
|
|
|
|
|
+ File zipFile = new File(zipUrl + "/" + "localArchive.zip"); //放于和需要压缩的文件夹同级目录
|
|
ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
|
|
ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
|
|
isDirectory(file, zipOut, "", true); //判断是否为文件夹
|
|
isDirectory(file, zipOut, "", true); //判断是否为文件夹
|
|
zipOut.close();
|
|
zipOut.close();
|
|
@@ -518,8 +524,8 @@ public class CommonUtil {
|
|
*/
|
|
*/
|
|
|
|
|
|
public static void main(String[] args) throws IOException {
|
|
public static void main(String[] args) throws IOException {
|
|
- // String imgurl = "/Users/hongchuangyanfa/Desktop/excel/432123.jpg";
|
|
|
|
- // String imgurl = "/Users/hongchuangyanfa/Desktop/excel/123.jpg";
|
|
|
|
|
|
+ // String imgurl = "/Users/hongchuangyanfa/Desktop/excel/432123.jpg";
|
|
|
|
+ // String imgurl = "/Users/hongchuangyanfa/Desktop/excel/123.jpg";
|
|
String imgurl = "/Users/hongchuangyanfa/Downloads/bccb09f5e2caa85611d380f596d4febb.jpg";
|
|
String imgurl = "/Users/hongchuangyanfa/Downloads/bccb09f5e2caa85611d380f596d4febb.jpg";
|
|
File file = ResourceUtils.getFile(imgurl);
|
|
File file = ResourceUtils.getFile(imgurl);
|
|
|
|
|
|
@@ -547,7 +553,7 @@ public class CommonUtil {
|
|
// 需要旋转图片
|
|
// 需要旋转图片
|
|
// 1 无需纠正 2 水平翻转(镜像)3 垂直翻转(旋转180°) 4 水平翻转+垂直翻转 5 水平翻转+旋转90°
|
|
// 1 无需纠正 2 水平翻转(镜像)3 垂直翻转(旋转180°) 4 水平翻转+垂直翻转 5 水平翻转+旋转90°
|
|
// 6 旋转90° 7 水平翻转+旋转270° 8 +旋转270°
|
|
// 6 旋转90° 7 水平翻转+旋转270° 8 +旋转270°
|
|
- if(orientation > 1) {
|
|
|
|
|
|
+ if (orientation > 1) {
|
|
BufferedImage originalImage = ImageIO.read(new ByteArrayInputStream(imageData));
|
|
BufferedImage originalImage = ImageIO.read(new ByteArrayInputStream(imageData));
|
|
AffineTransform transform = new AffineTransform();
|
|
AffineTransform transform = new AffineTransform();
|
|
if (orientation == 3) {
|
|
if (orientation == 3) {
|
|
@@ -635,7 +641,7 @@ public class CommonUtil {
|
|
ByteArrayInputStream bais = new ByteArrayInputStream(imageData);
|
|
ByteArrayInputStream bais = new ByteArrayInputStream(imageData);
|
|
BufferedImage originalImage = ImageIO.read(bais);
|
|
BufferedImage originalImage = ImageIO.read(bais);
|
|
long sizeLimit = 512000; //358KB
|
|
long sizeLimit = 512000; //358KB
|
|
- int width = originalImage.getWidth() ;
|
|
|
|
|
|
+ int width = originalImage.getWidth();
|
|
int height = originalImage.getHeight();
|
|
int height = originalImage.getHeight();
|
|
Image scaledImage = originalImage.getScaledInstance(width, height, Image.SCALE_SMOOTH);
|
|
Image scaledImage = originalImage.getScaledInstance(width, height, Image.SCALE_SMOOTH);
|
|
BufferedImage resizedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
|
|
BufferedImage resizedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
|
|
@@ -659,8 +665,8 @@ public class CommonUtil {
|
|
originalImage = ImageIO.read(bais);
|
|
originalImage = ImageIO.read(bais);
|
|
float width2 = originalImage.getWidth() * quality;
|
|
float width2 = originalImage.getWidth() * quality;
|
|
float height2 = originalImage.getHeight() * quality;
|
|
float height2 = originalImage.getHeight() * quality;
|
|
- scaledImage = originalImage.getScaledInstance(width, height, Image.SCALE_SMOOTH);
|
|
|
|
- resizedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
|
|
|
|
|
|
+ scaledImage = originalImage.getScaledInstance(width, height, Image.SCALE_SMOOTH);
|
|
|
|
+ resizedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
|
|
resizedImage.getGraphics().drawImage(scaledImage, 0, 0, null);
|
|
resizedImage.getGraphics().drawImage(scaledImage, 0, 0, null);
|
|
|
|
|
|
// 压缩图像
|
|
// 压缩图像
|
|
@@ -704,10 +710,11 @@ public class CommonUtil {
|
|
|
|
|
|
/**
|
|
/**
|
|
* 根据起止日期获取工作日
|
|
* 根据起止日期获取工作日
|
|
|
|
+ *
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
- public static int getWorkDays(LocalDate startTime,LocalDate endTime){
|
|
|
|
- if (startTime.compareTo(endTime) > 0){
|
|
|
|
|
|
+ public static int getWorkDays(LocalDate startTime, LocalDate endTime) {
|
|
|
|
+ if (startTime.compareTo(endTime) > 0) {
|
|
return -1;
|
|
return -1;
|
|
}
|
|
}
|
|
// if (startTime.compareTo(endTime) == 0){
|
|
// if (startTime.compareTo(endTime) == 0){
|
|
@@ -715,12 +722,12 @@ public class CommonUtil {
|
|
// }
|
|
// }
|
|
StringBuilder str = new StringBuilder();
|
|
StringBuilder str = new StringBuilder();
|
|
List<String> list = new ArrayList<>();
|
|
List<String> list = new ArrayList<>();
|
|
- while (!startTime.equals(endTime)){
|
|
|
|
- str.append("d="+startTime+"&");
|
|
|
|
|
|
+ while (!startTime.equals(endTime)) {
|
|
|
|
+ str.append("d=" + startTime + "&");
|
|
list.add(startTime.toString());
|
|
list.add(startTime.toString());
|
|
startTime = startTime.plusDays(1L);
|
|
startTime = startTime.plusDays(1L);
|
|
}
|
|
}
|
|
- str.append("d="+endTime+"&");
|
|
|
|
|
|
+ str.append("d=" + endTime + "&");
|
|
list.add(endTime.toString());
|
|
list.add(endTime.toString());
|
|
str.append("type=Y");
|
|
str.append("type=Y");
|
|
String post = HttpUtil.get("http://timor.tech/api/holiday/batch?" + str.toString());
|
|
String post = HttpUtil.get("http://timor.tech/api/holiday/batch?" + str.toString());
|
|
@@ -730,7 +737,7 @@ public class CommonUtil {
|
|
for (String s : list) {
|
|
for (String s : list) {
|
|
Map map = JSONObject.parseObject(jsonObject.get(s).toString(), Map.class);
|
|
Map map = JSONObject.parseObject(jsonObject.get(s).toString(), Map.class);
|
|
int type = (int) map.get("type");
|
|
int type = (int) map.get("type");
|
|
- if (type == 0 || type == 3){
|
|
|
|
|
|
+ if (type == 0 || type == 3) {
|
|
workDays++;
|
|
workDays++;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -738,24 +745,21 @@ public class CommonUtil {
|
|
}
|
|
}
|
|
|
|
|
|
public static String replaceOssUrl(String url) {
|
|
public static String replaceOssUrl(String url) {
|
|
- /* if(url.indexOf("-internal")<0 && url.indexOf(".aliyuncs.com")>=0) {
|
|
|
|
- // 如果当前环境变量不包含linuxtesttest,则替换URL中的oss路径
|
|
|
|
- url = url.replace(".aliyuncs.com", "-internal.aliyuncs.com");
|
|
|
|
- }*/
|
|
|
|
//本地部署- 甬台温
|
|
//本地部署- 甬台温
|
|
- if (url.indexOf("183.247.216.148")>=0) {
|
|
|
|
|
|
+ if (url.indexOf("183.247.216.148") >= 0 || url.indexOf("152.168.2.15") >= 0) {
|
|
// 如果当前环境变量不包含linuxtesttest,则替换URL中的oss路径
|
|
// 如果当前环境变量不包含linuxtesttest,则替换URL中的oss路径
|
|
- if(SystemUtils.isMacOs() || SystemUtils.isWindows()){
|
|
|
|
- url = url.replace("https://","http://").replace(":9000//",":9000/");
|
|
|
|
- }else{
|
|
|
|
- url = url.replace("https://","http://").replace("183.247.216.148","152.168.2.15").replace(":9000//",":9000/");
|
|
|
|
|
|
+ if (SystemUtils.isMacOs() || SystemUtils.isWindows()) {
|
|
|
|
+ url = url.replace("https://", "http://").replace(":9000//", ":9000/");
|
|
|
|
+ } else {
|
|
|
|
+ url = url.replace("https://", "http://").replace("183.247.216.148", "152.168.2.15").replace(":9000//", ":9000/");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return url;
|
|
return url;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
- * webp文件转字节数组
|
|
|
|
|
|
+ * webp文件转字节数组
|
|
|
|
+ *
|
|
* @param
|
|
* @param
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
@@ -763,8 +767,8 @@ public class CommonUtil {
|
|
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
|
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
|
try {
|
|
try {
|
|
BufferedImage webpImage = ImageIO.read(inputStream);
|
|
BufferedImage webpImage = ImageIO.read(inputStream);
|
|
- ImageIO.write(webpImage,"png",bos);
|
|
|
|
- }catch (Exception e){
|
|
|
|
|
|
+ ImageIO.write(webpImage, "png", bos);
|
|
|
|
+ } catch (Exception e) {
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
return bos.toByteArray();
|
|
return bos.toByteArray();
|
|
@@ -775,6 +779,7 @@ public class CommonUtil {
|
|
double pixelsPerCm = 90 / INCH_TO_CM;
|
|
double pixelsPerCm = 90 / INCH_TO_CM;
|
|
return (int) Math.round(cm * pixelsPerCm);
|
|
return (int) Math.round(cm * pixelsPerCm);
|
|
}
|
|
}
|
|
|
|
+
|
|
// 图片厘米转像素
|
|
// 图片厘米转像素
|
|
public static double pxToCm(int px) {
|
|
public static double pxToCm(int px) {
|
|
double cmPerPixel = INCH_TO_CM / 90;
|
|
double cmPerPixel = INCH_TO_CM / 90;
|