其他分享
首页 > 其他分享> > springboot整合七牛云实例

springboot整合七牛云实例

作者:互联网

整合步骤

  1. 引入依赖
    <dependency>
        <groupId>com.qiniu</groupId>
        <artifactId>qiniu-java-sdk</artifactId>
        <version>7.3.0</version>
    </dependency>

     

  2. 配置七牛云参数

    qiniu.accessKey=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    qiniu.secretKey=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    qiniu.bucket=XXXXXXX
    qiniu.zone=XXXXXXX
    qiniu.domain=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

     

  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,qiniu,实例,static,import,com,response,String
来源: https://blog.csdn.net/zlliuh/article/details/110006192