Please disable your adblock and script blockers to view this page

Search this blog

Monday 17 April 2017

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

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

  • import;
    import java.sql.Blob;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import javax.servlet.ServletConfig;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    public class PreviewFileServlet extends HttpServlet {
        private static final String CONTENT_TYPE = "text/html; charset=UTF-8";
        public void init(ServletConfig config) throws ServletException {
         * @param request
         * @param response
         * @throws ServletException
         * @throws IOException
        public void doGet(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 ( {
                    Blob blob = rs.getBlob("IMAGE_FILE");
                    BufferedInputStream in = new BufferedInputStream(blob.getBinaryStream());
                    int b;
                    byte[] buffer = new byte[10240];
                    while ((b =, 0, 10240)) != -1) {
                        os.write(buffer, 0, b);
            } catch (Exception e) {
            } finally {
                if (conn != null) {
                    try {
                    } catch (SQLException e) {
                if (os != null) {
        /**Method to get oracle DB conncection object
         * @return
         * @throws Exception
        public static 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:inlineFrame id="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


  1. I like your writing style, Well Explained Ashish

    Thanks for your efforts

  2. Hi Ashish

    Thanks for excellent explanation ,Can we use any other component to preview file ?


  3. Thanks Ashish,
    Please can you update the connection to database and use the built in oracle database connect in ADF?