springboot整合七牛云实例

整合步骤

  1. 引入依赖
    
        com.qiniu
        qiniu-java-sdk
        7.3.0
    

     

  2. 配置七牛云参数

    qiniu.accessKey=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    qiniu.secretKey=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    qiniu.bucket=XXXXXXX
    qiniu.zone=XXXXXXX
    qiniu.domain=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    import com.qiniu.common.Zone;
    import com.qiniu.storage.BucketManager;
    import com.qiniu.storage.UploadManager;
    import com.qiniu.util.Auth;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    public class QiniuConfig {
        /**
         * 华东机房,配置自己空间所在的区域
         */
        @Bean
        public com.qiniu.storage.Configuration qiniuConfig() {
            return new com.qiniu.storage.Configuration(Zone.zone2());
        }
    
        /**
         * 构建一个七牛上传工具实例
         */
        @Bean
        public UploadManager uploadManager() {
            return new UploadManager(qiniuConfig());
        }
    
        @Value("${qiniu.accessKey}")
        private String accessKey;
        @Value("${qiniu.secretKey}")
        private String secretKey;
    
        /**
         * 认证信息实例
         * @return
         */
        @Bean
        public Auth auth() {
            return Auth.create(accessKey, secretKey);
        }
    
        /**
         * 构建七牛空间管理实例
         */
        @Bean
        public BucketManager bucketManager() {
            return new BucketManager(auth(), qiniuConfig());
        }
    }

     

  3. 编写七牛云工具类

    import com.qiniu.common.QiniuException;
    import com.qiniu.http.Response;
    import com.qiniu.storage.BucketManager;
    import com.qiniu.storage.UploadManager;
    import com.qiniu.util.Auth;
    import com.qiniu.util.StringMap;
    
    import java.io.File;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Properties;
    
    public class QiniuUtil {
    
        private static UploadManager uploadManager;
        private static BucketManager bucketManager;
        private static Auth auth;
        private static String bucket;
        private static String domain;
        private static StringMap putPolicy;
    
        static {
            uploadManager = SpringUtil.getBean(UploadManager.class);
            bucketManager = SpringUtil.getBean(BucketManager.class);
            auth = SpringUtil.getBean(Auth.class);
            try {
                InputStream stream=Thread.currentThread().getContextClassLoader().getResourceAsStream("application.properties");
                Properties p=new Properties();
                p.load(stream);
                bucket = p.get("qiniu.bucket").toString() ;
                domain = p.get("qiniu.domain").toString() ;
            } catch (IOException e) {
            }
        }
    
        /**
         * 上传文件
         * @param file
         * @param fileName
         * @return
         * @throws QiniuException
         */
        public static String uploadFile(File file, String fileName) throws QiniuException {
            Response response = uploadManager.put(file, fileName, getUploadToken());
            int retry = 0;
            while (response.needRetry() && retry < 3) {
                response = uploadManager.put(file, fileName, getUploadToken());
                retry++;
            }
            if (response.statusCode == 200) {
                return "http://" + domain + "/" + fileName;
            }
            return "上传失败!";
        }
    
        /**
         * 删除文件
         * @param key
         * @return
         * @throws QiniuException
         */
        public static String delete(String key) throws QiniuException {
            Response response = bucketManager.delete(bucket, key);
            int retry = 0;
            while (response.needRetry() && retry++ < 3) {
                response = bucketManager.delete(bucket, key);
            }
            return response.statusCode == 200 ? "删除成功!" : "删除失败!";
        }
    
        /**
         * 获取上传凭证
         */
        private static String getUploadToken() {
            return auth.uploadToken(bucket, null, 3600, putPolicy);
        }
    }

     

  4. 调用工具类

        springboot整合七牛云实例_第1张图片

你可能感兴趣的:(springboot,java,spring,boot)