图片上传组件使用的的阿里云oss存储
1.优点:开发简单,拥有强大功能,免维护
2.缺点:付费
应用场景:完善个人信息选用阿里云的oss服务进行图片存储
地址:https://www.aliyun.com/product/oss
上传文件
查看图片
参照阿里提供的开发指南JavaSDK版
https://help.aliyun.com/document_detail/32009.html?spm=a2c4g.11186623.6.916.6f416d39e2YD5I
阿里云oss组件抽取
第一步导入依赖
在tanhua-commons中添加javaSDK的依赖,如果之前已经添加过,则忽略些步骤
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.10.2</version>
</dependency>
properties 配置对象
在tanhua-commons工程中创建OssProperties类中,从配置文件中获取阿里云OSS存储配置信息
package com.tanhua.commons.properties;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
@Data
@ConfigurationProperties(prefix = "tanhua.oss")
public class OssProperties {
private String endpoint;
private String accessKeyId;
private String accessKeySecret;
private String bucketName;
private String url;//mmeng.oss-cn-shenzhen.aliyuncs.com
}
上传模板对象
在tanhua-commons中创建上传模板类OssTemplate,自动装配注入此对象,完成上传图片功能
package com.tanhua.commons.templates;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.tanhua.commons.properties.OssProperties;
import java.io.FileInputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
public class OssTemplate {
private OssProperties ossProperties;
public OssTemplate(OssProperties ossProperties){
this.ossProperties = ossProperties;
}
/**
* 上传文件
* @param filename
* @param is
* @return
*/
public String upload(String filename, InputStream is) {
// Endpoint以杭州为例,其它Region请按实际情况填写。
String endpoint = ossProperties.getEndpoint();
// 云账号AccessKey有所有API访问权限,建议遵循阿里云安全最佳实践,创建并使用RAM子账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建。
String accessKeyId = ossProperties.getAccessKeyId();
String accessKeySecret = ossProperties.getAccessKeySecret();
// 创建OSSClient实例。
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
String suffix = filename.substring(filename.lastIndexOf("."));
String ymd = new SimpleDateFormat("yyyy/MM/dd").format(new Date());
String name = "images/" + ymd + "/" + UUID.randomUUID().toString() + suffix;
// 上传文件流。
ossClient.putObject(ossProperties.getBucketName(), name, is);
// 关闭OSSClient。
ossClient.shutdown();
return ossProperties.getUrl() + "/" + name;
}
}
配置类
在配置类TanhuaAutoConfiguration中加入OssTemplate对象相关配置
package com.tanhua.commons;
import com.tanhua.commons.properties.FaceProperties;
import com.tanhua.commons.properties.OssProperties;
import com.tanhua.commons.properties.SmsProperties;
import com.tanhua.commons.templates.OssTemplate;
import com.tanhua.commons.templates.SmsTemplate;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableConfigurationProperties({
SmsProperties.class, OssProperties.class
})
public class CommonsAutoConfiguration {
// @Bean//环信云sms短信调用
// public SmsTemplate smsTemplate(SmsProperties smsProperties){
// SmsTemplate smsTemplate = new SmsTemplate(smsProperties);
// smsTemplate.init();
// return smsTemplate;
// }
@Bean//阿里云oss图片存储
public OssTemplate ossTemplate(OssProperties ossProperties){
OssTemplate ossTemplate = new OssTemplate(ossProperties);
return ossTemplate;
}
}
tanhua-server工程的application.yml中加入oss配置信息如下
tanhua:
...
oss:
endpoint: http://oss-cn-shenzhen.aliyuncs.com
bucketName: mmeng
url: https://mmeng.oss-cn-shenzhen.aliyuncs.com
accessKeyId: LTAI5tScZvjv9goQM82HSGG3
accessKeySecret: X1MmvVK8ZzLIo1vcRZCA4Dnk3EOs5v
人脸检测
百度云Al
地址:https://ai.baidu.com/tech/face
抽取组件
tanhua-commons
<!--百度人脸识别API-->
<dependency>
<groupId>com.baidu.aip</groupId>
<artifactId>java-sdk</artifactId>
<version>4.8.0</version>
</dependency>
Propeties配置对象
在tanhua-commons工程中创建AipFaceProperties类中,从配置文件中获取配置信息
package com.tanhua.commons.properties;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
@Data
@ConfigurationProperties(prefix = "tanhua.face")
public class FaceProperties {
private String appId;
private String apiKey;
private String secretKey;
}
上传模板对象
在tanhua-commons工程中创建创建AipFaceTemplate,自动装配注入此对象,完成图片人脸检测
package com.tanhua.commons.templates;
import com.baidu.aip.face.AipFace;
import com.baidu.aip.util.Base64Util;
import com.tanhua.commons.properties.FaceProperties;
import org.json.JSONObject;
import java.util.HashMap;
public class FaceTemplate {
private FaceProperties faceProperties;
public FaceTemplate(FaceProperties faceProperties){
this.faceProperties = faceProperties;
}
public boolean detect(byte[] bytes){
// 初始化一个AipFace
AipFace client = new AipFace(faceProperties.getAppId(), faceProperties.getApiKey(), faceProperties.getSecretKey());
HashMap<String, String> options = new HashMap<String, String>();
options.put("face_field", "age");
options.put("max_face_num", "2");
options.put("face_type", "LIVE");
options.put("liveness_control", "LOW");
// 调用接口
String image = Base64Util.encode(bytes);
String imageType = "BASE64";
// 人脸检测
JSONObject res = client.detect(image, imageType, options);
// 没检测到人脸时,error_code值不为0
Integer error_code = (Integer)res.get("error_code");
return error_code == 0;
}
}
配置类
在tanhua-commons工程中创建TanhuaAutoConfiguration中加入AipFaceTemplate对象相关配置
package com.tanhua.commons;
import com.tanhua.commons.properties.FaceProperties;
import com.tanhua.commons.properties.OssProperties;
import com.tanhua.commons.properties.SmsProperties;
import com.tanhua.commons.templates.FaceTemplate;
import com.tanhua.commons.templates.OssTemplate;
import com.tanhua.commons.templates.SmsTemplate;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableConfigurationProperties({
SmsProperties.class, OssProperties.class, FaceProperties.class
})
public class CommonsAutoConfiguration {
@Bean
public SmsTemplate smsTemplate(SmsProperties smsProperties){
SmsTemplate smsTemplate = new SmsTemplate(smsProperties);
smsTemplate.init();
return smsTemplate;
}
@Bean
public OssTemplate ossTemplate(OssProperties ossProperties){
OssTemplate ossTemplate = new OssTemplate(ossProperties);
return ossTemplate;
}
@Bean
public FaceTemplate faceTemplate(FaceProperties faceProperties){
return new FaceTemplate(faceProperties);
}
}
tanhua-server工程加入oss配置信息如下
tanhua:
...
face:
appId: 24789821
apiKey: 1XKAEzWjV5eitDqG1lZR2AAb
secretKey: M22gIznvxlmENsU88BwgRtzKlmIGGGrb