日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網(wǎng)為廣大站長(zhǎng)提供免費(fèi)收錄網(wǎng)站服務(wù),提交前請(qǐng)做好本站友鏈:【 網(wǎng)站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(wù)(50元/站),

點(diǎn)擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會(huì)員:747

前言

阿里云對(duì)象存儲(chǔ)服務(wù)(Object Storage Service,簡(jiǎn)稱 OSS),是阿里云提供的海量、安全、低成本、高可靠的云存儲(chǔ)服務(wù)。其數(shù)據(jù)設(shè)計(jì)持久性不低于 99.9999999999%(12 個(gè) 9),服務(wù)設(shè)計(jì)可用性(或業(yè)務(wù)連續(xù)性)不低于 99.995%。

OSS 具有與平臺(tái)無(wú)關(guān)的 RESTful API 接口,您可以在任何應(yīng)用、任何時(shí)間、任何地點(diǎn)存儲(chǔ)和訪問(wèn)任意類型的數(shù)據(jù)。

您可以使用阿里云提供的 API、SDK 接口或者 OSS 遷移工具輕松地將海量數(shù)據(jù)移入或移出阿里云 OSS。數(shù)據(jù)存儲(chǔ)到阿里云 OSS 以后,您可以選擇標(biāo)準(zhǔn)存儲(chǔ)(Standard)作為移動(dòng)應(yīng)用、大型網(wǎng)站、圖片分享或熱點(diǎn)音視頻的主要存儲(chǔ)方式,也可以選擇成本更低、存儲(chǔ)期限更長(zhǎng)的低頻訪問(wèn)存儲(chǔ)(Infrequent Access)和歸檔存儲(chǔ)(Archive)作為不經(jīng)常訪問(wèn)數(shù)據(jù)的存儲(chǔ)方式。

登錄阿里云,進(jìn)入到控制臺(tái)

來(lái)吧,展示!SpringBoot OSS 整合全過(guò)程

 

點(diǎn)擊確定,就建好了。

  1. 接下來(lái)就開始附代碼
    新建一個(gè)springboot項(xiàng)目
    導(dǎo)入依賴 pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.Apache.org/POM/4.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>

   <groupId>org.example</groupId>
   <artifactId>SpringOOS</artifactId>
   <version>1.0-SNAPSHOT</version>

   <dependencies>
   <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-web</artifactId>
       <version>2.3.0.RELEASE</version>
   </dependency>

   <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-test</artifactId>
       <scope>test</scope>
       <version>2.3.0.RELEASE</version>
   </dependency>

       <!-- OSS SDK 相關(guān)依賴 -->
       <dependency>
           <groupId>com.aliyun.oss</groupId>
           <artifactId>aliyun-sdk-oss</artifactId>
           <version>3.4.2</version>
       </dependency>

   <dependency>
       <groupId>org.projectlombok</groupId>
       <artifactId>lombok</artifactId>
       <version>1.18.4</version>
       <scope>provided</scope>
   </dependency>
   <dependency>
       <groupId>joda-time</groupId>
       <artifactId>joda-time</artifactId>
       <version>2.9.9</version>
   </dependency>

   <dependency>
       <groupId>org.apache.commons</groupId>
       <artifactId>commons-lang3</artifactId>
       <version>3.8.1</version>
   </dependency>

   </dependencies>
</project>

Application.yml

## aliyun oss
## 配置說(shuō)明參考: com.ljq.demo.springboot.common.config.OSSConfig.class
oss:
   endpoint: oss-cn-shenzhen.aliyuncs.com
   url: https://oos-all.oss-cn-shenzhen.aliyuncs.com/
   accessKeyId: #這里在個(gè)人中心里accesskeys查看
   accessKeySecret: #這里在個(gè)人中心里accesskeys查看
   bucketName: #這里寫OSS里自己創(chuàng)建的OSS文件夾

來(lái)吧,展示!SpringBoot OSS 整合全過(guò)程

 


來(lái)吧,展示!SpringBoot OSS 整合全過(guò)程

 

寫一個(gè)config配置類

package com.sykj.config;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import JAVA.io.Serializable;

/**
 * @Description: 阿里云 OSS 配置信息
 * @Author: jiangpengcheng
 * @Date: 2020/07/15
 */
@Data
@Configuration
public class OSSConfig implements Serializable {

    private static final long serialVersionUID = -119396871324982279L;

    /**
     * 阿里云 oss 站點(diǎn)
     */
    @Value("${oss.endpoint}")
    private String endpoint;

    /**
     * 阿里云 oss 資源訪問(wèn) url
     */
    @Value("${oss.url}")
    private String url;

    /**
     * 阿里云 oss 公鑰
     */
    @Value("${oss.accessKeyId}")
    private String accessKeyId;

    /**
     * 阿里云 oss 私鑰
     */
    @Value("${oss.accessKeySecret}")
    private String accessKeySecret;

    /**
     * 阿里云 oss 文件根目錄
     */
    @Value("${oss.bucketName}")
    private String bucketName;

}

工具類

package com.sykj.util;

import com.aliyun.oss.ClientConfiguration;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import com.aliyun.oss.model.ObjectMetadata;
import com.sykj.config.OSSConfig;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

/**
 * @Description: 阿里云 oss 上傳工具類(高依賴版)
 * @Author: @author jiangpengcheng
 * @Date: 2020/7/15
 */

public class OSSBootUtil {

    private OSSBootUtil(){}

    /**
     * oss 工具客戶端
     */
    private volatile static OSSClient ossClient = null;

    /**
     * 上傳文件至阿里云 OSS
     * 文件上傳成功,返回文件完整訪問(wèn)路徑
     * 文件上傳失敗,返回 null
     * @author jiangpengcheng
     * @param ossConfig oss 配置信息
     * @param file 待上傳文件
     * @param fileDir 文件保存目錄
     * @return oss 中的相對(duì)文件路徑
     */

    public static String upload(OSSConfig ossConfig, MultipartFile file, String fileDir){
        initOSS(ossConfig);
        StringBuilder fileUrl = new StringBuilder();
        try {
            String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf('.'));
            String fileName = System.currentTimeMillis() + "-" + UUID.randomUUID().toString().substring(0,18) + suffix;
            if (!fileDir.endsWith("/")) {
                fileDir = fileDir.concat("/");
            }
            fileUrl = fileUrl.append(fileDir + fileName);
            System.out.println(fileUrl+"-----------------");
            ObjectMetadata objectMetadata = new ObjectMetadata();
            objectMetadata.setContentType("image/jpg");

            ossClient.putObject(ossConfig.getBucketName(), fileUrl.toString(), file.getInputStream(),objectMetadata);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        fileUrl = fileUrl.insert(0,ossConfig.getUrl());
        return fileUrl.toString();
    }

    /**
     * 初始化 oss 客戶端
     * @param ossConfig
     * @return
     */
    private static OSSClient initOSS(OSSConfig ossConfig) {
        if (ossClient == null ) {
            synchronized (OSSBootUtil.class) {
                if (ossClient == null) {
                    ossClient = new OSSClient(ossConfig.getEndpoint(),
                            new DefaultCredentialProvider(ossConfig.getAccessKeyId(), ossConfig.getAccessKeySecret()),
                            new ClientConfiguration());
                }
            }
        }
        return ossClient;
    }

    /**
     * 根據(jù)前臺(tái)傳過(guò)來(lái)的文件地址 刪除文件
     * @author jiangpengcheng
     * @param objectName
     * @param ossConfig
     * @return
     */
    public static ResponseResult delete(String objectName,OSSConfig ossConfig) {
        initOSS(ossConfig);
        //將完整路徑替換成 文件地址 因?yàn)閥ml里的url有了地址鏈接https: //oos-all.oss-cn-shenzhen.aliyuncs.com/
        // 如果再加上地址 那么又拼接了 所以刪除不了 要先把地址替換為 jpc/2020-07-16/1594857669731-51d057b0-9778-4aed.png
        String fileName = objectName.replace("https://oos-all.oss-cn-shenzhen.aliyuncs.com/", "");
        System.out.println(fileName+"******************************");
        // 根據(jù)BucketName,objectName刪除文件
        ossClient.deleteObject(ossConfig.getBucketName(), fileName);

        return ResponseResult.ok("刪除成功",fileName);
    }
}

數(shù)據(jù)返回工具類

package com.sykj.util;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @ClassName ResponseResult
 * @Description TODO
 * Author JiangPengCheng
 * Date 2020/7/15 9:13
 **/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ResponseResult implements Serializable {
    private Integer code;
    private String  message;
    private Object object;

    public static ResponseResult ok(String message){
        return new ResponseResult(200,message,null);
    }

    public static ResponseResult ok(String message, Object object){
        return new ResponseResult(200,message,object);
    }

    public static  ResponseResult error(String message){
        return new ResponseResult(500,message,null);
    }
    public static  ResponseResult error(String message, Object o){
        return new ResponseResult(500,message,o);
    }
}

Service類

package com.sykj.service;

import com.sykj.util.ResponseResult;
import org.springframework.http.ResponseEntity;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Description: 公共業(yè)務(wù)
 * @Author: junqiang.lu
 * @Date: 2018/12/24
 */
public interface CommonService {

    /**
     * 上傳文件至阿里云 oss
     *
     * @param file
     * @param
     * @return
     * @throws Exception
     */
    ResponseResult uploadOSS(MultipartFile file) throws Exception;

     ResponseResult delete(String objectName);
}

Service實(shí)現(xiàn)類

package com.sykj.service.impl;

import com.sykj.config.OSSConfig;
import com.sykj.service.CommonService;
import com.sykj.util.OSSBootUtil;
import com.sykj.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description: 公共業(yè)務(wù)具體實(shí)現(xiàn)類
 * @Author: junqiang.lu
 * @Date: 2018/12/24
 */
@Service("commonService")
public class CommonServiceImpl implements CommonService {

    @Autowired
    private OSSConfig ossConfig;

    /**
     * 上傳文件至阿里云 oss
     *
     * @param file
     * @param
     * @return
     * @throws Exception
     */
    @Override

    public ResponseResult uploadOSS(MultipartFile file) throws Exception {

        // 格式化時(shí)間
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        String format = simpleDateFormat.format(new Date());
        // 高依賴版本 oss 上傳工具
        String ossFileUrlBoot = null;
        /**
         * ossConfig 配置類
         * file 文件
         * "jpc/"+format 上傳文件地址 加時(shí)間戳
         */
        ossFileUrlBoot = OSSBootUtil.upload(ossConfig, file, "jpc/"+format);
        System.out.println(ossFileUrlBoot);
        Map<String, Object> resultMap = new HashMap<>(16);
//        resultMap.put("ossFileUrlSingle", ossFileUrlSingle);
        resultMap.put("ossFileUrlBoot", ossFileUrlBoot);

       return ResponseResult.ok("上傳成功~~",ossFileUrlBoot);
    }

    @Override
    public ResponseResult delete(String objectName) {
        ResponseResult delete = OSSBootUtil.delete(objectName, ossConfig);
        return delete;
    }
}

Controller類

package com.sykj.comtroller;
import com.sun.org.apache.regexp.internal.RE;
import com.sykj.service.CommonService;
import com.sykj.util.ResponseResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Description: 公共模塊控制中心
 * @Author: junqiang.lu
 * @Date: 2018/12/24
 */
@RestController
@RequestMapping("api/demo/common")
public class CommonController {

    private static final Logger logger = LoggerFactory.getLogger(CommonController.class);

    @Autowired
    private CommonService commonService;

    /**
     * 上傳文件至阿里云 oss
     *
     * @param file
     * @param
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/upload/oss", method = {RequestMethod.POST}, produces = {MediaType.APPLICATION_JSON_VALUE})
    public ResponseResult uploadOSS(@RequestParam(value = "file") MultipartFile file) throws Exception {
        System.out.println(file.getInputStream());
//        ResponseResult responseResult = commonService.uploadOSS(file);

//        HttpHeaders headers = new HttpHeaders();
//        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        return ResponseResult.ok("ok");
    }

   @RequestMapping("/delete/oss")
    public ResponseResult deltetOss(String objectName){
       System.out.println(objectName+"-------------------------------");
       ResponseResult delete = commonService.delete(objectName);
       return delete;
   }

}

注意postman選擇file

來(lái)吧,展示!SpringBoot OSS 整合全過(guò)程

 

這樣就完成了

最后

感謝你看到這里,看完有什么的不懂的可以在評(píng)論區(qū)問(wèn)我,覺(jué)得文章對(duì)你有幫助的話記得給我點(diǎn)個(gè)贊,每天都會(huì)分享java相關(guān)技術(shù)文章或行業(yè)資訊,歡迎大家關(guān)注和轉(zhuǎn)發(fā)文章!

分享到:
標(biāo)簽:SpringBoot
用戶無(wú)頭像

網(wǎng)友整理

注冊(cè)時(shí)間:

網(wǎng)站:5 個(gè)   小程序:0 個(gè)  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會(huì)員

趕快注冊(cè)賬號(hào),推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨(dú)大挑戰(zhàn)2018-06-03

數(shù)獨(dú)一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過(guò)答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫(kù),初中,高中,大學(xué)四六

運(yùn)動(dòng)步數(shù)有氧達(dá)人2018-06-03

記錄運(yùn)動(dòng)步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績(jī)?cè)u(píng)定2018-06-03

通用課目體育訓(xùn)練成績(jī)?cè)u(píng)定