Pages - Menu

Sunday, September 15, 2013

Servlet Read Database Blob Images || Oracle ADF

Today,
i let Script speak about itself !!, simple Servlet to get Image Files from Database based on parameters specified and using weblogic datasource created before and blob data types, to be used in Oracle ADF Applications and configured in Web.xml file.
  • Calling the Servlet
<af:activeImage shortDesc=”#{bindings.CompanyLogo.hints.tooltip}”
id=”it4”
inlineStyle=”height:190px; width:180px;”
source=”/imageservlet?id=#{bindings.CompanyId.inputValue}&amp;type=company”
binding=”#{ImageBean.companyLogo}”
partialTriggers=”ctb7 ctb8 ctb9 ctb6”/>
  • Configured in Web.xml file
<servlet>
        <servlet-name>ImageServlet</servlet-name>
        <servlet-class>Beans.ImageServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>ImageServlet</servlet-name>
        <url-pattern>/imageservlet</url-pattern>
    </servlet-mapping>
  • Finally Servlet Script
package Beans;

import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

public class ImageServlet extends HttpServlet {
    private static final String CONTENT_TYPE = “text/html; charset=UTF-8”;

    public void init(ServletConfig config) throws ServletException {
        super.init(config);
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType(CONTENT_TYPE);
        String imageId = request.getParameter(“id”);
        String type = request.getParameter(“type”);
        OutputStream os = response.getOutputStream();
        Connection conn = null;
        try {
            Context ctx = new InitialContext();
            //Datasource as defined in <res-ref-name> element of weblogic.xml
            DataSource ds = (DataSource)ctx.lookup(“cwc”);
            conn = ds.getConnection();

            String sql = null;
            String dec = null;
            if (type.equals(“company”) || type.contains(“company”)) {

                sql = “SELECT company_id, company_logo ” + “FROM company ” + “WHERE company_id = ?”;
                dec = “company_logo”;

            } else {

                sql = “SELECT user_id, logo ” + “FROM users ” + “WHERE user_id = ?”;
                dec = “logo”;
            }

            PreparedStatement statement = conn.prepareStatement(sql);

            statement.setString(1, imageId);
            ResultSet rs = statement.executeQuery();
            if (rs.next()) {
                Blob blob = rs.getBlob(dec);
                BufferedInputStream in = new BufferedInputStream(blob.getBinaryStream());
                int b;
                byte[] buffer = new byte[10240];
                while ((b = in.read(buffer, 0, 10240)) != -1) {
                    os.write(buffer, 0, b);
                }
                os.close();
            } else {

                ServletContext sc = getServletContext();
                String filename = “”;
                if (type.equals(“company”) || type.contains(“company”)) {

                    filename = sc.getRealPath(“/images/unknown.jpg”);

                } else {
                    filename = sc.getRealPath(“/images/user.png”);
                }
                String mimeType = sc.getMimeType(filename);
                response.setContentType(mimeType);
                File file = new File(filename);
                response.setContentLength((int)file.length());
                FileInputStream in = new FileInputStream(file);
                OutputStream out = response.getOutputStream();
                byte[] buf = new byte[1024];
                int count = 0;
                while ((count = in.read(buf)) >= 0) {
                    out.write(buf, 0, count);
                }
                in.close();
                out.close();
            }

        } catch (Exception e) {
           
            ServletContext sc = getServletContext();
            String filename = “”;
            if (type.equals(“company”) || type.contains(“company”)) {

                filename = sc.getRealPath(“/images/unknown.jpg”);

            } else {
                filename = sc.getRealPath(“/images/user.png”);
            }
            String mimeType = sc.getMimeType(filename);
            response.setContentType(mimeType);
            File file = new File(filename);
            response.setContentLength((int)file.length());
            FileInputStream in = new FileInputStream(file);
            OutputStream out = response.getOutputStream();
            byte[] buf = new byte[1024];
            int count = 0;
            while ((count = in.read(buf)) >= 0) {
                out.write(buf, 0, count);
            }
            in.close();
            out.close();
           
        } finally {
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException sqle) {
                System.out.println(“SQLException error”);
            }
        }
    }
   
    // Wael Abdeen
}

No comments:

Post a Comment