Quantcast
Channel: Ashish Awasthi's Blog
Viewing all articles
Browse latest Browse all 165

Show saved file (pdf/text/html/xml/image) content on page from database (BLOB) in ADF Application

$
0
0

Hello All

Previously I have posted about uploading and saving files in database blob column now this post is about showing saved file content on page using ADF Faces inline frame component

In this post I am extending same previous application, Now see how we can implement this
There are few more steps to go , See the additional steps to show file content on page-




  • Create a servlet to parse file into output stream , this output stream will be used to show file content further.
    To create servlet right click on viewController project select New--->From Gallery-->Web Tier-->Servlet




  • See servlet source code to read BLOB file from database and then parse file into outputStream

  • importjava.io.BufferedInputStream;
    importjava.io.IOException;
    importjava.io.OutputStream;

    importjava.sql.Blob;
    importjava.sql.Connection;
    importjava.sql.DriverManager;
    importjava.sql.PreparedStatement;
    importjava.sql.ResultSet;
    importjava.sql.SQLException;

    importjavax.servlet.ServletConfig;
    importjavax.servlet.ServletException;
    importjavax.servlet.http.HttpServlet;
    importjavax.servlet.http.HttpServletRequest;
    importjavax.servlet.http.HttpServletResponse;

    publicclassPreviewFileServletextends HttpServlet {
    privatestaticfinal String CONTENT_TYPE ="text/html; charset=UTF-8";

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

    /**
    * @param request
    * @param response
    * @throws ServletException
    * @throws IOException
    */
    publicvoiddoGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
    //Get fileName from parameter
    String fileName =(request.getParameter("fname"));

    OutputStream os = response.getOutputStream();
    Connection conn =null;
    try{
    System.out.println("FileName is- "+ fileName);
    //Get database connection object
    conn = getDBConnection();
    //Prepare statement using SQL query to get blob file using file name
    PreparedStatement statement =
    conn.prepareStatement("SELECT IMAGE_FILE FROM FILE_UPD_DWN WHERE FILE_NAME = ?");

    //Pass file name as parameter
    statement.setString(1, fileName);

    //Execute Statement, It'll return a result set
    ResultSet rs = statement.executeQuery();

    //Get BLOB file and read binaryStream and write to outputStream
    if(rs.next()){
    Blob blob = rs.getBlob("IMAGE_FILE");
    BufferedInputStream in =new BufferedInputStream(blob.getBinaryStream());
    int b;
    byte[] buffer =newbyte[10240];
    while((b = in.read(buffer,0,10240))!=-1){
    os.write(buffer,0, b);
    }
    os.close();
    }
    }catch(Exception e){
    System.out.println(e);
    }finally{
    if(conn !=null){
    try{
    conn.close();
    }catch(SQLException e){
    }
    }

    if(os !=null){
    os.close();
    }
    }
    }

    /**Method to get oracle DB conncection object
    * @return
    * @throws Exception
    */
    publicstatic Connection getDBConnection()throws Exception {
    //Type4 Oracle JDBC driver
    String driver ="oracle.jdbc.driver.OracleDriver";
    //Connection url string (hostname,portname, service name)
    String url ="jdbc:oracle:thin:@localhost:1521:XE";
    //DB UserName
    String username ="HR";
    //DB user password
    String password ="hr";

    Class.forName(driver);// load Oracle driver
    //Get connecion object using above detail
    Connection conn = DriverManager.getConnection(url, username, password);
    return conn;
    }
    }

  • Now open page in editor and drop an af:inlineFrame from component palette . inlineFrame is used to create a frame to show any external page or document just like as HTML iframe , it is loaded from source attribute So here i am using servlet as source attribute for this inlineFrame

  • <af:inlineFrameid="if2"inlineStyle="height:350px;width:800px;"sizing="preferred"
    source="/previewfileservlet?fname=#{bindings.FileName.inputValue == null ? 'No' : bindings.FileName.inputValue}"/>

    Here fileName of document is added to pageDef bindings that is passed to servelt as parameter

  • Now run this application and check it

    View Image File


  • View Text File


Sample ADF Application-Download

Thanks :) Happy Learning

Viewing all articles
Browse latest Browse all 165

Trending Articles