Przeglądaj źródła

OSS内网公共方法优化

lvy 5 dni temu
rodzic
commit
f19aa53fc2

+ 107 - 8
blade-common/src/main/java/org/springblade/common/utils/CommonUtil.java

@@ -1,12 +1,9 @@
 package org.springblade.common.utils;
 
 import cn.hutool.core.io.FileUtil;
-import cn.hutool.core.lang.func.Func;
-import cn.hutool.core.util.URLUtil;
 import cn.hutool.http.HttpUtil;
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
-import com.aliyuncs.utils.IOUtils;
 import com.drew.imaging.ImageMetadataReader;
 import com.drew.imaging.ImageProcessingException;
 import com.drew.metadata.Metadata;
@@ -16,21 +13,22 @@ import com.google.common.collect.Maps;
 import org.apache.commons.imaging.ImageReadException;
 import org.apache.commons.imaging.Imaging;
 import org.apache.commons.lang.StringUtils;
+import org.springblade.common.constant.CommonConstant;
 import org.springframework.util.CollectionUtils;
 
 import java.awt.*;
-import java.awt.color.ColorSpace;
-import java.awt.color.ICC_ColorSpace;
 import java.awt.geom.AffineTransform;
 import java.awt.image.AffineTransformOp;
 import java.awt.image.BufferedImage;
-import java.awt.image.ColorConvertOp;
 import java.io.*;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
 import java.math.BigDecimal;
 import java.net.*;
 import java.time.LocalDate;
 import java.util.*;
 import java.util.List;
+import java.util.concurrent.ConcurrentHashMap;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 import java.util.stream.Collectors;
@@ -54,6 +52,17 @@ import net.coobird.thumbnailator.Thumbnails;
 
 public class CommonUtil {
     private static final double INCH_TO_CM = 2.54;
+    private static Method paramCacheMethod = null;
+    private static Object paramCacheObj = null;
+    static {
+        try {
+            Class<?> cls = Class.forName("org.springblade.system.cache.ParamCache");
+            paramCacheObj = cls.newInstance();
+            paramCacheMethod = cls.getMethod("getValue", String.class);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
 
     public static Boolean checkBigDecimal(Object value) {
         try {
@@ -146,7 +155,7 @@ public class CommonUtil {
             System.out.println("----前-------"+urlStr);
             int lastIndex = urlStr.lastIndexOf("/") + 1;
             String fileName = urlStr.substring(lastIndex);
-            urlStr = urlStr.substring(0, lastIndex) + URLEncoder.encode(fileName, "UTF-8").replace("+", "%20");
+            urlStr = urlStr.substring(0, lastIndex) + URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20");
             //获取OSS文件流
             URL url = new URL(urlStr);
             URLConnection conn = url.openConnection();
@@ -168,7 +177,27 @@ public class CommonUtil {
             urlStr = replaceOssUrl(urlStr);
             int lastIndex = urlStr.lastIndexOf("/") + 1;
             String fileName = urlStr.substring(lastIndex);
-            urlStr = urlStr.substring(0, lastIndex) + URLEncoder.encode(fileName, "UTF-8").replace("+", "%20");
+            urlStr = urlStr.substring(0, lastIndex) + URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20");
+            //获取OSS文件流
+            URL url = new URL(urlStr);
+            URLConnection conn = url.openConnection();
+
+            conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
+            return conn.getInputStream();
+        } catch (Exception e) {
+            System.out.println("-----后------"+urlStr);
+            return null;
+        }
+    }
+    public static InputStream getOSSInputStream(String urlStr, int type) {
+        try {
+            System.out.println("----前-------"+urlStr);
+            if (type == 1) {
+                urlStr = replaceOssUrl(urlStr);
+            }
+            int lastIndex = urlStr.lastIndexOf("/") + 1;
+            String fileName = urlStr.substring(lastIndex);
+            urlStr = urlStr.substring(0, lastIndex) + URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20");
             //获取OSS文件流
             URL url = new URL(urlStr);
             URLConnection conn = url.openConnection();
@@ -998,9 +1027,79 @@ public class CommonUtil {
             } else {
                 url = url.replace("https://", "http://").replace("183.247.216.148", "152.168.2.15").replace(":9000//", ":9000/");
             }
+        } else {
+            getNetUrl(url);
         }
         return url;
     }
+    public static String getCacheValue(String key){
+        try {
+            if (paramCacheMethod != null && paramCacheObj != null) {
+                return (String) paramCacheMethod.invoke(paramCacheObj, key);
+            }
+        } catch (IllegalAccessException | InvocationTargetException e) {
+            e.printStackTrace();
+        }
+        return "";
+    }
+    public static String getNetUrl(String fileUrl) {
+        String filePath = getCacheValue(CommonConstant.SYS_LOCAL_URL);
+        String sysFileNetUrl = getCacheValue(CommonConstant.SYS_FILE_NET_URL);
+        String sysIsOnline = getCacheValue(CommonConstant.SYS_ISONLINE);
+        if (filePath.isEmpty() || sysFileNetUrl.isEmpty() || sysIsOnline.isEmpty()) {
+            return fileUrl;
+        }
+        String filePath2 = getSysLocalFileUrl(filePath, sysIsOnline);
+        if(fileUrl.contains("aliyuncs.com") || fileUrl.contains("xinan1.zos.ctyun.cn") || fileUrl.contains("/mnt/sdc/Users/hongchuangyanfa/Desktop/")){
+            if(fileUrl.contains("/mnt/sdc/Users/hongchuangyanfa/Desktop/")){
+                if(SystemUtils.isWindows() || SystemUtils.isMacOs()){
+                    filePath2 = filePath;
+                }else{
+                    return sysFileNetUrl + fileUrl.replaceAll("//", "/").replaceAll(filePath2, "");
+                }
+            }else{
+                return fileUrl;
+            }
+        } else {
+            if("20".equals(sysIsOnline)){
+                filePath2 = filePath;
+            }
+        }
+        String s1 = fileUrl.replaceAll("//", "/").replace("///","/");
+        filePath2= filePath2.replaceAll("//","/").replaceAll("///","/");
+        String s2= s1.replaceAll(filePath2, "");
+        return sysFileNetUrl + s2;
+    }
+    public static String getSysLocalFileUrl(String filePath, String sysIsOnline) {
+        if (sysIsOnline.equals("1")) { //正式环境
+            if (SystemUtils.isMacOs()) {
+                filePath = "/Users/hongchuangyanfa/Desktop/";
+            } else if (SystemUtils.isWindows()) {
+                filePath = "C://upload//";
+            }
+        } else if (sysIsOnline.equals("2")) { //109测试环境
+            if (SystemUtils.isMacOs()) {
+                filePath = "/www/wwwroot/Users/hongchuangyanfa/Desktop/";
+            } else if (SystemUtils.isWindows()) {
+                filePath = "C://upload//";
+            }
+        } else if (sysIsOnline.equals("20")) { //183
+            if (SystemUtils.isLinux()) {
+                filePath = "/home/www/wwwroot/Users/hongchuangyanfa/Desktop/";
+            } else if (SystemUtils.isMacOs()) {
+                filePath = "/Users/hongchuangyanfa/Desktop/";
+            } else if (SystemUtils.isWindows()) {
+                filePath = "C://upload//";
+            }
+        } else {  //本地环境
+            if (SystemUtils.isMacOs()) {
+                filePath = "/Users/hongchuangyanfa/Desktop/";
+            } else if (SystemUtils.isWindows()) {
+                filePath = "C://upload//";
+            }
+        }
+        return filePath;
+    }
 
     /**
      * webp文件转字节数组