title: Downloading attachment from database using java
body: <p>I have used the following code for downloading the attachment from database but the downloaded file is of 0 bytes. What is the cause and how can I fix it? Here is the code:</p>
  
  <pre><code>import java.io.BufferedInputStream;
  import java.io.IOException;
  import java.io.OutputStream;
  import java.io.PrintWriter;
  import java.sql.Connection;
  import java.sql.DriverManager;
  import java.sql.PreparedStatement;
  import java.sql.ResultSet;
  import javax.servlet.ServletException;
  import javax.servlet.annotation.WebServlet;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  
  /**
   *
   * @author ROHIT
   */
  @WebServlet(name = "DownloadAttachment", urlPatterns = {"/DownloadAttachment"})
  public class DownloadAttachment extends HttpServlet {
  
      /** 
       * Processes requests for both HTTP &lt;code&gt;GET&lt;/code&gt; and &lt;code&gt;POST&lt;/code&gt; methods.
       * @param request servlet request
       * @param response servlet response
       * @throws ServletException if a servlet-specific error occurs
       * @throws IOException if an I/O error occurs
       */
      protected void processRequest(HttpServletRequest request, HttpServletResponse response)
              throws ServletException, IOException {
  
  
          String mailID = request.getParameter("username");
          String fileName = request.getParameter("filename");
  
          response.setContentType("application/msword");
          response.setHeader("Content-Disposition", "attachment; filename=" + "\"" + fileName + "\"");
  
          OutputStream os = null;
          try {
              os = response.getOutputStream();
          } catch (IOException ioe0) {
              ioe0.printStackTrace();
          }
  
  
          String driverclass="oracle.jdbc.driver.OracleDriver";
          String dburl="jdbc:oracle:thin:@ROHIT-PC:1521:XE";
          String dbuser="system";
          String dbpassword="jaihanuman";
  
          PreparedStatement ptmt = null;
          Connection con = null;
          ResultSet rs;
  
  
          try {
  
                  Class.forName(driverclass);
              con=DriverManager.getConnection(dburl,dbuser,dbpassword);
                  String sql = "select filedata from attachfile where filename = ? where mailid = ?";
                  ptmt=con.prepareStatement(sql);
                  ptmt.setString(1, fileName);
                  ptmt.setString(2, mailID);
                  rs = ptmt.executeQuery();
  
  
              while (rs.next()) {
                  BufferedInputStream pdfData = new BufferedInputStream(rs.getBinaryStream("filedata"));
                  byte[] buf = new byte[100 * 1024];
                  int len;
                  while ((len = pdfData.read(buf, 0, buf.length)) != -1) {
                      os.write(buf, 0, len);
                  }
              }
              rs.close();
              con.close();
  
          } catch (Exception ee) {
              System.out.println("Failure in " + ee.toString());
              ee.printStackTrace();
          }
  
      }
  
  // &lt;editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."&gt;
  
  /** 
   * Handles the HTTP &lt;code&gt;GET&lt;/code&gt; method.
   * @param request servlet request
   * @param response servlet response
   * @throws ServletException if a servlet-specific error occurs
   * @throws IOException if an I/O error occurs
   */
  @Override
      protected
  
  void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
          processRequest(request, response);
  
  
  } 
  
      /** 
       * Handles the HTTP &lt;code&gt;POST&lt;/code&gt; method.
       * @param request servlet request
       * @param response servlet response
       * @throws ServletException if a servlet-specific error occurs
       * @throws IOException if an I/O error occurs
       */
  
          @Override
          protected void
  
  doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
          processRequest(request, response);
  
  
  }
  
      /** 
       * Returns a short description of the servlet.
       * @return a String containing servlet description
       */
  
          @Override
          public String
  
  getServletInfo() {
          return "Short description";
  
  }// &lt;/editor-fold&gt;
  }
  </code></pre>