1. 程式人生 > >web下載七牛雲上面的圖片資源

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 "";
    }