天天看點

Java使用阿裡雲OSS和短信導入依賴

阿裡雲OSS和短信工具

導入依賴

<dependency>
    <groupId>cn.gjing</groupId>
    <artifactId>tools-aliyun</artifactId>
    <version>1.0.7</version>
</dependency>           

一、OSS

1、配置

以下配置除了

後四個配置不必填

,其他都

必填

tools:
  aliyun:
    # 使用者key,在阿裡雲擷取,此處為全局配置,短信和oss未單獨設定則會預設調用此處配置
    access-key: xxxxxxx
    # 使用者秘鑰,在阿裡雲擷取,此處為全局配置,短信和OSS都會預設調用此處的key
    access-key-secret: xxxxxxx
    oss:
      # 使用者key,在阿裡雲擷取,此處優先級高于全局配置
      access-key: xxxxxxx
      # 使用者秘鑰,此處優先級高于全局配置
      access-key-secret: xxxxxxx
      # 節點, 前往阿裡雲檢視
      end-point: xxxxxxxx
      # 存儲空間,不存在會建立
      bucket: xxxxxxx
      # 最大連接配接數,預設1024
      max-connections: 1024
      # 最大空閑時間(毫秒),預設60000
      idle-time: 60000
      # socket逾時時間(毫秒),預設50000
      socket-timeout: 50000
      # 連接配接逾時時間(毫秒),預設50000
      connection-timeout: 50000           

2、檔案上傳

  • 簡單上傳

适用于上傳小檔案,成功後會傳回檔案名,前端隻需要将檔案名與bucket域名拼接即可,bucket域名可在oss控制台,點選bucket清單,找到你使用的bucket點進去後點選概覽,就可以在右邊界面看到了,或者自己拼接:

https://<bucket>.<endPoint>

import cn.gjing.tools.aliyun.oss.SimpleOssUpload;
/**
 * @author Gjing
 **/
@RestController
public class TestController {
    @Resource
    private SimpleOssUpload simpleOssUpload;

    @PostMapping("/file")
    public String upload(MultipartFile file) {
        // 直接上傳,其他參數使用預設方式
        return this.simpleOssUpload.upload(file);
    }

    @PostMapping("/file2")
    public String upload(MultipartFile file) {
        // 手動設定檔案名,預設為源檔案的檔案名,也可以指定包含檔案字尾在内的完整路徑
        return this.simpleOssUpload.upload(file, "目錄1/123.jpg");
    }

    @PostMapping("/file3")
    public String upload(MultipartFile file) {
        // 可以手動設定bucket,否則讀取yml檔案中配置的bucket, bucket如果在阿裡雲oss不存在,會自動建立
        return this.simpleOssUpload.upload(file, "檔案名", "bucket");
    }

    @DeleteMapping("/test1")
    public void deleteFile(String fileName) {
        // 删除指定oss檔案,檔案名為上傳檔案後傳回的
        this.simpleOssUpload.deleteFile(fileName);
    }

    @DeleteMapping("/test2")
    public String deleteFiles(String[] fileNames) {
        // 批量删除, 會在執行完成後會傳回删除成功的檔案名清單,批量删除最多同時删除``1000``個
        return this.simpleOssUpload.deleteFiles(Arrays.asList(fileNames)).toString();
    }
}           
  • 分片上傳

    适用于大檔案上傳,通過分片,加快上傳的速度,上傳成功後傳回一個

    FileMeta

    ,裡面包含

    fileName

    uploadId

    bucket

    預設分片切割大小為1M

import cn.gjing.tools.aliyun.oss.FileMeta;
import cn.gjing.tools.aliyun.oss.MultipartOssUpload;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author Gjing
 **/
@RestController
public class TestController {
    @Resource
    private MultipartOssUpload multipartOssUpload;

    @PostMapping("/file")
    public String upload(MultipartFile file) {
        // 直接上傳,其他的參數使用預設
        return this.multipartOssUpload.upload(file).getFileName();
    }

    @PostMapping("/file2")
    public String upload(MultipartFile file) {
        // 手動設定檔案名,預設擷取源檔案的檔案名, 也可以指定包含檔案字尾在内的完整路徑
        return this.multipartOssUpload.upload(file, "目錄1/123.jpg").getFileName();
    }

    @PostMapping("/file3")
    public String upload(MultipartFile file) {
        // 可以手動設定bucket,否則讀取yml檔案中配置的bucket,bucket如果在阿裡雲oss不存在,會建立
        return this.multipartOssUpload.upload(file, "檔案名", "bucket").getFileName();
    }
}           

4、判斷檔案是否存在

/**
 * @author Gjing
 **/
@RestController
public class TestController {
    @Resource
    private OssDownload ossDownload;

    @PostMapping("/test")
    public boolean test(String fileName) {
        return this.ossDownload.isExist(fileName);
    }
}           

5、檔案下載下傳

/**
 * @author Gjing
 **/
@RestController
public class TestController {
    @Resource
    private OssDownload ossDownload;

    @GetMapping("/test")
    public void downLocal(String fileName, HttpServletResponse response) {
        // 1、下載下傳到本地指定目錄
        this.ossDownload.downByLocal("/Users/colin/Desktop/1/", fileName);
        // 2、通過流下載下傳
        this.ossDownload.downByStream(fileName, response);
    }
}           

二、短信

region

其他都

必填

tools:
  aliyun:
    # 使用者key,在阿裡雲擷取
    access-key: xxxxxxx
    # 使用者秘鑰,在阿裡雲擷取
    access-key-secret: xxxxxx
    sms:
      # 使用者key,在阿裡雲擷取,此處優先級高于全局配置
      access-key: xxxxxxx
      # 使用者秘鑰,此處優先級高于全局配置
      access-key-secret: xxxxxxx
      # 短信模闆ID,必須是已添加并稽核通過的
      template-code: xxxxxxx
      # 短信簽名名稱,必須是已添加并稽核通過的
      sign-name: xxxxxx
      # 區域,預設default
      region: default           

2、發送短信

短信模闆變量對應的實際值

/**
 * @author Gjing
 **/
@RestController
public class SmsController {
    @Resource
    private SmsHelper smsHelper;

    @PostMapping("/sms")
    public String send(String phones) {
        Map<String, Integer> param = new HashMap<>();
        param.put("code", 12345);
        return this.smsHelper.send(phones, param);
    }
}           

send()

方法參數如下

參數 描述
phones 11位手機号,多個用英文逗号隔開,上限為

1000

templateCode 短信模闆code,必須是已存在且稽核通過
signName 短信簽名名稱,必須是已存在且稽核通過
param

3、查詢指定号碼發送記錄

/**
 * @author Gjing
 **/
@RestController
public class SmsController {
    @Resource
    private SmsHelper smsHelper;

    @GetMapping("/sms_record")
    public String findSmsRecord(String phone) {
        return this.smsHelper.findSendDetail(phone, "2020-02-01", 1, 5);
    }
}           

findSendDetail()

phone 11位手機号
sendDate 發送日期,格式:yyyy-MM-dd
page 頁數
row 每頁條數,最大

50

源代碼位址:

tools-aliyun