Java实现批量下载图片,打包成zip压缩包

2023-02-17 00:00:00 打包 批量 压缩包
/** * 批量下载图片后台逻辑 * @return */
@GetMapping("/downLoad")
public void download(HttpServletRequest request, HttpServletResponse response){ 

	try { 
		String downloadFilename = System.currentTimeMillis()+".zip";//文件的名称
		downloadFilename = URLEncoder.encode(downloadFilename, "UTF-8");//转换中文否则可能会产生乱码
		response.setContentType("application/octet-stream");// 指明response的返回对象是文件流
		response.setHeader("Content-Disposition", "attachment;filename=" + downloadFilename);// 设置在下载框默认显示的文件名
		ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());
// String[] files = new String[]{ 
// "https://ts1.cn.mm.bing.net/th/id/R-C.4a3983757438d048839e2e9afbf1e671?rik=hytT2hgkHaI0dQ&riu=http%3a%2f%2fb.zol-img.com.cn%2fdesk%2fbizhi%2fimage%2f3%2f960x600%2f1367830547991.jpg&ehk=z%2fcn0lolZi9CRbUsat%2b28jS48YwiwwB9exHy3ZmOpjI%3d&risl=&pid=ImgRaw&r=0",
// "https://tse1-mm.cn.bing.net/th/id/OIP-C.TMSwVny5FOF-5injQCrirQHaEo?w=308&h=191&c=7&r=0&o=5&pid=1.7"};
		String[] files = "https://static.bokao2o.com/c.png,https://static.bokao2o.com/b.png,https://static.bokao2o.com/a.png".split(",");
		for (int i=0;i<files.length;i++) { 
			URL url = new URL(files[i]);
			zos.putNextEntry(new ZipEntry(i+".jpg"));
			InputStream fis = url.openConnection().getInputStream();
			byte[] buffer = new byte[1024];
			int r = 0;
			while ((r = fis.read(buffer)) != -1) { 
				zos.write(buffer, 0, r);
			}
			fis.close();
		}
		zos.flush();
		zos.close();
	} catch (UnsupportedEncodingException e) { 
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) { 
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}
/** * 批量下载图片后台逻辑 * @return */
@RequestMapping("/downLoad")
public void downLoad(HttpServletResponse response) throws IOException { 
	List<String> files = Arrays.asList("https://ts1.cn.mm.bing.net/th/id/R-C.4a3983757438d048839e2e9afbf1e671?rik=hytT2hgkHaI0dQ&riu=http%3a%2f%2fb.zol-img.com.cn%2fdesk%2fbizhi%2fimage%2f3%2f960x600%2f1367830547991.jpg&ehk=z%2fcn0lolZi9CRbUsat%2b28jS48YwiwwB9exHy3ZmOpjI%3d&risl=&pid=ImgRaw&r=0",
			"https://tse1-mm.cn.bing.net/th/id/OIP-C.TMSwVny5FOF-5injQCrirQHaEo?w=308&h=191&c=7&r=0&o=5&pid=1.7");  //图片地址集合
	String downloadFilename = System.currentTimeMillis()+".zip";//文件的名称
	downloadFilename = URLEncoder.encode(downloadFilename, "UTF-8");//转换中文否则可能会产生乱码
	response.setContentType("application/octet-stream");// 指明response的返回对象是文件流
	response.setHeader("Content-Disposition", "attachment;filename=" + downloadFilename);// 设置在下载框默认显示的文件名
	ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());
	int i = 0;
	for (String string : files) { 
		URL url = new URL(string);
		HttpURLConnection urlcon=(HttpURLConnection)url.openConnection();
		String message = urlcon.getHeaderField(0);
		if(StringUtils.isNotEmpty(message)){ 
			if (message.startsWith("HTTP/1.1 404")) { 
				continue;
			}
			zos.putNextEntry(new ZipEntry((++i)+".jpg"));
			InputStream fis = urlcon.getInputStream();
			byte[] buffer = new byte[1024];
			int r = 0;
			while ((r = fis.read(buffer)) != -1) { 
				zos.write(buffer, 0, r);
			}
			fis.close();
		}

	}

	zos.flush();
	zos.close();


}
    原文作者:GodOfCode_
    原文地址: https://blog.csdn.net/huangquan__/article/details/126055131
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。

相关文章