先缓存到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;
}
}