Selaa lähdekoodia

Feature: 使用url下载图片

wudi 5 vuotta sitten
vanhempi
commit
d378638b44

+ 5 - 0
pom.xml

@@ -18,6 +18,11 @@
 	</properties>
 	<dependencies>
 		<dependency>
+			<groupId>commons-io</groupId>
+			<artifactId>commons-io</artifactId>
+			<version>2.6</version>
+		</dependency>
+		<dependency>
 			<groupId>org.springframework.boot</groupId>
 			<artifactId>spring-boot-starter-jdbc</artifactId>
 		</dependency>

+ 19 - 5
src/main/java/com/mokasz/image_pool/restful/controller/ImageController.java

@@ -3,6 +3,7 @@ package com.mokasz.image_pool.restful.controller;
 import com.mokasz.image_pool.algorithm.OpenCvService;
 import com.mokasz.image_pool.restful.entity.ImageQueryResponse;
 import com.mokasz.image_pool.restful.entity.ImageSaveResponse;
+import com.mokasz.image_pool.restful.service.DownloadBytes;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -27,12 +28,24 @@ public class ImageController {
     return new ImageSaveResponse("ok", 0);
   }
 
+  private byte[] getFileBytes (MultipartFile file, String url) throws IOException {
+    if (url != null) {
+      return DownloadBytes.downloadAsBytesArray(url);
+    } else if (file != null) {
+      logger.info("File size: {} Name: {}", file.getSize(), file.getOriginalFilename());
+      return file.getBytes();
+    } else {
+      throw new RuntimeException("缺少file或url");
+    }
+  }
+
   @PostMapping("/store")
   public ImageSaveResponse store(
-      @RequestParam("file") MultipartFile file, @RequestParam("pool") int pool) throws IOException {
-    logger.info("File size: {} Name: {}", file.getSize(), file.getOriginalFilename());
+      @RequestParam(value = "file", required = false) MultipartFile file,
+      @RequestParam(value = "url", required = false) String url,
+      @RequestParam("pool") int pool) throws IOException {
     float[] floats = new float[8];
-    service.calculatePHash(file.getBytes(), floats);
+    service.calculatePHash(this.getFileBytes(file, url), floats);
     float[] binaries = new float[64];
     service.transformBinaryArray(floats, binaries);
     logger.info("{}", binaries);
@@ -52,14 +65,15 @@ public class ImageController {
 
   @PostMapping("/match")
   public List<ImageQueryResponse> match(
-      @RequestParam("file") MultipartFile file,
+      @RequestParam(value = "file", required = false) MultipartFile file,
+      @RequestParam(value = "url", required = false) String url,
       @RequestParam("pool") int pool,
       @RequestParam(value = "max", defaultValue = "1") float max,
       @RequestParam(value = "min", defaultValue = "1") float min,
       @RequestParam(value = "limit", defaultValue = "10") int limit)
       throws IOException {
     float[] floats = new float[8];
-    service.calculatePHash(file.getBytes(), floats);
+    service.calculatePHash(this.getFileBytes(file, url), floats);
     float[] binaries = new float[64];
     service.transformBinaryArray(floats, binaries);
     logger.info("{}", floats);

+ 0 - 0
src/main/java/com/mokasz/image_pool/restful/service/.gitkeep


+ 12 - 0
src/main/java/com/mokasz/image_pool/restful/service/DownloadBytes.java

@@ -0,0 +1,12 @@
+package com.mokasz.image_pool.restful.service;
+
+import org.apache.commons.io.IOUtils;
+
+import java.io.IOException;
+import java.net.URL;
+
+public class DownloadBytes {
+    public static byte[] downloadAsBytesArray (String url) throws IOException {
+        return IOUtils.toByteArray(new URL(url));
+    }
+}