我们的要求是从FTP服务器下载文件,我们一个接一个地按顺序使用下载文件,这需要花费大量时间。
顺序代码:
for(String mediaValue: generatedMediaTypes) {
// Logic here is getting the user selected files from Ui and keep it in string array str
try {
ddownloadMultipleNavDbs(str[i]),uniqueID);
//now we download all the files here with downloadfile logic with help of unique id we are a folder by that name and keep all this files in that folder
} catch (Exception ex) {
logger.error(ex.getMessage());
}
}
现在将所有文件下载到uniqueID文件夹后,我会压缩并通过downloadZip(request,uniqueID)
发送给客户:
public void downloadZipFile (HttpServletResponse response, String suuid) throws IOException
{
try
{
ZipOutputStream zipOutputStream = new ZipOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
File loadableFolder = new File(uniqueID);
String timeStamp = new java.sql.Timestamp(Calendar.getInstance().getTime().getTime()).toString().replaceAll(
"-", "_").replaceAll(" ", "_").replaceAll(":", "_");
response.setHeader(
"Content-Disposition",
"attachment; filename=" + "File" + timeStamp.substring(
0, timeStamp.indexOf(".")) + ".ZIP");
File[] loadableMedias = //here we get the files from that uinqueID folder
ZipUtil.addFilesToZipStream(Medias, Folder,
zipOutputStream);
zipOutputStream.close();
response.flushBuffer();
response.getOutputStream().flush();
response.getOutputStream().close();
}
它在上面的代码中运行得很好。 当我使用executorservice并行下载文件时
executorService.submit(new myThread(str[i]),uniqueID);
我面临错误:
getOutputStream()已经关闭
有人可以解释一下我为什么会遇到这个错误吗?
为什么我们会遇到此错误以及如何解决?