ali短信发送工具类

阿里接口请求发送工具类

使用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>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE code
        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());
            //获取response的body
            //System.out.println(EntityUtils.toString(response.getEntity()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return code;

Oss上传删除文件工具类


/**
 * @author cyh
 * 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;

    /**
     * 随机生成文件名
     *
     * @param file
     * @return
     */
    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);
            //2.通过时间格式创建文件路径 保存文件
            fileName = "travel-file-1/" + fileName;
            // 创建PutObject请求。
            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) {

        // 填写文件完整路径。文件完整路径中不能包含Bucket名称。
//        String objectName = dir+"/"+filename;

        // 创建OSSClient实例。
        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;
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值