web下载七牛云上面的图片资源
2016-10-12 14:24
155 查看
本文将怎么通过浏览器打包下载七牛云服务器上面的图片资源;
1 先讲怎么打包下载吧.ZipOutputStream我用的是这个工具类
创建: ZipOutputStream out = new ZipOutputStream(new FileOutputStream(strZipName));
通过在for里面,对out.putNextEntry(new ZipEntry(“名字” + i+”.png”));
注意,要对资源流进行关闭处理
String[] fileUrl 是对应在七牛云上面的资源,可以直接在浏览器上面直接访问到的资源.
2 web下在对应文件,先创建jsp(不做介绍).
3 对应的控制器
**如果不用压缩打包处理,可以直接获取流后用对应的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 ""; }
相关文章推荐
- Ajax资源下载 - 活动提示的图片
- Ajax资源下载 - 活动提示的图片
- 笨笨图片批量抓取下载 V0.2 beta[C# | WinForm | 正则表达式 | HttpWebRequest | Async异步编程]
- 《PHP和MySQL Web 开发,原书第4版》光盘/源代码/资源/下载
- (原)red-green Image.合成三维立体效果, 红绿眼镜 3D图 ,三维图片的核心算法。googler.cc上面有完善的程序和源码下载。 网上以前的那个有错误,我纠正了下~!~
- 2005年8月11日 MSDN WebCast《Windows移动开发系列课程:设计友好的Windows Mobile 应用程序》资源下载
- 强烈推荐:75个最佳Web设计资源下载
- C# DLL资源文件打包(图片、JS、CSS)[WebResource]
- Ajax资源下载 - 活动提示的图片
- [转]C# DLL资源文件打包(图片、JS、CSS)[WebResource]
- Ajax资源下载 - 活动提示的图片
- Ajax资源下载 - 活动提示的图片
- Ajax资源下载 - 活动提示的图片
- C# DLL资源文件打包(图片、JS、CSS)[WebResource]
- Ajax资源下载 - 活动提示的图片
- Ajax资源下载 - 活动提示的图片
- Ajax资源下载 - 活动提示的图片
- Ajax资源下载 - 活动提示的图片
- ASP.NET Web 开发中的静态资源(JS、CSS、图片)版本控制
- C# DLL资源文件打包(图片、JS、CSS)[WebResource]