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
- 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
- Now run this application and check it
View Image File
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; } }
<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
View Text File
Sample ADF Application-Download
Thanks :) Happy Learning
I like your writing style, Well Explained Ashish
ReplyDeleteThanks for your efforts
Hi Ashish
ReplyDeleteThanks for excellent explanation ,Can we use any other component to preview file ?
Suresh
Thanks Ashish,
ReplyDeletePlease can you update the connection to database and use the built in oracle database connect in ADF?
Sorry, I am not getting your question
Delete