我有这种方法,我需要知道用户何时结束下载文件。但是在方法结束时,它会打印到控制台“结束”消息,但下载仍在运行。如何跟踪下载活动结束?
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("GET");
response.setContentType("application/zip");
response.setHeader("Content-disposition", "attachment; filename=abc.zip");
File[] files = ...//getting files here
FileOutputStream fos = new FileOutputStream("ABC.zip");
ZipOutputStream zipOut = new ZipOutputStream(fos);
zipOut.setLevel(0);
for (File fileToZip : files) {
FileInputStream fis = new FileInputStream(fileToZip);
ZipEntry zipEntry = new ZipEntry(fileToZip.getName());
zipOut.putNextEntry(zipEntry);
byte[] bytes = new byte[1024];
int length;
while ((length = fis.read(bytes)) >= 0) {
zipOut.write(bytes, 0, length);
}
fis.close();
}
zipOut.close();
fos.close();
File my_file = new File("ABC.zip");
OutputStream out = response.getOutputStream();
FileInputStream in = new FileInputStream(my_file);
byte[] buffer = new byte[4096];
int length;
while ((length = in.read(buffer)) > 0) {
out.write(buffer, 0, length);
}
in.close();
System.out.print("file download start");
out.flush();
System.out.println("file download end");
}