spingmvc 文件下载打包zip

38 篇文章 0 订阅

先缓存到File对象数组,再保存传流:

	public Object doFileDownload(String id_src, OutputStream outputStream) throws IOException {
		HDFSUtils hdfsUtils = new HDFSUtils();
		String[] id_src_array = id_src.split(",");
		if (id_src_array.length > 10) {
			JSONObject result = new JSONObject();
			result.put("result", "0");
			result.put("desc", "Download file overran by ten flles!");
			return result;
		} else if (id_src_array.length == 1) {
			String download_url = doQueryFilePath(id_src);
			String hdfsPath = root_dir + download_url;
			InputStream inputStream = hdfsUtils.downLoadFile(id_src, hdfsPath);
			byte[] b = new byte[4096];
			int length;
			while ((length = inputStream.read(b)) > 0) {
				outputStream.write(b, 0, length);
			}
			outputStream.close();
			inputStream.close();
			return null;
		} else {
			ZipOutputStream out = new ZipOutputStream(new FileOutputStream("report.zip"));
			File[] file = new File[id_src_array.length];
			byte[] buffer = new byte[4096];
			for (int i = 0; i < id_src_array.length; i++) {
				file[i] = new File(id_src_array[i]);
			}
			for (int i = 0; i < file.length; i++) {
				String download_url = doQueryFilePath(id_src_array[i]);
				String hdfsPath = root_dir + download_url;
				InputStream inputStream = hdfsUtils.downLoadFile(id_src, hdfsPath);
				out.putNextEntry(new ZipEntry(download_url.split("/")[download_url.split("/").length - 1]));
				int len;
				while ((len = inputStream.read(buffer)) > 0) {
					out.write(buffer, 0, len);
				}
				out.closeEntry();
				inputStream.close();
			}
			out.close();
			File file1 = new File("report.zip");
			InputStream inputStream = new FileInputStream(file1);
			byte[] b = new byte[1024];
			int length;
			while ((length = inputStream.read(b)) > 0) {
				outputStream.write(b, 0, length);
			}
			inputStream.close();
			return null;
		}
	}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值