七牛文件上传demo

    @Test
    public void haha() {
        //构造一个带指定 Region 对象的配置类
        Configuration cfg = new Configuration(Region.region2());
//...其他参数参考类注释

        UploadManager uploadManager = new UploadManager(cfg);
//...生成上传凭证,然后准备上传
        String accessKey = "your key";
        String secretKey = " you decret";
        String bucket = "your 空间名字";

//默认不指定key的情况下,以文件内容的hash值作为文件名
        String key = null;

        try {
            byte[] uploadBytes = "hello qiniu cloud".getBytes("utf-8");
            Auth auth = Auth.create(accessKey, secretKey);
            String upToken = auth.uploadToken(bucket);

            try {
                Response response = uploadManager.put(uploadBytes, key, upToken);
                //解析上传成功的结果
                DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
                System.out.println(putRet.key);
                System.out.println(putRet.hash);
            } catch (QiniuException ex) {
                Response r = ex.response;
                System.err.println(r.toString());
                try {
                    System.err.println(r.bodyString());
                } catch (QiniuException ex2) {
                    //ignore
                    System.out.println(ex2);
                }
            }
        } catch (UnsupportedEncodingException ex) {
            //ignore
            System.out.println(ex);
        }

    }

导 包

 <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.5</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
            <version>3.14.4</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.67</version>
        </dependency>
        <dependency>
            <groupId>com.qiniu</groupId>
            <artifactId>happy-dns-java</artifactId>
            <version>0.1.6</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.qiniu</groupId>
            <artifactId>qiniu-java-sdk</artifactId>
            <version>[7.2.0, 7.2.99]</version>
        </dependency>

注意点:

//构造一个带指定Zone对象的配置类
 Configuration cfg = new Configuration(Zone.zone0());//Zone.zone0() 指华东
 Configuration cfg = new Configuration(Zone.zone1());//华北
 Configuration cfg = new Configuration(Zone.zone2());//华南

上传本地文件并且生成外链:

private String path = "C:\\Users\\ASUS\\Desktop\\课表.jpg";
    @Test
    public void haha() {
        //构造一个带指定 Region 对象的配置类
        Configuration cfg = new Configuration(Region.region2());
//...其他参数参考类注释

        UploadManager uploadManager = new UploadManager(cfg);
//...生成上传凭证,然后准备上传
        String accessKey = "yours key";
        String secretKey = "yours key";
        String bucket = "ywf";

//默认不指定key的情况下,以文件内容的hash值作为文件名
        String key = null;

        try {
           
            Auth auth = Auth.create(accessKey, secretKey);
            String upToken = auth.uploadToken(bucket);

            try {
                Response response = uploadManager.put(path, key, upToken);
                //解析上传成功的结果
                DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
                System.out.println(putRet.key);
                System.out.println(putRet.hash);

            } catch (QiniuException ex) {
                Response r = ex.response;
                System.err.println(r.toString());
                try {
                    System.err.println(r.bodyString());
                } catch (QiniuException ex2) {
                    //ignore
                    System.out.println(ex2);
                }
            }
        } catch (UnsupportedEncodingException ex) {
            //ignore
            System.out.println(ex);
        }

    }

图片服务器 会给你一个域名,将 域名+ ret.hash
就是图片的访问外链了。

  @Override
    public String saveImageWithPath(MultipartFile file) {
        if(!checkSize(file)) {
            return "ERROR";
        }
        Configuration cfg = new Configuration(Region.region0());//华东的仓库
//...其他参数参考类注释

        UploadManager uploadManager = new UploadManager(cfg);

        String key = null;

        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(buckName);
        String result=null;
        try {
            Response response = uploadManager.put(file.getBytes(), key, upToken);
            //解析上传成功的结果
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            // System.out.println(putRet.key);
            // System.out.println(putRet.hash);
            log.info(putRet.key);
            result = putRet.hash;
        } catch (QiniuException ex) {
            Response r = ex.response;
            log.error(r.toString());
            try {
                log.error(r.bodyString());
            } catch (QiniuException ex2) {
                //ignore
                log.error(String.valueOf(ex2));
            }
        } catch (IOException e) {
            log.error(e.getMessage());
        }
        //图片路径: 域名+'/'+ 具体的hash值
        return prefix+'/'+result;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值