Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何同时刷新HttpServletResponse中的两个文件_Java_Flush - Fatal编程技术网

Java 如何同时刷新HttpServletResponse中的两个文件

Java 如何同时刷新HttpServletResponse中的两个文件,java,flush,Java,Flush,在调用API时,我希望下载一个excel文件和一个zip文件。 我已经破解了分别下载它们的代码,但是当把它们放在一起时,只有一个文件被下载,而另一个文件没有被下载。 问题是我不能同时使用response.getOutPutStream().flush()或response.flushBuffer() String absolutePath = context.getRealPath("resources/ZipFolders"); String inputFile = Paths.get(abs

在调用API时,我希望下载一个excel文件和一个zip文件。 我已经破解了分别下载它们的代码,但是当把它们放在一起时,只有一个文件被下载,而另一个文件没有被下载。 问题是我不能同时使用
response.getOutPutStream().flush()
response.flushBuffer()

String absolutePath = context.getRealPath("resources/ZipFolders");
String inputFile = Paths.get(absolutePath + "/Attachments.zip").toAbsolutePath().toString();
File finalFile = new File(inputFile);
ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(finalFile));

String absolutePath2 = context.getRealPath("resources/Spreadsheets");
String inputFile2 = Paths.get(absolutePath2 + "/Validation_Spreadsheet.xlsx").toAbsolutePath().toString();
File file = new File(inputFile2);
byte[] bytes = IOUtils.toByteArray(new FileInputStream(file));
ZipEntry zipEntry = new ZipEntry("Validation_Spreadsheet.xlsx");
zipOut.putNextEntry(zipEntry);
zipOut.write(bytes);
zipOut.closeEntry();
zipOut.close();
response.setContentType("application/zip");
response.setHeader("Content-disposition", "attachment;filename=attachment_trial.zip");
response.getOutputStream().write(IOUtils.toByteArray(new FileInputStream(finalFile)));
System.err.println("above flush>>>>>>>>>>>>>>");
response.getOutputStream().flush();

responsetrial.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
responsetrial.setHeader("Content-disposition", "attachment; filename=TransactionErrors.xlsx");
responsetrial.getOutputStream().write(IOUtils.toByteArray(new FileInputStream(file)));
System.err.println("above flush2>>>>>>>>>>>>>>");
responsetrial.getOutputStream().flush();

不可能通过一个HTTP请求下载两个文件。 您需要为此任务分别提出两个请求


如果您需要在一个“HTML按钮”中下载多个文件,您需要编写一些javascript逻辑来实现这一点。

这是否回答了您的问题?我已经读过了,它并没有解决这个问题。您试图通过相同的请求/响应发送两个文件,HTTP并不是为此而设计的。一种方法是压缩这两个文件并发送压缩后的文件。或者设计一种包含两个请求/响应的方法