阿里接口请求发送工具类
使用hutools工具
String host = "https://gyytz.market.alicloudapi.com";
String path = "/sms/smsSend";
String method = "POST";
String appcode = "appcode";
Map<String, String> headers = new HashMap<String, String>();
headers.put("Authorization", "APPCODE " + appcode);
String code = SMSUtil.getCode();
Map<String, String> bodys = new HashMap<String, String>();
JSONObject json = new JSONObject();
json.append("mobile", phone);
json.append("param", "**code**:" + code + ",**minute**:1");
json.append("smsSignId", "smsSignId");
json.append("templateId", "templateId");
try {
HttpResponse res = HttpRequest.post(host + path).setMethod(Method.POST)
.header("Authorization", "APPCODE " + appcode)
.contentType("application/x-www-form-urlencoded; charset=UTF-8")
.form(json)
.execute();
log.info("{},code:{}短信发送成功:{}", phone, code, res.body());
} catch (Exception e) {
e.printStackTrace();
}
return code;
Oss上传删除文件工具类
@Slf4j
@Component
public class OssUtil {
@Value("${oss.accesskeyid}")
private String ACCESSKEYID;
@Value("${oss.accesskeysecret}")
private String ACCESSKEYSECRET;
@Value("${oss.endpoint}")
private String ENDPOINT;
@Value("${oss.bucket}")
private String BUCKET;
@Value("${oss.host}")
private String HOST;
public String upLoad(MultipartFile file) {
String url = "";
OSS ossClient = new OSSClientBuilder().build(ENDPOINT, ACCESSKEYID, ACCESSKEYSECRET);
try {
InputStream inputStream = file.getInputStream();
String fileName = file.getOriginalFilename();
if (StrUtil.isBlankIfStr(fileName)) {
throw new BaseException(500, "上传Oss异常");
}
fileName = getFileName(fileName);
fileName = "travel-file-1/" + fileName;
ossClient.putObject(BUCKET, fileName, inputStream);
url = HOST + "/" + fileName;
} catch (OSSException oe) {
System.out.println("Error Message:" + oe.getErrorMessage());
System.out.println("Error Code:" + oe.getErrorCode());
System.out.println("Request ID:" + oe.getRequestId());
System.out.println("Host ID:" + oe.getHostId());
return null;
} catch (ClientException ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message:" + ce.getMessage());
return null;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
return url;
}
public Boolean delete(String url) {
OSS ossClient = new OSSClientBuilder().build(ENDPOINT, ACCESSKEYID, ACCESSKEYSECRET);
try {
String[] str = url.split("\\.com/");
String objectName = str[1];
log.info(objectName);
ossClient.deleteObject(BUCKET, objectName);
return true;
} catch (OSSException oe) {
System.out.println("Error Message:" + oe.getErrorMessage());
System.out.println("Error Code:" + oe.getErrorCode());
System.out.println("Request ID:" + oe.getRequestId());
System.out.println("Host ID:" + oe.getHostId());
} catch (ClientException ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message:" + ce.getMessage());
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
return false;
}
public static String getFileName(String oldFileName) {
String[] type = oldFileName.split("\\.");
if (type.length == 0) {
return null;
}
String type1 = type[type.length - 1];
String fileName = RandomUtil.randomString(11);
fileName = fileName + "." + type1;
return fileName;
}
}