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

Sharing is Caring

Hello All, Previously I have posted about uploading and saving files in database blob column now this post talks about how to show saved file content on the page using ADF Faces inline frame component

In this post I am extending the 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 the file into the output stream, this output stream will be used to show file content further.
      To create servlet right click on the viewController project select New—>From Gallery–>Web Tier–>Servlet

 

 

 

Servlet to preview file

 

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

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;

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 {
super.init(config);
}

/**
 * @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 (rs.next()) {
Blob blob = rs.getBlob("IMAGE_FILE");
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) {
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
 */
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 the page in the 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 a 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 the document is added to pageDef bindings and passed to the servlet as a parameter

    • Now run this application and check it –View Image File
Show image file in inline frame

View Text File

Show saved file

Sample ADF Application-Download

Thanks 🙂 Happy Learning

Related Posts

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

  1. Hi Ashish

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

    Suresh

  2. Hi,

    I can view .png,.jpg,.txt.pdf files. When i try to view doc,docx,xls,xlsx files its directly downloading as a zip file.

Leave a Reply

Your email address will not be published. Required fields are marked *