程序员JAVAJava

java使用阿里oss和短信功能

2019-06-03  本文已影响3人  阿靖哦

提供阿里oss和短信功能

推荐使用最新版本

安装

<dependency>
    <groupId>cn.gjing</groupId>
    <artifactId>tools-ali</artifactId>
    <version>1.0.1</version>
</dependency>

使用方法


阿里OSS:

@PostMapping("/upload")
@ApiOperation(value = "上传图片", httpMethod = "POST")
public ResponseEntity testUpload(@RequestParam(name = "file") MultipartFile file) {
   String fileOssUrl = AliOss.of("endPoint", "accessKeyId","accessKeySecret", "bucketName")
           .upload(file);
   return ResponseEntity.ok(fileOssUrl);
}
@DeleteMapping("/file")
@ApiOperation(value = "删除文件", httpMethod = "DELETE")
public ResponseEntity deleteImg(@RequestParam("url") String url) {
   List<String> deleteObjectList = AliOss.of("endPoint", "accessKeyId","accessKeySecret", "bucketName")
           .delete(Collections.singletonList(url));
   return ResponseEntity.ok(deleteObjectList);
}

下载文件: fileOssUrl为上传oss成功后返回的url,localFilePath为你要保存的本地目录(不存在会创建),返回true为成功;

@PostMapping("/img-native")
@ApiOperation(value = "下载文件到本地", httpMethod = "POST")
public ResponseEntity downImgToNative(String fileOssUrl, String path) {
   boolean down = AliOss.of("endPoint", "accessKeyId","accessKeySecret", "bucketName")
       .downloadFile(fileOssUrl, path);
   if (down) {
       return ResponseEntity.ok("下载成功");
   }
   return ResponseEntity.badRequest().body("下载失败");
}

流式下载文件:

@GetMapping("/img-stream")
@ApiOperation(value = "流式下载", httpMethod = "GET")
public ResponseEntity streamDown(String fileOssUrl, HttpServletResponse response) {
   boolean down = AliOss.of("endPoint", "accessKeyId","accessKeySecret", "bucketName")
       .downloadStream(fileOssUrl, response);
   if (down) {
       return ResponseEntity.ok("下载成功");
   }
   return ResponseEntity.badRequest().body("下载失败");
}

阿里短信:

@PostMapping("/send-ali")
@ApiOperation(value = "阿里短信", httpMethod = "POST")
public ResponseEntity sendAli() {
    //参数,必须与对应的短信模板参数对应,如果没有参数,可以传null或者空map
    Map<String, String> param = new HashMap<>(16);
    map.put("code", "123123");
    //接收的短信的手机号,支持多个,英文逗号隔开
    String send = AliSms.of("accessKeyId", "accessKeySecret")
            .send("157********", "短信模板Code", param, "短信签名");
    return ResponseEntity.ok(send);
}
@GetMapping("query-ali")
@ApiOperation(value = "查询阿里短信发送记录", httpMethod = "GET")
public ResponseEntity queryAli() {
    String result = AliSms.of("您的accessKeyId", "您的accessKey秘钥")
            .querySendDetails("157********", "查询日期(年月日)", "每页数量", "当前页数");
    return ResponseEntity.ok(result);
}
上一篇下一篇

猜你喜欢

热点阅读