title: Java OutputStream : Download Multiple Files
body: <p>I am trying to download multiple files using <code>OutputStream</code>. I have created a for loop, looping through a Vector <code>FileS</code> which has file names and download them. But only the first file is getting downloaded. My <code>Vector</code> has <code>FileZero.xml</code> at 0 and <code>FileOne.xml</code> at 1 and only fileZero is getting downloaded. Please help; here's my code</p>
  
  <pre><code>        for (int x = 0; x &lt; FileS.size(); x++) {
              ByteArrayOutputStream buffer = new ByteArrayOutputStream();
              int nRead;
              byte[] data = new byte[1024];
              reportFile = rm.getClass().getResourceAsStream("/folder/" + FileS.elementAt(x));
              try {
                  while ((nRead = reportFile.read(data, 0, data.length)) != -1) {
                      buffer.write(data, 0, nRead);
              }
          } catch (Exception e) {
              e.printStackTrace();
          }
          buffer.flush();
          byte[] byteArray = buffer.toByteArray();
  
          try {
  
              byte bytes[] = null;
              bytes = byteArray;
              response.setContentLength(bytes.length);
              response.setContentType("application/force-download");
              response.setHeader("Content-Disposition", "inline;filename=" + FileS.elementAt(x));
              OutputStream out = response.getOutputStream();
              out.write(bytes);
              out.flush();
              out.close();
  
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
      } catch (Exception e) {
         e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>Also, I tried commenting out <code>out.flush()</code> and <code>out.close()</code>;but it didn't help. I tried putting the <code>OutputStream</code> object outside the loop just to test that also didn't help.</p>