天天看點

阿裡雲OSS上傳案例代碼

引入阿裡雲OSS依賴

<dependency>
                <groupId>com.aliyun.oss</groupId>
                <artifactId>aliyun-sdk-oss</artifactId>
                <version>3.1.0</version>
            </dependency>
           

  現在已經到3.10了,具體可以參見阿裡雲OSS官網

項目目錄結構

阿裡雲OSS上傳案例代碼

配置檔案中放置OSS關鍵資訊例如:

#阿裡雲 OSS
#不同的伺服器,位址不同
aliyun.oss.file.endpoint=//自己的地域節點
aliyun.oss.file.keyid=//自己的keyid
aliyun.oss.file.keysecret=//自己的keysecret
#bucket可以在控制台建立,也可以使用java代碼建立
aliyun.oss.file.bucketname=//自己的bucketname
           

 ConstantPropertiesUtil為讀取配置檔案中的常量

@
      
Component
public class ConstantPropertiesUtil implements InitializingBean {

    //讀取配置檔案内容
    @Value("${aliyun.oss.file.endpoint}")
    private String endpoint;

    @Value("${aliyun.oss.file.keyid}")
    private String keyId;

    @Value("${aliyun.oss.file.keysecret}")
    private String keySecret;

    @Value("${aliyun.oss.file.bucketname}")
    private String bucketName;

    //定義公開靜态常量
    public static String END_POIND;
    public static String ACCESS_KEY_ID;
    public static String ACCESS_KEY_SECRET;
    public static String BUCKET_NAME;

    @Override
    public void afterPropertiesSet() throws Exception {
        END_POIND = endpoint;
        ACCESS_KEY_ID = keyId;
        ACCESS_KEY_SECRET = keySecret;
        BUCKET_NAME = bucketName;
    }
}
           

  controller

@Autowired
    private OssService ossService;
    //上傳頭像的方法
    @PostMapping
    public R uploadOssFile(MultipartFile file) {
        //擷取上傳檔案  MultipartFile
        //傳回上傳到oss的路徑
        String url = ossService.uploadOssFile(file);
        return R.ok().data("url",url);
    }
           

  serviceimpl  輸出可以去掉

@Override
    public String uploadOssFile(MultipartFile file) {
        //擷取四個值
        String endpoint = ConstantPropertiesUtil.END_POIND;
        String accessKeyId = ConstantPropertiesUtil.ACCESS_KEY_ID;
        String accessKeySecret = ConstantPropertiesUtil.ACCESS_KEY_SECRET;
        String bucketName = ConstantPropertiesUtil.BUCKET_NAME;
        //擷取檔案名稱
        String fileName = file.getOriginalFilename();
        //擷取随機字元串并拼接檔案名
        String s = UUID.randomUUID().toString().replaceAll("-", "");
        //獲得新的檔案名
        String newFileName=s+fileName;
        System.out.println(newFileName);
        //擷取目前年月日
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String format = sdf.format(new Date());
        System.out.println(format);
        //日期加檔案民拼接成為OSS中的路徑
        String OSSPath=format+"/"+newFileName;
        System.out.println(OSSPath);
        //開啟OSS
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        try {
            //擷取檔案流
            InputStream inputStream = file.getInputStream();
            //傳入地域節點,OSS中的路徑例如a/bb/c.jpg,檔案流
            ossClient.putObject(bucketName, OSSPath,inputStream);
        }catch (Exception e){
        }
        finally {
            // 關閉OSSClient。
            ossClient.shutdown();
        }
        //把上傳之後檔案路徑傳回
        //需要把上傳到阿裡雲oss路徑手動拼接出來

        String url = "https://"+bucketName+"."+endpoint+"/"+fileName;
        return url;
    }
           

  測試

oss