web下载七牛云上面的图片资源

本文将怎么通过浏览器打包下载七牛云服务器上面的图片资源;

**如果不用压缩打包处理,可以直接获取流后用对应的out输出就行,不做具体解析;**

1 先讲怎么打包下载吧.ZipOutputStream我用的是这个工具类
创建: ZipOutputStream out = new ZipOutputStream(new FileOutputStream(strZipName));
通过在for里面,对out.putNextEntry(new ZipEntry(“名字” + i+”.png”));
注意,要对资源流进行关闭处理
String[] fileUrl 是对应在七牛云上面的资源,可以直接在浏览器上面直接访问到的资源.

 @Test
    public void downloadFile() throws Exception {
        byte[] buffer = new byte[1024];
        // 生成的ZIP文件名为Demo.zip
        String strZipName = "Demo.zip";
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(strZipName));
        String[] fileUrl = {
                "http://lddn.com/1=4411b99f-e6f3-4d95-9c75-50ef1100f2d9_2016-07-08%2019-31-27%E5%B1%8F%E5%B9%95%E6%88%AA%E5%9B%BE.png",
                "http://dn.com/3=855eb361-0068-4d20-88d8-ae5e440fddcf_2016-10-10%2010-22-20%E5%B1%8F%E5%B9%95%E6%88%AA%E5%9B%BE.png" };

        for (int i = 0; i < fileUrl.length; i++) {
            InputStream inStream = getInputStream(fileUrl[i]);
            // FileInputStream fis = new FileInputStream(file1[i]);
            out.putNextEntry(new ZipEntry("名字" + i+".png"));
            int len;
            // 读入需要下载的文件的内容,打包到zip文件
            while ((len = inStream.read(buffer)) > 0) {
                out.write(buffer, 0, len);
            }
            out.closeEntry();
            inStream.close();
        }
        out.flush();
        out.close();
        System.out.println("生成Demo.zip成功");

    }
上面是普通java文件下载文件到本地的方法;
下面将一下web下载

2 web下在对应文件,先创建jsp(不做介绍).

3 对应的控制器

@ResponseBody
    @RequestMapping("download")
    public BaseVO downloadImage(HttpServletResponse response, AdMaterialParam param) {
    //要添加HttpServletResponse方法,因为浏览器下载要对对应的头数据进行设置;
}

response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(file, "UTF-8"));
adMaterialService.**download**(url);为下载图片返回流数据
try {
                response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(file, "UTF-8"));
                // ZipOutputStream out = new ZipOutputStream(new FileOutputStream(file));
                ZipOutputStream out = new ZipOutputStream(response.getOutputStream());
                byte[] buffer = new byte[1024];
                for (AdImagePath adImagePath : adImagePaths) {
                    String url = qiniuYunUtils.getUrl() + adImagePath.getImageUrl();
                    InputStream inputStream = adMaterialService.download(url);

                    String imageName = adMaterialService.getImageOriginalFileName(adImagePath.getImageUrl());
                    out.putNextEntry(new ZipEntry(imageName));
                    int length;
                    while ((length = inputStream.read(buffer)) > 0) {
                        out.write(buffer, 0, length);
                    }
                    if (out != null) {
                        out.closeEntry();
                    }
                    if (inputStream != null) {
                        inputStream.close();
                    }
                }
                if (out != null) {
                    out.flush();
                    out.close();
                }
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
**对网络资源的下载;**
 @Override
    public InputStream download(String url) {
        try {
            URL imageUrl = new URL(url);
            HttpURLConnection httpURLConnection = (HttpURLConnection) imageUrl.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setConnectTimeout(5 * 1000);
            return httpURLConnection.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
public String getImageOriginalFileName(String url) {
        int indexFirst = url.indexOf("_") + 1;
        if (indexFirst > 0) {
            return url.substring(indexFirst);
        }
        return "";
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在 Node.js 中使用七牛云实现图片下载可以按照以下步骤进行: 1. 首先,保你已经安装了 `qiniu` 模块,可以通过以下命令进行安装: ``` npm install qiniu ``` 2. 在你的代码中引入 `qiniu` 模块: ```javascript const qiniu = require('qiniu'); ``` 3. 设置七牛云的相关配置,包括 Access Key、Secret Key 和存储空间的名称: ```javascript const accessKey = 'your-access-key'; const secretKey = 'your-secret-key'; const bucket = 'your-bucket-name'; ``` 4. 初始化七牛云的配置: ```javascript const mac = new qiniu.auth.digest.Mac(accessKey, secretKey); const config = new qiniu.conf.Config(); const bucketManager = new qiniu.rs.BucketManager(mac, config); ``` 5. 定义一个函数来下载图片,传入图片的 key(文件名)作为参数: ```javascript function downloadImage(key) { return new Promise((resolve, reject) => { const savePath = './images/' + key; // 设置保存图片的路径和文件名 const options = { force: true, // 强制覆盖已存在的文件 }; bucketManager.fetch(bucket, key, savePath, options, (err, respBody, respInfo) => { if (err) { reject(err); } else { resolve(respInfo); } }); }); } ``` 6. 调用 `downloadImage` 函数来下载图片,传入图片的 key: ```javascript downloadImage('your-image-key') .then(respInfo => { console.log('图片下载成功', respInfo); }) .catch(err => { console.error('图片下载失败', err); }); ``` 以上代码会将指定的图片下载到当前目录下的 `./images/` 文件夹中,并输出相应的结果信息。 请注意替换代码中的 `your-access-key`、`your-secret-key` 和 `your-bucket-name` 为你自己的七牛云的相关信息,以及将 `'your-image-key'` 替换为你要下载图片的实际 key。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值