1、上传至腾讯云

1、导入包

1
2
3
4
5
6
<!--腾讯云存储依赖-->
<dependency>
<groupId>com.qcloud</groupId>
<artifactId>cos_api</artifactId>
<version>5.6.8</version>
</dependency>

2、配置yml

1
2
3
4
5
6
7
8
spring:
tengxun:
accessKey: #访问秘钥 SecretId
secretKey: #访问秘钥 SecretKey
bucket: ap-shenzhen-fsi #存储桶地址 我的是深圳金融
bucketName: guao-1305634158 #存储桶名
path: https://guao-158.cos.ap-shenzhen-fsi.myqcloud.com #访问域名
qianzhui: image #前缀

秘钥获取地址:访问密钥 - 控制台 (tencent.com)

3、实现代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.PutObjectResult;
import com.qcloud.cos.region.Region;
import com.znsd.ecommerce.api.model.Content;
import com.znsd.ecommerce.api.utli.BaseResult;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Calendar;
import java.util.UUID;

/**
* 文件上传控制器
*/
@Controller
@RequestMapping(value = "/upload")
public class UploadController {

@Value("${spring.tengxun.accessKey}")
private String accessKey;
@Value("${spring.tengxun.secretKey}")
private String secretKey;
@Value("${spring.tengxun.bucket}")
private String bucket;
@Value("${spring.tengxun.bucketName}")
private String bucketName;
@Value("${spring.tengxun.path}")
private String path;
@Value("${spring.tengxun.qianzhui}")
private String qianzhui;

@PostMapping("/tengxun")
@ResponseBody
public Content Upload(MultipartFile file) {
if (file == null) {
return BaseResult.error("文件为空");
}
String oldFileName = file.getOriginalFilename();
String eName = oldFileName.substring(oldFileName.lastIndexOf("."));
String newFileName = UUID.randomUUID() + eName;
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH)+1;
int day = cal.get(Calendar.DATE);
// 1 初始化用户身份信息(secretId, secretKey)
COSCredentials cred = new BasicCOSCredentials(accessKey, secretKey);
// 2 设置bucket的区域, COS地域的简称请参照 https://cloud.tencent.com/document/product/436/6224
ClientConfig clientConfig = new ClientConfig(new Region(bucket));
// 3 生成cos客户端
COSClient cosclient = new COSClient(cred, clientConfig);
// bucket的命名规则为{name}-{appid} ,此处填写的存储桶名称必须为此格式
String bucketName = this.bucketName;

// 简单文件上传, 最大支持 5 GB, 适用于小文件上传, 建议 20 M 以下的文件使用该接口
// 大文件上传请参照 API 文档高级 API 上传
File localFile = null;
try {
localFile = File.createTempFile("temp", null);
file.transferTo(localFile);
// 指定要上传到 COS 上的路径
String key = "/" + this.qianzhui + "/" + year + "/" + month + "/" + day + "/" + newFileName;
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
PutObjectResult putObjectResult = cosclient.putObject(putObjectRequest);
return BaseResult.success("上传成功", this.path+putObjectRequest.getKey().toString());
} catch (IOException e) {
return BaseResult.error("上传失败,异常");
} finally {
// 关闭客户端(关闭后台线程)
cosclient.shutdown();
}
}

private class UploadMsg {
public int status;
public String msg;
public String path;

public UploadMsg() {
super();
}

public UploadMsg(int status, String msg, String path) {
this.status = status;
this.msg = msg;
this.path = path;
}
}
}

注:BaseResult.success()为自己写的统一返回方法

2、实现短信发送

1
2
3
4
5
6
<!--腾讯云短信发送需要包-->
<dependency>
<groupId>com.github.qcloudsms</groupId>
<artifactId>qcloudsms</artifactId>
<version>1.0.6</version>
</dependency>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
public class SMS {
/**
* 随机生成6位验证码
*
* @return 6位验证码
*/
public static String getRandomCode(Integer code) {
Random random = new Random();
StringBuilder result = new StringBuilder();
for (int i = 0; i < code; i++) {
result.append(random.nextInt(10));
}
return result.toString();
}

public static String tenSendMsg(String phone,String code){
// 短信应用 SDK AppID
int appid = 1400549217; // SDK AppID 以1400开头
// 短信应用 SDK AppKey
String appkey = "d8d3eb7e5db6d42895da"; //修改为自己手机发送接口
// 需要发送短信的手机号码
String[] phoneNumbers = {phone};
// 短信模板 ID,需要在短信应用中申请
int templateId = 1039299; // NOTE: 这里的模板 ID`7839`只是示例,真实的模板 ID 需要在短信控制台中申请
// 签名
String smsSign = "codeliufeng"; // NOTE: 签名参数使用的是`签名内容`,而不是`签名ID`。这里的签名"腾讯云"只是示例,真实的签名需要在短信控制台申请
try {
String[] params = {code,"5"};
SmsSingleSender ssender = new SmsSingleSender(appid, appkey);
SmsSingleSenderResult result = ssender.sendWithParam("86", phoneNumbers[0],
templateId, params, smsSign, "", "");
return result.errMsg;
} catch (HTTPException | com.github.qcloudsms.httpclient.HTTPException | IOException e) {
// HTTP 响应码错误
e.printStackTrace();
}
return null;
}
}