您的位置:首页 > 其它

spingmvc 文件下载打包zip

2016-12-20 10:39 351 查看
先缓存到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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: