Upload image blob in Data Base Table

 Upload image blob in Data Base Table

We have to show image in browser page for that

Step-1

We need to add pic column in the table




Step-2

Now we have to create application with Employee table create EO,VO.APP MODULE.


Step-3

Create Image.jsff page put fields in this format







 

Code for input file to upload image:-

--------------------------------------------------------------------------------------------

   package view.Bean;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.sql.SQLException;

import javax.faces.application.FacesMessage;

import javax.faces.context.FacesContext;

import javax.faces.event.ValueChangeEvent;

import oracle.binding.BindingContainer;

import oracle.adf.model.BindingContext;

import oracle.binding.OperationBinding;

import oracle.adf.model.binding.DCIteratorBinding;

import oracle.jbo.domain.BlobDomain;

import org.apache.myfaces.trinidad.model.UploadedFile;

public class UploaadImage {

    public UploaadImage() {

    }


    public void uploadImage(ValueChangeEvent valueChangeEvent) {

        // Add event code here...

         try

                {

                  UploadedFile file = (UploadedFile) valueChangeEvent.getNewValue();

                  BindingContainer bindings = BindingContext.getCurrent().getCurrentBindingsEntry();

                  DCIteratorBinding iter = (DCIteratorBinding) bindings.get("EmployeeVOIterator");

                  iter.getCurrentRow().setAttribute("Pic", newBlobDomainForInputStream(file.getInputStream()));

                  OperationBinding op = bindings.getOperationBinding("Commit");

                  op.execute();

                  FacesContext context = FacesContext.getCurrentInstance();

                  context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Image Uploaded Successfully..", null));

                //  getMyInputFileComponent().resetValue();

                }

                catch (Exception e)

                {

                  // TODO: Add catch code

                  e.printStackTrace();

                }

                

                }

                

                private BlobDomain newBlobDomainForInputStream(InputStream in) throws SQLException, IOException {

                  BlobDomain b = new BlobDomain();

                  OutputStream out = b.getBinaryOutputStream();

                  writeInputStreamToOutputStream(in, out);

                  in.close();

                  out.close();

                  return b;

                }

                

                private void writeInputStreamToOutputStream(InputStream in, OutputStream out) throws IOException {

                  byte[] buffer = new byte[8192];

                  int bytesRead = 0;

                  while ((bytesRead = in.read(buffer, 0, 8192)) != -1) {

                      out.write(buffer, 0, bytesRead);

                  }

                }

        

    }


 --------------------------------------------------------------------------------------------

Check this with our application bindings:-


for that we have to add iterator and action in bindings:-


 Step-4

Now create servlet to show image

right click on view controller and select servlets



Copy this URL path for further reference to image


Step-5 

Now here write code for Show Image




 

Code for servlet show image:-

---------------------------------------------------------------------------------------------------------------


import java.io.IOException;

import javax.servlet.*;

import javax.servlet.http.*;

import java.io.BufferedInputStream;

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 ShowImage extends HttpServlet {

    private static final String CONTENT_TYPE = "text/html; charset=UTF-8";

    public void init(ServletConfig config) throws ServletException {

        super.init(config);

    }

    private Connection getConnection() throws Exception

      {

        Context ctx = new InitialContext();

        DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/HRDS");

        Connection conn = ds.getConnection();

        return conn;

      }

      public void doGet(HttpServletRequest request,

                        HttpServletResponse response) throws ServletException,

                                                             IOException {

          response.setContentType(CONTENT_TYPE);

          String id = request.getParameter("id");

          OutputStream os = response.getOutputStream();

          Connection conn = null;

          try

          {

            conn = getConnection();

            PreparedStatement statement =

              conn.prepareStatement("SELECT PIC FROM EMPLOYEES where EMPLOYEE_ID= ? ");

            statement.setInt(1, new Integer(id));

            ResultSet rs = statement.executeQuery();

            if (rs.next())

            {

              Blob blob = rs.getBlob(1);

              if (blob != null)

              {

                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();

              }

            }

          }

          catch (Exception e)

          {

            e.printStackTrace();

         }

          finally

          {

            try

            {

              if (conn != null)

             {

                conn.close();

              }

            }


            catch (SQLException sqle)

            {

              sqle.printStackTrace();

            }

          }

          }

          public void doPost(HttpServletRequest request, HttpServletResponse response)

          throws ServletException, IOException

          {

          doGet(request, response);

          }

          }


 ----------------------------------------------------------------------------------------------------------------------


We have to check this path in app module

configuration>Edit>Data Source Name.




Copy this data source name and paste in show image tab

 

Click on image in UI  pass the source code Employee Id with show image Servlet path

 

/showimage?id=#{bindings.EmployeeId.inputValue}

 

 




Step-6


We have to create main page and drag drop region the select USESUPLOAD=”TRUE’


Step-7

Save and run the page

 

Farhath Saleem

 

Comments

Popular posts from this blog

Programmatically invoke a Popup to create a new row.

POJO(Plain Old Java Object) - Editable Data - DataControl.dcx

Dynamic Tabs in ADF