将本地图片存储到云服务器上(七牛云)

2019-06-27  本文已影响0人  闲置的Programmer

首先引入七牛云的依赖

<dependency>
   <groupId>com.qiniu</groupId>
   <artifactId>qiniu-java-sdk</artifactId>
   <version>7.2.11</version>
   <scope>compile</scope>
</dependency>
<dependency>
   <groupId>com.squareup.okhttp3</groupId>
   <artifactId>okhttp</artifactId>
   <version>3.3.1</version>
   <scope>compile</scope>
</dependency>
<dependency>
   <groupId>com.google.code.gson</groupId>
   <artifactId>gson</artifactId>
   <version>2.6.2</version>
   <scope>compile</scope>
</dependency>
<dependency>
   <groupId>com.qiniu</groupId>
   <artifactId>happy-dns-java</artifactId>
   <version>0.1.4</version>
   <scope>compile</scope>
</dependency>
<dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>4.12</version>
   <scope>test</scope>
</dependency>

然后编写工具类

package com.utils;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;

import java.io.IOException;

public class QiNiuUploading {
    public static String ACCESS_KEY = "";//key值
    public static String SECRET_KEY = "";
    //要上传的空间
    private static String bucketname = "xxx";//空间自己定义名字

    //密钥配置
    private static  Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
    //创建上传对象
    private static Configuration cfg = new Configuration(Zone.zone2());//上传的机房
    private static UploadManager uploadManager = new UploadManager(cfg);

    //简单上传,使用默认策略,只需要设置上传的空间名就可以了
    public static String getUpToken(){
        return auth.uploadToken(bucketname);
    }
    //简单上传,使用默认策略,只需要设置上传的空间名就可以了
    /*public  static String getcoverUpToken(String name){
        return auth.uploadToken(bucketname,name);
    }*/

    //普通上传
    public static boolean upload(String localFilePath,String key) throws IOException {
        System.err.println("进入普通上传Util");
        try {
            //调用put方法上传
            //Response res = uploadManager.put(file, key, getUpToken());
            Response res = uploadManager.put(localFilePath, key, getUpToken());
            System.err.println("上传结束");
            //打印返回的信息
            //res.bodyString()
            System.err.println("七牛返回值信息:"+res.bodyString());
            if (res.statusCode == 200){
                System.err.println("上传成功.");
                return true;
            }else{
                System.err.println("上传失败");
                return false;
            }
        } catch (QiniuException e) {
            Response r = e.response;
            System.err.println("上传失败2");
            System.err.println("上传失败r2"+r);
            return false;
        }
    }

    //覆盖上传
    public static boolean coverUpload(String localFilePath,String key) throws IOException {
        System.err.println("进入普通覆盖上传Util");
        System.err.println("名称为:"+key);
        String upToken = auth.uploadToken(bucketname, key);
        System.err.println("upToken:"+upToken);

        //String upToken = auth.uploadToken(bucket);

        try {
            //调用put方法上传
            Response res = uploadManager.put(localFilePath, key, upToken);
            //Response res = uploadManager.put(file, key, upToken);
            System.err.println("覆盖上传结束.");
            //打印返回的信息
            //res.bodyString()

            System.err.println("七牛返回值信息:"+res.bodyString());
            if (res.statusCode == 200){
                System.err.println("覆盖上传成功");
                return true;
            }else{
                System.err.println("覆盖上传失败.");
                return false;
            }
        } catch (QiniuException e) {
            Response r = e.response;
            System.err.println("覆盖上传失败2");
            System.err.println("覆盖上传失败r2"+r);
            return false;
        }
    }

    //删除图片
    public static boolean deleteUpload(String key){
        System.err.println("进入普通删除文件");
        BucketManager bucketManager = new BucketManager(auth, cfg);
        try {
            System.err.println("开始删除. ");
            bucketManager.delete(bucketname, key);
            System.err.println("删除成功");
        } catch (QiniuException ex) {
            //如果遇到异常,说明删除失败
            System.err.println(ex.code());
            System.err.println(ex.response.toString());
            return false;
        }
        return true;
    }

}



最后就可以直接进行调用方法进行上传啦

boolean qiniu = false;
                if (isSuccess) {
                    //将本地的图片储存在七牛云
                    qiniu = QiNiuUploading.upload("本地图片路径", “七牛云图片名称,不要重复”);
                } else {
                    System.out.println("美颜后的图片存入本地失败");
                }

上一篇下一篇

猜你喜欢

热点阅读