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
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值