title: Servlet for download files from a specific folder?
body: <p>I am new to JAVA technology,especially Servlets.I need to make a Web application project which have an upload and a  download files to/from a server(tomcat).I have already an upload servlet,which works fine.</p>
  
  <p>i have also a download servlet,found on the internet.But the problem is that this servlet allows downloading only a specific file,and the path to this specific file is given in the servlet. I need to let the client see the entire content of my upload folder and select which file he wants to download from this folder.</p>
  
  <p>The code of the download servlet is this: </p>
  
  <pre><code>import java.io.DataInputStream; 
  import java.io.File; 
  import java.io.FileInputStream;
  import java.io.IOException;
  import javax.servlet.ServletContext; 
  import javax.servlet.ServletException;
  import javax.servlet.ServletOutputStream;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  
  
  
   public class DownloadServlet extends javax.servlet.http.HttpServlet implements
              javax.servlet.Servlet {
          static final long serialVersionUID = 1L;
          private static final int BUFSIZE = 4096;
          private String filePath;`
  
  public void init() {
      // the file data.xls is under web application folder
  
      filePath = getServletContext().getRealPath("")  + File.separator;// + "data.xls";
  }
  
  protected void doGet(HttpServletRequest request,
          HttpServletResponse response) throws ServletException, IOException {
      File file = new File(filePath);
      int length   = 0;
      ServletOutputStream outStream = response.getOutputStream();
      ServletContext context  = getServletConfig().getServletContext();
      String mimetype = context.getMimeType(filePath);
  
      // sets response content type
      if (mimetype == null) {
          mimetype = "application/octet-stream";
      }
      response.setContentType(mimetype);
      response.setContentLength((int)file.length());
      String fileName = (new File(filePath)).getName();
  
      // sets HTTP header
      response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
  
      byte[] byteBuffer = new byte[BUFSIZE];
      DataInputStream in = new DataInputStream(new FileInputStream(file));
  
      // reads the file's bytes and writes them to the response stream
      while ((in != null) &amp;&amp; ((length = in.read(byteBuffer)) != -1))
      {
          outStream.write(byteBuffer,0,length);
      }
  
      in.close();
      outStream.close();
  }
  }
  </code></pre>
  
  <p>The JSP page is this:</p>
  
  <pre><code>&lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1"
     pageEncoding="ISO-8859-1"%&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt;
  &lt;title&gt;Download Servlet Test&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  Click on the link to download: &lt;a href="DownloadServlet"&gt;Download Link&lt;/a&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>I searched a lot of servlets but all of them were like this...they allowed downloading only a specific file.
  Can anyone help me?
  Thank you very much!</p>