Browse Source

2021年6月9日-配置整改

zhujinghui 4 năm trước cách đây
mục cha
commit
a875cf62fd

+ 49 - 0
4dkankan-user-common/src/main/java/com/fdkankan/common/util/FileUtils.java

@@ -0,0 +1,49 @@
+package com.fdkankan.common.util;
+
+import lombok.extern.slf4j.Slf4j;
+import sun.misc.BASE64Encoder;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.HttpURLConnection;
+import java.net.URL;
+
+@Slf4j
+public class FileUtils {
+
+    public static String getBase64ContentFromUrl(String urlStr) throws IOException {
+        URL url = new URL(urlStr);
+        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
+        // 设置超时间为3秒
+        conn.setConnectTimeout(3 * 1000);
+        // 防止屏蔽程序抓取而返回403错误
+        conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
+
+        // 得到输入流
+        InputStream inputStream = conn.getInputStream();
+        // 获取自己数组
+        byte[] data = readInputStream(inputStream);
+
+        return new BASE64Encoder().encode(data);
+    }
+
+    /**
+     * 从输入流中获取字节数组
+     *
+     * @param inputStream
+     * @return
+     * @throws IOException
+     */
+    private static byte[] readInputStream(InputStream inputStream) throws IOException {
+        byte[] buffer = new byte[1024];
+        int len = 0;
+        ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        while ((len = inputStream.read(buffer)) != -1) {
+            bos.write(buffer, 0, len);
+        }
+        bos.close();
+        return bos.toByteArray();
+    }
+
+}

+ 11 - 10
4dkankan-user-web/src/main/java/com/fdkankan/web/backend/IndexController.java

@@ -4,10 +4,7 @@ import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
 import com.fdkankan.common.constant.MsgCode;
 import com.fdkankan.common.exception.BaseRuntimeException;
-import com.fdkankan.common.util.Base64Converter;
-import com.fdkankan.common.util.PasswordUtils;
-import com.fdkankan.common.util.RandomValidateCodeUtil;
-import com.fdkankan.common.util.Result;
+import com.fdkankan.common.util.*;
 import com.fdkankan.domain.backend.DepartmentEntity;
 import com.fdkankan.domain.backend.RoleEntity;
 import com.fdkankan.domain.backend.UserEntity;
@@ -31,10 +28,12 @@ import org.apache.commons.codec.binary.Base64;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.transaction.annotation.Transactional;
+import org.springframework.util.ObjectUtils;
 import org.springframework.web.bind.annotation.*;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
 import java.util.Date;
 import java.util.HashMap;
 import java.util.List;
@@ -69,6 +68,7 @@ public class IndexController {
     private OperatorFeign operatorFeign;
 
 
+
     @ApiOperation("用户登陆")
     @PostMapping(value = "/login")
     @ApiImplicitParams({
@@ -266,12 +266,13 @@ public class IndexController {
     @ApiOperation("返回url base64编码")
     @GetMapping("/getEncodeUrl")
     public Result getEncodeUrl(HttpServletRequest request, HttpServletResponse response) {
-        String url = request.getParameter("url");
-        byte[] b = url.getBytes();
-        org.apache.commons.codec.binary.Base64 base64 = new Base64();
-        b = base64.encode(b);
-        String base64Url = new String(b);
-        return Result.success("success", base64Url);
+        String fileUrl = request.getParameter("url");
+        try {
+            return Result.success("success", FileUtils.getBase64ContentFromUrl(fileUrl));
+        } catch (IOException e) {
+            log.error("base64编码转码报错", e);
+            return Result.failure(1, "转码异常");
+        }
     }