java 批量下载图片并打包成zip压缩包
项目需求:
对接第三方接口时,因为数据中存储的是图片链接,第三方审核时需要把所有图片以zip压缩包的方式传入到他们ftp服务中。
提示:以下是本篇文章正文内容,下面案例可供参考
话不多说直接看代码,案例1使用的数据库查询出来的方式链接数据做的测试Demo,需要把数据库的链接换成自己的账号和密码。
一、案例(1)
import java.io.*;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;public class DownloadImage {public static void main(String[] args) throws Exception {List<String> imageUrl = new ArrayList<>();try {//连接数据库Class.forName("com.mysql.jdbc.Driver");System.out.println("成功加载MySQL驱动!");String url = "自己数据库链接路径";Connection conn;conn = DriverManager.getConnection(url, "账号", "密码");Statement stmt = conn.createStatement();System.out.println("成功连接到数据库!");//查询数据库中图片,路径是https方式存储的String sql = "SELECT license_photo,juridical_person,valid_surfaces FROM `tb_a_company` WHERE id ='142aa9017a8b422dab14cdc3ebe4d42e'";ResultSet rs = stmt.executeQuery(sql);while (rs.next()) {//这里getString后的数字代表筛选出的结果所在的列数,从1开始计数imageUrl.add(rs.getString(1));imageUrl.add(rs.getString(2));imageUrl.add(rs.getString(3));System.out.println();}rs.close();stmt.close();conn.close();} catch (Exception e) {e.printStackTrace();}String url = "E:\\测试" + ".zip";File zipFile = new File(url);// 调用压缩方法zipFiles(imageUrl, zipFile);System.out.println("文件下载成功");}public static void zipFiles(List<String> srcFiles, File zipFile) {try {if (srcFiles.size() != 0) {// 判断压缩后的文件存在不,不存在则创建if (!zipFile.exists()) {zipFile.createNewFile();} else {zipFile.delete();zipFile.createNewFile();}// 创建 FileInputStream 对象InputStream fileInputStream = null;// 实例化 FileOutputStream 对象FileOutputStream fileOutputStream = new FileOutputStream(zipFile);// 实例化 ZipOutputStream 对象ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream);// 创建 ZipEntry 对象ZipEntry zipEntry = null;// 遍历源文件数组//第三方公司要求每个图片的命名方式需要和他们一致所以我使用的普通for循环,如没要求可用增强for循环,后缀名可截取图片的地址。for (int i = 0; i < srcFiles.size(); i++) {// 将源文件数组中的当前文件读入 FileInputStream 流中URL uri = new URL(srcFiles.get(i));fileInputStream= uri.openStream();// 实例化 ZipEntry 对象,源文件数组中的当前文件if (i==0){zipEntry = new ZipEntry("2222222.jpg");}else if (i==1){zipEntry = new ZipEntry("28882222.jpg");}else if (i==2){zipEntry = new ZipEntry("28777222.jpg");}zipOutputStream.putNextEntry(zipEntry);// 该变量记录每次真正读的字节个数int len;// 定义每次读取的字节数组byte[] buffer = new byte[1024];while ((len = fileInputStream.read(buffer)) > 0) {zipOutputStream.write(buffer, 0, len);}}//关闭流zipOutputStream.closeEntry();zipOutputStream.close();fileInputStream.close();fileOutputStream.close();System.out.println("下载完成");}} catch (Exception e) {e.printStackTrace();}}
}
二、案例(2)
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class TestMain {public static void main(String[] args) {//创建list 存放图片List<String> fileList = new ArrayList<>();fileList.add("https://gd1.alicdn.com/imgextra/i1/3033164147/O1CN01EmYJ9J1gVNDyANxU7_!!3033164147.jpg");fileList.add("https://gd4.alicdn.com/imgextra/i4/3033164147/O1CN0103fyrT1gVNDuSdAbu_!!3033164147.jpg");fileList.add("https://img.alicdn.com/imgextra/i3/743792891/O1CN015kzkok1XE7jwEYesk_!!743792891.jpg");String url = "D:\\测试11" + ".zip";File zipFile = new File(url);// 调用压缩方法ZipMultiFileUtil.zipFiles(fileList, zipFile);}
}
public class ZipMultiFileUtil {public static void zipFiles(List<String> srcFiles, File zipFile) {try {if (srcFiles.size() != 0) {// 判断压缩后的文件存在不,不存在则创建if (!zipFile.exists()) {zipFile.createNewFile();} else {zipFile.delete();zipFile.createNewFile();}// 创建 FileInputStream 对象InputStream fileInputStream = null;// 实例化 FileOutputStream 对象FileOutputStream fileOutputStream = new FileOutputStream(zipFile);// 实例化 ZipOutputStream 对象ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream);// 创建 ZipEntry 对象ZipEntry zipEntry = null;// 遍历源文件数组for (int i = 0; i < srcFiles.size(); i++) {// 将源文件数组中的当前文件读入 FileInputStream 流中URL uri = new URL(srcFiles.get(i));fileInputStream= uri.openStream();// 实例化 ZipEntry 对象,源文件数组中的当前文件if (i==0){zipEntry = new ZipEntry("2222222.jpg");}else if (i==1){zipEntry = new ZipEntry("28882222.jpg");}else if (i==2){zipEntry = new ZipEntry("28777222.jpg");}System.out.println(zipEntry);zipOutputStream.putNextEntry(zipEntry);// 该变量记录每次真正读的字节个数int len;// 定义每次读取的字节数组byte[] buffer = new byte[1024];while ((len = fileInputStream.read(buffer)) > 0) {zipOutputStream.write(buffer, 0, len);}}zipOutputStream.closeEntry();zipOutputStream.close();fileInputStream.close();fileOutputStream.close();System.out.println("下载完成");}} catch (Exception e) {e.printStackTrace();}}}
java 批量下载图片并打包成zip压缩包相关推荐
- Java实现批量下载图片,打包成zip压缩包
/*** 批量下载图片后台逻辑* @return*/ @GetMapping("/downLoad") public void download(HttpServletReques ...
- java实现批量下载图片并打包成zip
今天项目经理让做一个功能,就是批量下载图片,整体思路简单,但遇到了一个坑,需求是根据勾选的checkbox,点击下载按钮,弹出选择不同规格的图片对话框,并在下载完成后关闭对话框.坑就在下载完成后关闭对 ...
- 批量下载文件,打包成zip压缩包
批量下载文件,用程序打成zip压缩包在下载 前台传来要下载的url数组 @RequestMapping(value = "/download",method = RequestMe ...
- Java实现文件批量下载,打包成zip压缩包
最近在做一个管理系统的项目,需要实现一个功能,就是批量下载文件,并打包成zip压缩包. 前端通过POST请求传来要下载的文件列表,Java代码实现如下: import java.io.Bu ...
- Java将多个文件打包成ZIP并下载
Java将多个文件打包成ZIP并下载 需求是多个文件需要同时打包成zip压缩文件并下载到本地,首先我需要的是知道下载文件的路径.我有一个专门的sys_file_info表,表中有对应的文件路径.业务表 ...
- 使用filemanager-webpack-plugin将前端项目打包成zip压缩包
使用filemanager-webpack-plugin插件将前端的dist目录打包成zip压缩包,省去手动压缩,进一步可以配合 idea 的Cloud Toolkit插件 或者jekenis进行自动 ...
- java 批量下载图片,批量打包文件并下载
1.批量下载网页上,动态生成的图片到服务器上指定的目录中 2.将目录中的所有的图片打包成zip包 3.删除原来的目录 4.下载zip包 1:由于网页上的图片是全部都是连接动态生成的,所以同事在网上找了 ...
- java后台批量下载文件并压缩成zip下载
因项目需要,将服务器上的图片文件压缩打包zip,下载到本地桌面. 首先,前端js: function doQueryPic() {var picsDate = $("#picsDate&qu ...
- 使用 easyExcel 生成多个 excel 并打包成zip压缩包
前言: 最近项目有个需求,需要生成多个 excel 并打包成 zip下载:由于需要生成的 excel 头字段过多,这里有96个时间段的表头,如果建一个有96个字段的实体不太好,还好 easyExcel ...
最新文章
- jQuery的ajax使用场景讨论(c#)
- HTTPDNS成为移动互联网的标配–原因与原理解析(转)
- 小企业是否能用得上ITIL?
- Linux系统的存储管理
- NodeJS学习笔记: RESTful —— 为本系列做个小结
- MapReduce英语面试
- 世界机器人大会进入最后一天,最受市民关注的是它!
- mybatis使用注解替代xml配置,动态生成Sql
- 红黑树模拟软件_【程序员面试必备】红黑树详细图解
- linux中批量创文件夹的方法
- python list拆分_python里如何把一个list分成两个
- tensorboard的安装与使用
- 超级终端连接华为交换机_win8系统使用超级终端连接华为交换机的操作方法
- Python练习题16:人名独特性统计
- tensorflow:Not creating XLA devices, tf_xla_enable_xla_devices not set
- linux里 read函数用法
- web渗透_一句话木马(webshell)_dvwa环境
- 度过Android开发第一关——配置环境
- 通信基站c语言,中兴C网IP基站BBU开通操作全图解
- 黑鸟每日安全资源推送​第24期(求关注转发,良心推送)