后端将图片打成zip包返回给前端
我这是因为业务需求前端需要批量下载图片二维码,我这里就直接将图片打成zip包通过IO传给前端
//获取二维码放在系统目录文件image下,这个方法就纯粹的获取图片,暂时放在项目的目录下,大家自便
createImageFile(list);
//将图片文件压缩成zip文件
String fName = fileToZip(response);
//图片打包好后删除图片
boolean b = deleteDir(sourceFilePath);
return fName;
将文件夹压缩成zip文件,这里返回给前端的是压缩文件的名称,前端在根据文件名称获取相应的文件
private static final String sourceFilePath = "src/main/resources/image"; //待压缩的文件夹路径
private static final String zipFilePath = "src/main/resources/zip"; //压缩后存放路径
private static final String fileName = "imageZip"; //文件夹名称
public String fileToZip(HttpServletResponse response) {
//判断文件夹是否存在,不存在则创建
File file = new File(zipFilePath);
if (!file.exists()){
file.mkdirs();
}
boolean flag = false;
File sourceFile = new File(sourceFilePath);
FileInputStream fis = null;
BufferedInputStream bis = null;
FileOutputStream fos = null;
ZipOutputStream zos = null;
if (sourceFile.exists() == false) {
System.out.println("待压缩的文件目录:" + sourceFilePath + "不存在.");
} else {
try {
File zipFile = new File(zipFilePath + "/" + fileName + ".zip");
if (zipFile.exists()) {
System.out.println(zipFilePath + "目录下存在名字为:" + fileName + ".zip" + "打包文件.");
} else {
File[] sourceFiles = sourceFile.listFiles();
if (null == sourceFiles || sourceFiles.length < 1) {
System.out.println("待压缩的文件目录:" + sourceFilePath + "里面不存在文件,无需压缩.");
} else {
fos = new FileOutputStream(zipFile);
zos = new ZipOutputStream(new BufferedOutputStream(fos));
byte[] bufs = new byte[1024 * 10];
for (int i = 0; i < sourceFiles.length; i++) {
//创建ZIP实体,并添加进压缩包
ZipEntry zipEntry = new ZipEntry(sourceFiles[i].getName());
zos.putNextEntry(zipEntry);
//读取待压缩的文件并写进压缩包里
fis = new FileInputStream(sourceFiles[i]);
bis = new BufferedInputStream(fis, 1024 * 10);
int read = 0;
while ((read = bis.read(bufs, 0, 1024 * 10)) != -1) {
zos.write(bufs, 0, read);
}
bis.close();
fis.close();
}
flag = true;
}
}
} catch (FileNotFoundException e) {
ExceptionUtil.throwException("图片打包失败,请联系管理员查看");
} catch (IOException e) {
ExceptionUtil.throwException("图片存入失败,请联系管理员查看");
} finally {
//关闭流
try {
if (null != bis) bis.close();
if (null != zos) zos.close();
if (null != fis) fis.close();
if (null != fos) fos.close();
//打包好压缩包后删除image的图片
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
return fileName;
处理完毕后记得删除图片文件,或压缩包文件
public static boolean deleteDir(String path) {
File file = new File(path);
if (!file.exists()) {//判断是否待删除目录是否存在
System.err.println("文件夹不存在!");
return false;
}
String[] content = file.list();//取得当前目录下所有文件和文件夹
for (String name : content) {
File temp = new File(path, name);
System.gc(); //加上确保文件能删除,不然可能删不掉
if (temp.isDirectory()) {//判断是否是目录
deleteDir(temp.getAbsolutePath());//递归调用,删除目录里的内容
temp.delete();//删除空目录
} else {
if (!temp.delete()) {//直接删除文件
System.err.println("删除失败 :文件名 " + name);
}
}
}
return true;
}
将zip文件传递给前端
//将压缩包传递给前端
//fileName刚刚我们给前端的文件名称
downloadZip(response,zipFilePath + "\\" + fileName + ".zip");
public static void downloadZip(HttpServletResponse response, String path){
response.setContentType("application/zip");
response.setCharacterEncoding("utf-8");
response.setHeader("Content-Disposition", "attachment;filename="+fileName+".zip");
File file = new File(path);
try {
ZipFile zipFile = new ZipFile(file);
InputStream fis = new FileInputStream(zipFile.getFile());
IOUtils.copy(fis, response.getOutputStream());
fis.close();
}catch (Exception e){
e.printStackTrace();
}
//将压缩包删除
deleteDir(zipFilePath);
}