七牛云,云仓库下载文件---JAVA实现

使用七牛云仓库下载文件:

下载的文件要把pom中的OKHTTP换成3.6.0版本

<!--OKHTTP-->
<dependency>
    <groupId>com.squareup.okhttp3</groupId>
    <artifactId>okhttp</artifactId>
    <version>3.6.0</version>
</dependency>
package com.example.Test;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.util.Auth;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;


public class DownLoad {
    // 设置需要操作的账号的AK和SK					(AK和SK均在七牛云中获得,以下会说明)
    private static final String ACCESS_KEY = "ACCESS_KEY ";
    private static final String SECRET_KEY = "SECRET_KEY ";

    // 要上传的空间								(刚刚新建空间的名称)
    private static final String bucketname = "imgstest";

    // 密钥
    private static final Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);

    //地区分布实例化
    private static final Configuration cfg = new Configuration(Region.huanan());

    //新建空间时,七牛云分配出的域名 (自己可在万网购买域名解析后,绑定到加速域名)
    private static final String DOMAIN = "rilbajsto.hn-bkt.clouddn.com";

    /**
     * 获取下载文件路径,即:donwloadUrl
     */
    public String getDownloadUrl(String targetUrl) {
        String downloadUrl = auth.privateDownloadUrl(targetUrl);
        return downloadUrl;
    }
    /**
     * 下载
     */
    public void download(String targetUrl) {
        //获取downloadUrl
        String downloadUrl = getDownloadUrl(targetUrl);
        //本地保存路径
        String filePath = "E:\\imgs";
        download(downloadUrl, filePath);
    }
    /**
     * 通过发送http get 请求获取文件资源
     */
    private static void download(String url, String filepath) {
        OkHttpClient client = new OkHttpClient();
        System.out.println(url);
        Request req = new Request.Builder().url(url).build();
        Response resp = null;
        try {
            resp = client.newCall(req).execute();
            System.out.println(resp.isSuccessful());
            if(resp.isSuccessful()) {
                ResponseBody body = resp.body();
                InputStream is = body.byteStream();
                byte[] data = readInputStream(is);
                File imgFile = new File(filepath + "\\download.jpeg");   //下载到本地的图片命名
                FileOutputStream fops = new FileOutputStream(imgFile);
                fops.write(data);
                fops.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("Unexpected code " + resp);
        }
    }
    /**
     * 读取字节输入流内容
     */
    private static byte[] readInputStream(InputStream is) {
        ByteArrayOutputStream writer = new ByteArrayOutputStream();
        byte[] buff = new byte[1024 * 2];
        int len = 0;
        try {
            while((len = is.read(buff)) != -1) {
                writer.write(buff, 0, len);
            }
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return writer.toByteArray();
    }
    /**
     * 主函数:测试
     */
    public static void main(String[] args) {
        //构造私有空间的需要生成的下载的链接;
        //格式: http://私有空间绑定的域名/空间下的文件名
        String targetUrl = "http://rilbajsto.hn-bkt.clouddn.com/FlFEDbuL_QygvPXF-lWg5NxxgunD";   //外链域名下的图片路径
        new DownLoad().download(targetUrl);
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
首先,你需要在你的 Vue 项目中安装 `qiniu-js` 包,这个包可以帮助你将文件上传到七牛云。 安装命令:`npm install qiniu-js` 然后,在你的 Vue 组件中引入 `qiniu-js` 包,代码如下: ``` import * as qiniu from 'qiniu-js' ``` 接下来,我们需要实现异步上传文件七牛云的功能。假设我们已经有一个上传文件的方法 `handleUpload`,代码如下: ``` handleUpload(file) { const token = 'your-qiniu-upload-token' // 替换成你的七牛云上传凭证 const key = file.name // 文件名作为七牛云存储的文件名 const config = { useCdnDomain: true, // 是否使用七牛云的 CDN 域名 } const putExtra = { fname: file.name, // 文件原始名字 params: {}, // 上传额外参数 mimeType: null // 上传文件类型 } const observable = qiniu.upload(file, key, token, putExtra, config) const observer = { next(res) { // 上传进度回调 console.log(res.total.percent) }, error(err) { // 上传失败回调 console.log(err) }, complete(res) { // 上传成功回调 console.log(res) } } const subscription = observable.subscribe(observer) // 执行上传操作 } ``` 在上面的代码中,我们通过 `qiniu.upload` 方法来执行文件上传操作,参数依次为: - `file`: 要上传的文件对象 - `key`: 上传到七牛云后的文件名 - `token`: 七牛云上传凭证 - `putExtra`: 上传额外参数,包括文件名、上传参数和文件类型 - `config`: 上传配置,包括是否使用七牛云的 CDN 域名 在上传过程中,我们通过观察者模式来监听上传进度、上传成功和上传失败事件,并执行相应的回调函数。 最后,你可以在你的组件中调用 `handleUpload` 方法来上传文件,例如: ``` <el-upload action="" :before-upload="handleUpload" > <el-button type="primary">点击上传</el-button> </el-upload> ``` 在上面的代码中,我们使用了 Element UI 的上传组件,并将 `before-upload` 属性设置为 `handleUpload` 方法,这样就可以触发文件上传操作了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

牛总来学习了

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值