elis 2 éve
szülő
commit
7d31a922c3

+ 30 - 9
nngkxxdp/src/main/java/com/example/nngkxxdp/program/controller/HLWIntefaceController.java

@@ -1,31 +1,27 @@
 package com.example.nngkxxdp.program.controller;
 
-import cn.hutool.core.convert.Convert;
-import cn.hutool.core.date.DateUtil;
+
 import cn.hutool.core.util.ObjectUtil;
 import cn.hutool.core.util.StrUtil;
 import cn.hutool.json.JSONObject;
 import com.example.nngkxxdp.hlw.HlwHttpUtil;
 import com.example.nngkxxdp.program.constant.MiniConstant;
 import com.example.nngkxxdp.program.service.HLWIntefaceService;
-import com.example.nngkxxdp.util.RedisUtil;
 import com.example.nngkxxdp.util.SendUtil;
 import lombok.RequiredArgsConstructor;
-import org.springframework.beans.factory.annotation.Value;
 import org.springframework.core.io.ByteArrayResource;
+import org.springframework.core.io.Resource;
 import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.http.HttpHeaders;
 import org.springframework.http.MediaType;
 import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.*;
 
-import javax.annotation.Resource;
+import java.io.ByteArrayOutputStream;
 import java.io.InputStream;
 import java.net.URL;
 import java.util.Map;
 
-import static jdk.nashorn.internal.objects.NativeError.getFileName;
-import static org.apache.poi.util.IOUtils.toByteArray;
 
 @RestController
 @RequestMapping("mini/hlw")
@@ -129,8 +125,7 @@ public class HLWIntefaceController {
         InputStream inputStream;
         try {
             inputStream = new URL(url).openStream();
-
-            resource = (Resource) new ByteArrayResource(toByteArray(inputStream));
+            resource = new ByteArrayResource(toByteArray(inputStream));
         } catch (Exception e) {
             e.printStackTrace();
         }
@@ -139,4 +134,30 @@ public class HLWIntefaceController {
                 .header(HttpHeaders.CONTENT_DISPOSITION, " filename=\"" + getFileName(url) + "\"")
                 .body(resource);
     }
+
+    private byte[] toByteArray(InputStream in){
+        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
+        try {
+            byte[] bytes = new byte[1024];
+            int n;
+            while ((n = in.read(bytes)) != -1){
+                byteArrayOutputStream.write(bytes, 0, n);
+            }
+            System.out.println(byteArrayOutputStream.size());
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return byteArrayOutputStream.toByteArray();
+    }
+
+    private String getFileName(String url){
+        String[] strs = url.split("/");
+        for (String str: strs){
+            // 这里我仅处理png格式的,根据需求修改即可
+            if (str.toLowerCase().endsWith(".png")){
+                return str;
+            }
+        }
+        return null;
+    }
 }