Download multiple files Java

这一生的挚爱 提交于 2021-02-07 09:46:16

问题


I am using the following code to download a file within the 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();        
}

along with this I want to download another file at the same location Results.csv I've tried using the same code above twice but it didn't work.

How to download multiple files without using zipoutputstream?


回答1:


MIME/multipart responses are, as far as I know, not part of the HTTP standard. Some browsers seem to support it, but I recommend against using it.

Instead, you could pack those files into a ZIP file (using a ZipOutputStream), and return that as your response. That's also the way DropBox handles the download of multiple files at once.



来源:https://stackoverflow.com/questions/22933401/download-multiple-files-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!