下载多个文件 Java

2 java download

我正在使用以下代码下载文件WEB-INF

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    String b = null;
    Cookie[] cookies = request.getCookies();
    if (cookies != null) {
         for (Cookie cookie : cookies) {
           if (cookie.getName().equals("thecookie")) {
               b = cookie.getValue();
            }
          }
        }

    BufferedReader br = new BufferedReader(new FileReader(b+"/logs.txt"));
    String path = br.readLine();
    br.close();

    File file = new File(path+"/Results.xlsx");

    FileInputStream fileIn = new FileInputStream(file);
    ServletOutputStream out = response.getOutputStream();
    response.setHeader("Content-Disposition", "attachment; filename=Result.xlsx");
    response.setContentType(
        "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");

    byte[] outputByte = new byte[4096];
    int bytesRead;
    //copy binary contect to output stream
    while((bytesRead = fileIn.read(outputByte)) != -1)
    {
        out.write(outputByte, 0, bytesRead);
    }
    fileIn.close();
    out.flush();
    out.close();        
}
Run Code Online (Sandbox Code Playgroud)

除此之外,我想在同一位置下载另一个文件,Results.csv我已经尝试使用上面两次相同的代码,但它不起作用。

如何在不使用的情况下下载多个文件zipoutputstream

Pet*_*ser 6

据我所知,MIME/多部分响应不是 HTTP 标准的一部分。有些浏览器似乎支持它,但我建议不要使用它。

相反,您可以将这些文件打包到ZIP 文件中(使用ZipOutputStream),并将其作为响应返回。这也是 DropBox 处理同时下载多个文件的方式。