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

Posted SDingBa

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 "";
    

以上是关于web下载七牛云上面的图片资源的主要内容,如果未能解决你的问题,请参考以下文章

web上传图片到七牛云服务器

免费的对象存储——七牛云还是腾讯云

Java实现七牛云文件或图片上传下载

Canvas保存图片到七牛云

Canvas保存图片到七牛云

七牛的配置