Java多文件压缩下载(可自定义每个压缩文件的文件名)

今天给大家分享一个多文件压缩下载的工具类,可自定义每个压缩文件的文件名。

代码如下:

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DownLoad {

	/**
	 * 多文件压缩下载
	 * @param list (map里面存每个文件的信息:URL:文件路径;NAME:单个文件下载后的文件名)
	 * @param attachmentName 压缩包的名称
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public static boolean downLoadMany(List<Map<String, Object>> list, String attachmentName, HttpServletRequest request, HttpServletResponse response) throws Exception {
		boolean bool = false;
		BufferedInputStream bis = null;
		// 生成一个文件包
		String zipPath = "F:\file/" + attachmentName + ".zip";// 压缩路径
		try {
			ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(zipPath));
			String fileName = "";
			for (int i = 0; i < list.size(); i++) {
				fileName = list.get(i).get("NAME") + fileName.substring(fileName.lastIndexOf("."));
				// 最终文件路径+名称
				String path = list.get(i).get("URL").toString();

				bis = new BufferedInputStream(new FileInputStream(path));

				ZipEntry zipEntry = new ZipEntry(fileName);
				zipOutputStream.putNextEntry(zipEntry);
				// 读取文件内容
				byte[] buffer = new byte[1024]; // 创建存放输入流的缓冲
				int num = -1; // 读入的字节数
				while ((num = bis.read(buffer)) > 0) {
					zipOutputStream.write(buffer, 0, num);
				}
				zipOutputStream.closeEntry();
				bis.close();
			}
			zipOutputStream.close();
			// 调用下载
			downLoad_HTTP(attachmentName + ".zip", request, response);
			bool = true;
			File demoFile = new File(zipPath);
			if (demoFile.exists()) {
				demoFile.delete();
			}
		} catch (Exception e) {
			e.printStackTrace();
			bool = false;
		}
		return bool;
	}

	public static void downLoad_HTTP(String attachment, HttpServletRequest request, HttpServletResponse response) {
		response.reset();
		response.setContentType("application/x-msdownload");
		try {
			response.addHeader("Content-Disposition", "attachment;   filename=" + new String(attachment.getBytes("GB2312"), "ISO-8859-1"));
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		try {
			// 最终文件路径+名称
			String path = "F:\file/" + attachment;

			bis = new BufferedInputStream(new FileInputStream(path));
			bos = new BufferedOutputStream(response.getOutputStream());
			byte[] buff = new byte[1024];
			while ((bis.read(buff, 0, buff.length)) != -1) {
				bos.write(buff, 0, buff.length);
			}
			bos.flush();

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (bos != null)
					bos.close();
				if (bis != null)
					bis.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}

猜你喜欢

转载自blog.csdn.net/m_crayon/article/details/105424469