提交 23c0a04e authored 作者: yangqi's avatar yangqi

回调

上级 14bcb67b
......@@ -3,13 +3,20 @@ package cn.lefull.api;
import cn.lefull.common.annotation.NoAuth;
import cn.lefull.common.response.ApiResponse;
import cn.lefull.service.ali.FaceService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;
/**
* @author 杨奇
......@@ -17,6 +24,9 @@ import java.util.Map;
@RestController
@RequestMapping("/face")
public class FaceController {
public static final String REAL_PATH = "/opt/server/iot/";
@Autowired
private FaceService faceService;
......@@ -68,4 +78,58 @@ public class FaceController {
faceService.searchFace(dbName, url);
return new ApiResponse<>();
}
@NoAuth
@RequestMapping("/uploads")
public String uploadFiles(HttpServletRequest request,
@RequestPart("faceImage") MultipartFile faceImage,
@RequestPart("backgroundImage") MultipartFile backgroundImage) {
Logger logger = LoggerFactory.getLogger(FaceController.class);
logger.info("upload....");
SimpleDateFormat sdf = new SimpleDateFormat("/yyyy/MM/dd/");
//获得路径
String format = sdf.format(new Date());
String path = REAL_PATH + format;
//创建文件夹
File fold = new File(path);
while (!fold.exists()) {fold.mkdirs();}
StringBuffer retsb = new StringBuffer();
MultipartFile[] uploadFile = new MultipartFile[] {faceImage, backgroundImage};
try {
for (int i = 0; i < uploadFile.length; i++) {
MultipartFile file = uploadFile[i];
String oldname = file.getOriginalFilename();
// 新文件名
String newname = UUID.randomUUID().toString() + "_" + file.getName() + ".jpg";
file.transferTo(new File(fold, newname));
String ssss = request.getScheme() + "://" + request.getServerName() + ":" +
request.getServerPort() +
"/facebody/preview?path=" + format + newname;
logger.info(ssss);
retsb.append(String.format("<img src=\"%s\" /><br/>\n", ssss));
}
} catch (IOException e) {
e.printStackTrace();
}
return retsb.toString();
}
@NoAuth
@RequestMapping("/upload")
public void uploadFile() throws Exception {
String dbName = "faceDb_321";
String file = "D:/phpstudy/phpstudy_pro/WWW/wework/runtime/1.jpg";
faceService.searchFaces(dbName, file);
}
}
......@@ -2,14 +2,18 @@ package cn.lefull.common.sdk.ali.base;
import com.aliyun.com.viapi.FileUtils;
import com.aliyun.facebody20191230.models.*;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.net.URL;
import java.util.*;
import java.util.stream.Collectors;
@Component
......@@ -186,4 +190,38 @@ public class Face {
FileUtils fileUtils = FileUtils.getInstance(this.accessKeyId, this.accessKeySecret);
return fileUtils.upload(file);
}
public URL uploadImage(String fileName, File file) {
String objectName = (new StringBuilder()
.append(fileName))
.toString();
// 创建OSSClient实例。
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
try {
PutObjectRequest request = new PutObjectRequest(bucketName, objectName, file);
ossClient.putObject(request);
Date expiration = new Date(System.currentTimeMillis() + 3600 * 1000);
URL url = ossClient.generatePresignedUrl(bucketName, objectName, expiration);
System.out.println(url);
return url;
} catch (OSSException oe) {
System.err.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.err.println("Error Message:" + oe.getErrorMessage());
System.err.println("Error Code:" + oe.getErrorCode());
System.err.println("Request ID:" + oe.getRequestId());
System.err.println("Host ID:" + oe.getHostId());
return null;
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
}
}
......@@ -45,4 +45,9 @@ public interface FaceService {
* 搜索人脸
*/
boolean searchFace(String dbName, String url) throws Exception;
/**
* 搜索人脸
*/
public void searchFaces(String dbName, String filePath) throws Exception;
}
......@@ -15,6 +15,8 @@ import com.aliyun.facebody20191230.models.SearchFaceResponseBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.File;
import java.net.URL;
import java.util.List;
import java.util.Map;
......@@ -157,4 +159,14 @@ public class FaceServiceImpl implements FaceService{
logMapper.addLog(log);
return true;
}
@Override
public void searchFaces(String dbName, String filePath) throws Exception {
Utils.checkParamsNotEmpty(dbName, "dbName");
Utils.checkParamsNotEmpty(filePath, "filePath");
File file = new File(filePath);
URL url = sdk.uploadImage(dbName, "1.jpg", file);
sdk.searchFace(dbName, url.toString());
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论