FileDocCategorySizeDatePackage
DBPDFReader.javaAPI DocExample1932Wed Apr 05 11:25:42 BST 2000None

DBPDFReader.java

/*
 * This example is from the book "Java Enterprise in a Nutshell".
 * Copyright (c) 1999 by O'Reilly & Associates.  
 * You may distribute this source code for non-commercial purposes only.
 * You may study, modify, and use this example for any purpose, as long as
 * this notice is retained.  Note that this example is provided "as is",
 * WITHOUT WARRANTY of any kind either expressed or implied.
 */

import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class DBPDFReader extends HttpServlet {

  Connection con;

  public void init(ServletConfig config) throws ServletException {
    super.init(config);
    try {
      Class.forName("oracle.jdbc.driver.OracleDriver");
      con = DriverManager.getConnection("jdbc:oracle:oci7:@DBHOST", 
                                        "user", "passwd");
    }
    catch (ClassNotFoundException e) { 
      throw new UnavailableException(this, "Couldn't load OracleDriver");
    }    
    catch (SQLException e) { 
      throw new UnavailableException(this, "Couldn't get db connection");
    }    
  }

  public void doGet(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException {
    
    try {
      res.setContentType("application/pdf");
      ServletOutputStream out = res.getOutputStream();
     
      Statement stmt = con.createStatement();
      ResultSet rs = stmt.executeQuery(
        "SELECT PDF FROM PDF WHERE PDFID = " + req.getParameter("PDFID"));

      if (rs.next()) {
        BufferedInputStream pdfData =
          new BufferedInputStream(rs.getBinaryStream("PDF"));
        byte[] buf = new byte[4 * 1024];  // 4K buffer
        int len;
        while ((len = pdfData.read(buf, 0, buf.length)) != -1) {
          out.write(buf, 0, len);
        }
      }
      else {
        res.sendError(res.SC_NOT_FOUND);
      }
    }
    catch(SQLException e) {
      // Report it
    }
  }
}