15143018065 hace 1 semana
padre
commit
77e6effc5a

+ 6 - 5
ruoyi-modules/mz-gljt/src/main/java/com/ruoyi/gljt/utils/ObsUtils.java

@@ -29,11 +29,9 @@ public class ObsUtils {
                 File file = new File(filePath);
                 FileInputStream fileInputStream = new FileInputStream(file);
                 inputStream = new BufferedInputStream(fileInputStream);
-                if (StringUtils.equals(SpringUtil.getActiveProfile(), "gljt") || StringUtils.equals(SpringUtil.getActiveProfile(), "dev")) {
-                    byte[] in = IOUtils.toByteArray(inputStream);
-                    byte[] up = ShiftCryptoUtils.shiftDecrypt(in);
-                    inputStream = new ByteArrayInputStream(up);
-                }
+                byte[] in = IOUtils.toByteArray(inputStream);
+                byte[] up = ShiftCryptoUtils.shiftDecrypt(in);
+                inputStream = new ByteArrayInputStream(up);
                 res = Base64Utils.inputStream2Base64(inputStream);
             } catch (Exception e) {
                 e.printStackTrace();
@@ -54,6 +52,9 @@ public class ObsUtils {
                 System.out.println("取得结果 =======》 " + obsObject.getBucketName());
                 try {
                     inputStream = obsObject.getObjectContent();
+                    byte[] in = IOUtils.toByteArray(inputStream);
+                    byte[] up = ShiftCryptoUtils.shiftDecrypt(in);
+                    inputStream = new ByteArrayInputStream(up);
                     res = Base64Utils.inputStream2Base64(inputStream);
                     System.out.println("转换为base64结束 =======》 " + obsObject.getBucketName());
                 } catch (Exception e) {

+ 154 - 18
ruoyi-modules/ruoyi-file/src/main/java/com/ruoyi/file/controller/SysFileController.java

@@ -75,20 +75,6 @@ public class SysFileController
     @PostMapping("upload")
     public R<SysFile> upload(MultipartFile file) {
         if (!StringUtils.equals(SpringUtil.getActiveProfile(), "prod")) {
-            if (StringUtils.equals(SpringUtil.getActiveProfile(), "gljt") || StringUtils.equals(SpringUtil.getActiveProfile(), "dev")) {
-                try {
-                    // 上传并返回访问地址
-                    MultipartFile shift = ShiftCryptoUtils.encryptMultipartFile(file);
-                    String url = sysFileService.uploadFile(shift);
-                    SysFile sysFile = new SysFile();
-                    sysFile.setName(FileUtils.getName(url));
-                    sysFile.setUrl(url);
-                    return R.ok(sysFile);
-                } catch (Exception e) {
-                    log.error("上传文件失败", e);
-                    return R.fail(e.getMessage());
-                }
-            }
             try {
                 // 上传并返回访问地址
                 String url = sysFileService.uploadFile(file);
@@ -135,6 +121,61 @@ public class SysFileController
         }
     }
 
+    @PostMapping("gljtUpload")
+    public R<SysFile> gljtUpload(MultipartFile file) {
+        MultipartFile shift;
+        try {
+            shift = ShiftCryptoUtils.encryptMultipartFile(file);
+        } catch (Exception e) {
+            return R.fail(e.getMessage());
+        }
+        if (!StringUtils.equals(SpringUtil.getActiveProfile(), "prod")) {
+            try {
+                // 上传并返回访问地址
+                String url = sysFileService.uploadFile(shift);
+                SysFile sysFile = new SysFile();
+                sysFile.setName(FileUtils.getName(url));
+                sysFile.setUrl(replaceFirstOccurrence(url, "uploadPath", "gljtUploadPath"));
+                return R.ok(sysFile);
+            } catch (Exception e) {
+                log.error("上传文件失败", e);
+                return R.fail(e.getMessage());
+            }
+        } else {
+            SysFile sysFile = new SysFile();
+            ObsClient obsClient = null;
+            String objectKey = FileUploadUtils.extractFilename(shift);
+            if (StringUtils.isNotEmpty(objectKey)) {
+                if (StringUtils.equals(objectKey.substring(0, 1), "/")) {
+                    objectKey = objectKey.substring(1);
+                }
+            }
+            try {
+                // 上传并返回访问地址
+                obsClient = new ObsClient(obsConfig.getAK(), obsConfig.getSK(), obsConfig.getENDPOINT());
+                PutObjectResult putObjectResult = obsClient.putObject(obsConfig.getBUCKET_NAME(),
+                        "mzylfwobs/uploadPath/" + objectKey, shift.getInputStream());
+                String[] arr = putObjectResult.getObjectKey().split("/");
+                String url = putObjectResult.getObjectKey().replaceAll("mzylfwobs", "");
+                sysFile.setName(arr[arr.length - 1]);
+                sysFile.setUrl(replaceFirstOccurrence(url, "uploadPath", "gljtUploadPath"));
+                return R.ok(sysFile);
+            } catch (Exception e) {
+                log.error("上传文件失败", e);
+                return R.fail(e.getMessage());
+            } finally {
+                if (ObjectUtils.isNotEmpty(obsClient)) {
+                    try {
+                        // 关闭OBS连接
+                        obsClient.close();
+                    } catch (IOException e) {
+                        log.error("OBS关闭连接报错!===============" + e.getMessage());
+                    }
+                }
+            }
+        }
+    }
+
     @PostMapping("uploadMul")
     public R<SysFile> uploadMul(@RequestBody JSONObject fileStr) {
         JSONArray fileArray = new JSONArray();
@@ -342,6 +383,32 @@ public class SysFileController
         }
     }
 
+    @GetMapping("/gljtUploadPath/{year}/{month}/{day}/{fileName}")
+    public void gljtUploadPath(@PathVariable("year") String year, @PathVariable("month") String month,
+                              @PathVariable("day") String day, @PathVariable("fileName") String fileName, HttpServletResponse resp) {
+        if (!StringUtils.equals(SpringUtil.getActiveProfile(), "prod")) {
+            try {
+                fileName = URLDecoder.decode(fileName, "utf-8");
+                String filePath = localFilePath + "/" + year + "/" + month + "/" + day + "/" + fileName;
+                System.out.println("本地路径<<<<======>>>>" + filePath);
+                buildResponse(resp, fileName);
+                gljtConvertOutputStream(resp, filePath);
+            } catch (Exception e) {
+                log.error("取得本地文件失败==》", e);
+            }
+        } else {
+            try {
+                fileName = URLDecoder.decode(fileName, "utf-8");
+                String objectKey = "mzylfwobs/uploadPath/" + year + "/" + month + "/" + day + "/" + fileName;
+                System.out.println("本地路径<<<<======>>>>" + objectKey);
+                buildResponse(resp, fileName);
+                gljtConvertOutputStreamObs(resp, obsConfig.getBUCKET_NAME(), objectKey);
+            } catch (Exception e) {
+                log.error("取得OBS文件失败==》", e);
+            }
+        }
+    }
+
     @GetMapping("/uploadPath/{year}/{month}/{fileName}")
     public void commonPreview(@PathVariable("year") String year, @PathVariable("month") String month,
                               @PathVariable("fileName") String fileName, HttpServletResponse resp) {
@@ -427,11 +494,34 @@ public class SysFileController
             File file = new File(filePath);
             FileInputStream fileInputStream = new FileInputStream(file);
             inputStream = new BufferedInputStream(fileInputStream);
-            if (StringUtils.equals(SpringUtil.getActiveProfile(), "gljt") || StringUtils.equals(SpringUtil.getActiveProfile(), "dev")) {
-                byte[] in = IOUtils.toByteArray(inputStream);
-                byte[] up = ShiftCryptoUtils.shiftDecrypt(in);
-                inputStream = new ByteArrayInputStream(up);
+            byteArrayOutputStream = new ByteArrayOutputStream();
+            int len;
+            b = new byte[1024];
+            while ((len = inputStream.read(b)) != -1) {
+                byteArrayOutputStream.write(b, 0, len);
             }
+        } catch (Exception e) {
+            e.printStackTrace();
+        } finally {
+            assert inputStream != null;
+            inputStream.close();
+        }
+        assert byteArrayOutputStream != null;
+        byteArrayOutputStream.writeTo(response.getOutputStream());
+        byteArrayOutputStream.close();
+    }
+
+    public void gljtConvertOutputStream(HttpServletResponse response, String filePath) throws IOException {
+        ByteArrayOutputStream byteArrayOutputStream = null;
+        InputStream inputStream = null;
+        byte[] b;
+        try {
+            File file = new File(filePath);
+            FileInputStream fileInputStream = new FileInputStream(file);
+            inputStream = new BufferedInputStream(fileInputStream);
+            byte[] in = IOUtils.toByteArray(inputStream);
+            byte[] up = ShiftCryptoUtils.shiftDecrypt(in);
+            inputStream = new ByteArrayInputStream(up);
             byteArrayOutputStream = new ByteArrayOutputStream();
             int len;
             b = new byte[1024];
@@ -474,6 +564,34 @@ public class SysFileController
         byteArrayOutputStream.close();
     }
 
+    public void gljtConvertOutputStreamObs(HttpServletResponse response, String bucketName, String objectKey) throws IOException {
+        ObsClient obsClient = new ObsClient(obsConfig.getAK(), obsConfig.getSK(), obsConfig.getENDPOINT());
+        ObsObject obsObject = obsClient.getObject(bucketName,objectKey, null);
+        ByteArrayOutputStream byteArrayOutputStream = null;
+        InputStream inputStream = null;
+        byte[] b;
+        try {
+            inputStream = obsObject.getObjectContent();
+            byte[] in = IOUtils.toByteArray(inputStream);
+            byte[] up = ShiftCryptoUtils.shiftDecrypt(in);
+            inputStream = new ByteArrayInputStream(up);
+            byteArrayOutputStream = new ByteArrayOutputStream();
+            int len;
+            b = new byte[1024];
+            while ((len = inputStream.read(b)) != -1) {
+                byteArrayOutputStream.write(b, 0, len);
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        } finally {
+            assert inputStream != null;
+            inputStream.close();
+        }
+        assert byteArrayOutputStream != null;
+        byteArrayOutputStream.writeTo(response.getOutputStream());
+        byteArrayOutputStream.close();
+    }
+
     public void buildResponse(HttpServletResponse response, String fileName) throws UnsupportedEncodingException {
         int index = fileName.lastIndexOf(".");
         char[] ch = fileName.toCharArray();
@@ -581,4 +699,22 @@ public class SysFileController
         }
     }
 
+    /**
+     * 替换字符串中第一次出现的指定子串
+     * @param original 原始字符串
+     * @param target 需要替换的目标子串
+     * @param replacement 替换内容
+     * @return 替换后的新字符串
+     */
+    public static String replaceFirstOccurrence(String original, String target, String replacement) {
+        int index = original.indexOf(target);
+        if (index == -1) {
+            // 未找到目标子串时返回原始字符串
+            return original;
+        }
+        // 拼接新字符串:前半部分 + 替换内容 + 剩余部分
+        return original.substring(0, index)
+                + replacement
+                + original.substring(index + target.length());
+    }
 }

+ 3 - 0
ruoyi-ui-gljt/nginx.conf

@@ -39,6 +39,9 @@ http {
         location /uploadPath/ {
            proxy_pass http://172.17.0.1:9090/file/uploadPath/;
         }
+        location /gljtUploadPath/ {
+           proxy_pass http://172.17.0.1:9090/file/gljtUploadPath/;
+        }
         location /baiduApi/ {
                    proxy_pass https://aip.baidubce.com/;
         }