title: How to use GWT when downloading Files with a Servlet?
body: <p>I am creating a simple project that will allow me to upload and download files using gwt. i am having trouble with the downloading of files that are on my server. </p>
  
  <p>For the file upload i used <a href="http://code.google.com/p/gwtupload/" rel="noreferrer">http://code.google.com/p/gwtupload/</a> and followed the instructions there. My file is stored on the server outside of the website container(on the hard drive), </p>
  
  <p>Now when it comes to the downloading of a file, i want a user to press a download button and whatever item is currently selected will download. i dont really know how this will be done</p>
  
  <p>i know i need a download servlet</p>
  
  <pre><code>public class DownloadAttachmentServlet extends HttpServlet {
  
  @Override
  protected void doPost(HttpServletRequest req, HttpServletResponse resp)
          throws ServletException, IOException {
      // TODO Auto-generated method stub
      super.doGet(req, resp);
  }
  
  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp)
          throws ServletException, IOException {
  
      String fileName = (String) req.getSession().getAttribute("fileName");
  
      YFUser user = (YFUser) req.getSession().getAttribute(TestServiceImpl.SESSION_USER);
      if (user == null)
          throw new ServletException("Invalid Session");
  
      InputStream in = null;
      OutputStream out = resp.getOutputStream();
  
      FileInputStream fIn = new FileInputStream(fileName);
      byte[] buffer = new byte[4096];
      int length;
      while ((length = in.read(buffer)) &gt; 0){
          out.write(buffer, 0, length);
      }
      in.close();
      out.flush();
  
  }
  }
  </code></pre>
  
  <p>for the moment i will just pass a fileName string to retrieve the file for testing</p>
  
  <p>now i am lost at what to do on the client side, i have a simple</p>
  
  <pre><code>public class DownloadFilePanel extends Composite {
  
  
  public DownloadFilePanel(final YFUser user , final String fileName){
  
      final Element downloadIframe = RootPanel.get("__download").getElement();
  
      VerticalPanel content = new VerticalPanel();
      content.add(new Label("Download For this File : " + fileName));
  
      Button button = new Button("Download");
      button.addClickHandler(new ClickHandler(){
          @Override
          public void onClick(ClickEvent event) {
              // i do not know what to do here
      });
              content.add(button);
      initWidget(content);
  
  
  }
  
  
  }
  </code></pre>
  
  <p>above is a simple widget that will supply a panel that will allow for the download of a file based on a fileName</p>
  
  <p>as you can see above,  i do not know what to do to be able to download the file</p>
  
  <p>is there any one that can point me in the right direction?</p>