This commit is contained in:
顾挽 2024-10-27 14:41:37 +08:00
parent e6e4a19ac8
commit f9aa90a619
6 changed files with 132 additions and 59 deletions

View File

@ -47,11 +47,11 @@
</dependency> </dependency>
<dependency> <!-- <dependency>
<groupId>com.pig4cloud.plugin</groupId> <groupId>com.pig4cloud.plugin</groupId>
<artifactId>oss-spring-boot-starter</artifactId> <artifactId>oss-spring-boot-starter</artifactId>
<version>3.0.0</version> <version>3.0.0</version>
</dependency> </dependency>-->
<dependency> <dependency>

View File

@ -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();
}
}
}

View File

@ -2,21 +2,16 @@ package com.guwan.controller.login;
import com.guwan.common.R; import com.guwan.common.R;
import com.guwan.config.MinioConfig; import com.guwan.config.MinioConfig;
import com.pig4cloud.plugin.oss.service.OssTemplate;
import io.minio.MinioClient; import io.minio.MinioClient;
import io.minio.PutObjectOptions; import io.minio.PutObjectOptions;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.FilenameUtils;
import org.apache.http.entity.FileEntity; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.time.Duration;
import java.util.Date;
import java.util.UUID; import java.util.UUID;
@RestController @RestController
@ -29,42 +24,6 @@ public class UploadController {
private final MinioClient minioClient; 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") @PostMapping("/GBUpload")
public R gbUpload(MultipartFile file) { public R gbUpload(MultipartFile file) {
if (file.isEmpty() || file.getSize() == 0) { if (file.isEmpty() || file.getSize() == 0) {

View File

@ -2,12 +2,13 @@ package com.guwan.controller.login;
import com.guwan.service.TokenService; import com.guwan.service.TokenService;
import com.guwan.util.IPUtils; import com.guwan.util.IPUtils;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.security.PermitAll; import jakarta.annotation.security.PermitAll;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.GetMapping;
import io.swagger.v3.oas.annotations.Operation; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController @RestController
@RequestMapping("/user") @RequestMapping("/user")
@ -21,7 +22,7 @@ public class UserController {
@Operation(summary = "使用账号密码登录") @Operation(summary = "使用账号密码登录")
public String login(String userId, HttpServletRequest request) { public String login(String userId, HttpServletRequest request) {
String ip = IPUtils.getIpAddr(request); String ip = IPUtils.getIpAddr(request);
System.out.println("ip = " + ip); System.out.println("ip111 = " + ip);
return tokenService.createToken(userId, ip); return tokenService.createToken(userId, ip);
} }

View File

@ -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);
}
}

View File

@ -4,10 +4,10 @@ server:
spring: spring:
data: data:
redis: redis:
host: 1.92.149.170 host: 123.57.30.183
port: 6379 port: 6379
password: 123456 password: aliredis666
database: 0 database: 1
datasource: datasource:
driver-class-name: com.mysql.cj.jdbc.Driver driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/studb 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: global:
minio: minio:
endpoint: http://127.0.0.1:9000 endpoint: http://1.92.149.170:9000
port: 9000 port: 9000
accessKey: admin accessKey: admin
secretKey: admin123456 secretKey: admin123456
bucketName: yunnni bucketName: cuwan