Ver código fonte

上传图片的接口 获取图片url的接口

tongfeng 2 anos atrás
pai
commit
91de993442

+ 1 - 0
java/sp-app/src/main/resources/application.properties

@@ -140,3 +140,4 @@ sp.minio.accesskey=minioadmin
 sp.minio.secretkey=minioadmin
 sp.minio.url=http://124.222.192.60:9000
 sp.minio.bucketName=mytest
+sp.minio.preview=https://fqgz.flowbb.cn

+ 3 - 1
java/sp-auth/src/main/java/com/anji/sp/config/SecurityConfig.java

@@ -99,7 +99,9 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
                   "/login/**",
                   "/phone/**", 
                   "/download/**", 
-                  "/upload/uploadMinio", 
+                  "/upload/uploadMinio",
+                  "/upload/uploadFaceImage",
+                  "/upload/getPreview",
                   "/captcha/**").anonymous()
                 .antMatchers(
                         HttpMethod.GET,

+ 36 - 0
java/sp-version/src/main/java/com/anji/sp/controller/SpFileUploadController.java

@@ -103,4 +103,40 @@ public class SpFileUploadController {
 		map.put("msg", "操作成功!");
 		return map;
 	}
+
+	@RequestMapping("/uploadFaceImage")
+	public Map<String, Object> uploadFaceImage(@RequestBody String image) {
+		JSONObject json = JSONObject.parseObject(image);
+		String imagePath="";
+		try {
+			imagePath = FileUploadUtils.saveImage(json.getString("base64"));
+		} catch (Exception e) {
+			e.printStackTrace();
+		}
+		// SUCCESS(100,"操作成功!"),
+		Map<String, Object> map = new HashMap<>();
+		map.put("code", 100);
+		map.put("msg", "操作成功!");
+		map.put("imagePath",imagePath);
+		return map;
+	}
+
+	@RequestMapping("/getPreview")
+	public Map<String,Object> getPreview(@RequestBody String fileKey){
+		JSONObject json = JSONObject.parseObject(fileKey);
+		String imagePath="";
+		try {
+			imagePath = FileUploadUtils.getPreviewUrl(json.getString("fileKey"));
+		} catch (Exception e) {
+			e.printStackTrace();
+		}
+		// SUCCESS(100,"操作成功!"),
+		Map<String, Object> map = new HashMap<>();
+		map.put("code", 100);
+		map.put("msg", "操作成功!");
+		map.put("url",imagePath);
+		return map;
+	}
+
+
 }

+ 16 - 2
java/sp-version/src/main/java/com/anji/sp/service/impl/MinioFileHandle.java

@@ -5,6 +5,7 @@ import java.io.InputStream;
 
 import javax.annotation.PostConstruct;
 
+import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.BeansException;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.context.ApplicationContext;
@@ -38,6 +39,11 @@ public class MinioFileHandle implements FileHandle,ApplicationContextAware {
 	@Value("${sp.minio.secretkey:minioadmin}")
 	public String appMinioSecretkey;
 
+	@Value("${sp.minio.preview:https://fqgz.flowbb.cn}")
+	protected String appMinioPreview;
+
+
+
 	private MinioClient minioClient;
 
 	@PostConstruct
@@ -131,8 +137,16 @@ public class MinioFileHandle implements FileHandle,ApplicationContextAware {
 
 	@Override
 	public String getPreviewUrl(String fileKey) throws Exception {
-		// TODO Auto-generated method stub
-		return null;
+		if (StringUtils.isNotBlank(fileKey) && (fileKey.startsWith("http") || fileKey.startsWith("https"))) {
+			return fileKey;
+		}
+		try {
+			String fileUrl = getObjectUrl(fileKey);
+			int index = fileUrl.indexOf("/", 8);
+			return appMinioPreview + fileUrl.substring(index);
+		} catch (Exception e) {
+			throw e;
+		}
 	}
 
 	@Override

+ 9 - 0
java/sp-version/src/main/java/com/anji/sp/util/file/FileUploadUtils.java

@@ -354,6 +354,15 @@ public class FileUploadUtils {
 		}
 	}
 
+	public static String getPreviewUrl(String fileKey) {
+		try {
+			return fileHandle.getPreviewUrl(fileKey);
+		} catch (Exception e) {
+			log.error("图片预览失败:" + e.getLocalizedMessage());
+			return null;
+		}
+	}
+
 	/**
 	 * 上传文件通过文件的流
 	 *