From f9aa90a61934d729376498699c1c8c28229f3845 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A1=BE=E6=8C=BD?= Date: Sun, 27 Oct 2024 14:41:37 +0800 Subject: [PATCH] =?UTF-8?q?=E9=A1=BE=E6=8C=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- login/pom.xml | 4 +- .../controller/login/FaceController.java | 103 ++++++++++++++++++ .../controller/login/UploadController.java | 47 +------- .../controller/login/UserController.java | 9 +- .../main/java/com/guwan/util/UUIDUtil.java | 13 +++ login/src/main/resources/application.yml | 15 +-- 6 files changed, 132 insertions(+), 59 deletions(-) create mode 100644 login/src/main/java/com/guwan/controller/login/FaceController.java create mode 100644 login/src/main/java/com/guwan/util/UUIDUtil.java diff --git a/login/pom.xml b/login/pom.xml index f3af5c0..57cc949 100644 --- a/login/pom.xml +++ b/login/pom.xml @@ -47,11 +47,11 @@ - + diff --git a/login/src/main/java/com/guwan/controller/login/FaceController.java b/login/src/main/java/com/guwan/controller/login/FaceController.java new file mode 100644 index 0000000..a35dd9c --- /dev/null +++ b/login/src/main/java/com/guwan/controller/login/FaceController.java @@ -0,0 +1,103 @@ +package com.guwan.controller.login; + +import com.guwan.config.MinioConfig; +import com.guwan.util.UUIDUtil; +import io.minio.MinioClient; +import io.minio.PutObjectOptions; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.InputStream; +import java.util.regex.Pattern; + +@RestController +public class FaceController { + + @Autowired + private MinioConfig minioConfig; + @Autowired + private MinioClient minioClient; + + + @GetMapping("/11") + public String test() { + try { + // 调用statObject()来判断对象是否存在。 + // 如果不存在, statObject()抛出异常, + // 否则则代表对象存在。 + minioClient.statObject(minioConfig.getBucketName(), "tempOrgImageFile"); + + //判断人脸照片是否合格 + //1.保存到本地 + InputStream tempInputStream = minioClient.getObject(minioConfig.getBucketName(), tempOrgImageFile); + String tempPath = globalValue.getStaticLocations() + "/";//+globalValue.getTempDir()+"/"; + String tempOrgFilePath = tempPath + tempOrgImageFile; + int index; + byte[] bytes = new byte[1024]; + File outFile = new File(tempOrgFilePath); + FileOutputStream downloadFile = new FileOutputStream(outFile); + while ((index = tempInputStream.read(bytes)) != -1) { + downloadFile.write(bytes, 0, index); + downloadFile.flush(); + } + downloadFile.close(); + tempInputStream.close(); + //2.测试上传的文件 + String tempFaceFileName = UUIDUtil.uuid() + ".jpg"; + String tempFaceFilePath = tempPath + tempFaceFileName; + + String osName = System.getProperty("os.name");//获取指定键(即os.name)的系统属性,如:Windows 7。 +// if (Pattern.matches("Windows.*", osName)) { + if (!Pattern.matches("Windows.*", osName)) { + int res = PicSDK.getFace(tempOrgFilePath, tempFaceFilePath); + if (res != 0) { + log.error("图片不合格,未检测到人脸"); +// return 2; + } + //保存底片文件到oss + InputStream inputStream = new FileInputStream(tempFaceFilePath); + PutObjectOptions putObjectOptions = new PutObjectOptions(inputStream.available(), -1); + putObjectOptions.setContentType("image/jpeg"); + minioClient.putObject( + minioConfig.getBucketName(), faceImageFileName, inputStream, putObjectOptions); + inputStream.close(); + } else { + minioClient.copyObject( + minioConfig.getBucketName(), + faceImageFileName, + null, + null, + minioConfig.getBucketName(), + tempOrgImageFile, + null, + null); + } + //拷贝临时文件正式文件 + minioClient.copyObject( + minioConfig.getBucketName(), + orgImageFileName, + null, + null, + minioConfig.getBucketName(), + tempOrgImageFile, + null, + null); + + entity.setOrgImage(orgImageFileName); + entity.setFaceImage(faceImageFileName); + //删除临时文件oss + minioClient.removeObject(minioConfig.getBucketName(), tempOrgImageFile); + //删除本地临时文件 + new File(tempFaceFilePath).delete(); + } catch (Exception e) { + entity.setOrgImage(""); + entity.setFaceImage(""); + e.printStackTrace(); + } + } + +} diff --git a/login/src/main/java/com/guwan/controller/login/UploadController.java b/login/src/main/java/com/guwan/controller/login/UploadController.java index 2252aa0..6105ff8 100644 --- a/login/src/main/java/com/guwan/controller/login/UploadController.java +++ b/login/src/main/java/com/guwan/controller/login/UploadController.java @@ -2,21 +2,16 @@ package com.guwan.controller.login; import com.guwan.common.R; import com.guwan.config.MinioConfig; -import com.pig4cloud.plugin.oss.service.OssTemplate; import io.minio.MinioClient; import io.minio.PutObjectOptions; -import jakarta.servlet.http.HttpServletRequest; import lombok.RequiredArgsConstructor; import org.apache.commons.io.FilenameUtils; -import org.apache.http.entity.FileEntity; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.*; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; -import java.io.IOException; import java.io.InputStream; -import java.time.Duration; -import java.util.Date; import java.util.UUID; @RestController @@ -29,42 +24,6 @@ public class UploadController { private final MinioClient minioClient; - - private final OssTemplate template; - /** - * 上传文件 - * 文件名采用uuid,避免原始文件名中带"-"符号导致下载的时候解析出现异常 - * @param file 资源 - * @return R(bucketName, filename) - */ - @PostMapping("/upload") - public String upload(@RequestParam("file") MultipartFile file) throws IOException { - String originalFilename = file.getOriginalFilename(); - String uuid = UUID.randomUUID().toString().replaceAll("-", ""); - String fileName = uuid + originalFilename; - template.putObject("demo", uuid + originalFilename, file.getInputStream()); - - - - - return fileName; - } - - - /** - * 获取外链 - * @param bucketName bucket名称 - * @param objectName 文件名称 - * @param minutes 过期时间,单位分钟,请注意该值必须小于7天 - * @return url - */ - - @GetMapping("/getDownloadExternalChain") - public String get(String bucketName, String objectName, int minutes) { - return template.getObjectURL(bucketName, objectName, Duration.ofMinutes(minutes)); - } - - @PostMapping("/GBUpload") public R gbUpload(MultipartFile file) { if (file.isEmpty() || file.getSize() == 0) { diff --git a/login/src/main/java/com/guwan/controller/login/UserController.java b/login/src/main/java/com/guwan/controller/login/UserController.java index 84815bc..28a8fb6 100644 --- a/login/src/main/java/com/guwan/controller/login/UserController.java +++ b/login/src/main/java/com/guwan/controller/login/UserController.java @@ -2,12 +2,13 @@ package com.guwan.controller.login; import com.guwan.service.TokenService; import com.guwan.util.IPUtils; +import io.swagger.v3.oas.annotations.Operation; import jakarta.annotation.security.PermitAll; import jakarta.servlet.http.HttpServletRequest; -import jakarta.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.*; -import io.swagger.v3.oas.annotations.Operation; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/user") @@ -21,7 +22,7 @@ public class UserController { @Operation(summary = "使用账号密码登录") public String login(String userId, HttpServletRequest request) { String ip = IPUtils.getIpAddr(request); - System.out.println("ip = " + ip); + System.out.println("ip111 = " + ip); return tokenService.createToken(userId, ip); } diff --git a/login/src/main/java/com/guwan/util/UUIDUtil.java b/login/src/main/java/com/guwan/util/UUIDUtil.java new file mode 100644 index 0000000..58b10db --- /dev/null +++ b/login/src/main/java/com/guwan/util/UUIDUtil.java @@ -0,0 +1,13 @@ +package com.guwan.util; + +import java.util.UUID; + +public class UUIDUtil { + public static String uuid(){ + return UUID.randomUUID().toString().replace("-",""); + } + public static void main(String args[]){ + String uuid = UUIDUtil.uuid(); + System.out.println(uuid); + } +} diff --git a/login/src/main/resources/application.yml b/login/src/main/resources/application.yml index ec98919..4ace2f2 100644 --- a/login/src/main/resources/application.yml +++ b/login/src/main/resources/application.yml @@ -4,10 +4,10 @@ server: spring: data: redis: - host: 1.92.149.170 + host: 123.57.30.183 port: 6379 - password: 123456 - database: 0 + password: aliredis666 + database: 1 datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/studb @@ -41,16 +41,13 @@ sa-token: -oss: - endpoint: http://localhost:9000 - access-key: admin - secret-key: admin123456 + global: minio: - endpoint: http://127.0.0.1:9000 + endpoint: http://1.92.149.170:9000 port: 9000 accessKey: admin secretKey: admin123456 - bucketName: yunnni \ No newline at end of file + bucketName: cuwan \ No newline at end of file