4 Replies Latest reply: Mar 29, 2011 7:04 AM by r035198x RSS

    Servlet Download Problem

    839431
      Hi,

      I am using a servlet for downloading my octet-stream file from server. Its working fine when the size of the file is above 0 bytes. If the file size is 0 bytes the file is not downloaded and I am getting http 502 error in server.

      Anybody have solution for this issue?

      Here is my code:


      HttpSession session = null;
      response.reset();
      session = request.getSession();
      try {
      String fileTest = null;
      String file = null;

      fileTest = (String) session.getAttribute("DOWNLOADSESSION");

      file = fileTest.substring(fileTest.lastIndexOf("/") + 1,fileTest.length());


      response.setContentType("application/binary");
      response.setHeader("Content-Disposition","attachment; filename=" + file + ";");

      FileInputStream fin = new FileInputStream(fileTest);
      ServletOutputStream out = response.getOutputStream();

      byte[] b = new byte[fin.available()];
      int len = 0;
      while ((len = fin.read(b)) != -1) {
      out.write(b, 0, len);
      }

      out.flush();
      out.close();
      return;
      }
      catch (Exception e) {
      LOG.error("Exception while processing request::"+e.getMessage());
      }