我想从S3等外部源下载多个文件,创建一个包含所有这些文件的zip
文件,并向用户提供一个链接以下载该文件zip
文件。使用Java将多个文件并行下载到S3中的zip文件
很显然,我可以按顺序处理文件,读取每个文件的输入流并将其写入ZipOutputStream
。
如何并行读取所有输入文件流并写入单个输出流,以便我可以向用户展示下载链接,而不必等到zip
文件完全写入?
我当前的代码:
String realpath = getServletContext().getRealPath("/");
response.setContentType("application/zip");
response.setHeader("Content-Disposition","attachment; filename="+fi.replace('/', '-')+"_"+ff.replace('/', '-')+".zip");
ServletOutputStream out = null;
ZipOutputStream zipfile = null;
try
{
List<Object[]> cfdis = /*my hibernate criteria source, your Database?*/
out = response.getOutputStream();
zipfile = new ZipOutputStream(out);
ZipEntry zipentry = null;
for(Object[] cfdi:cfdis)
{
zipentry = new ZipEntry(cfdi[1].toString()+".xml");
zipfile.putNextEntry(zipentry);
InputStream in = new FileInputStream(new File(realpath+cfdi[0].toString()));
byte[] bytes = new byte[FILEBUFFERSIZE];
int bytesRead;
while ((bytesRead = in.read(bytes)) != -1)
{
zipfile.write(bytes, 0, bytesRead);
}
}
@Muhammadismail而不是将代码放在评论中,请编辑您的问题,并在其中包含代码。 – Cinnam
希望我已经解决了这个问题。 – wOxxOm
非常感谢@wOxxOm –