Show uploaded file (pdf/text/html/xml/image) content on page -Oracle ADF

Sharing is Caring

Hello all
In the previous post
Uploading and downloading files from absolute server path in Oracle ADF (12.1.3)
we saw that how can we upload any file to absolute server path and download the same from there.
Uploading part is done using simple java code to write the file to a specified folder and downloading part is done using <af:fileDownloadActionListener>

Now in this post, I am going to show that how can we display a file on the page directly without downloading it.
So for this, I am using the same sample application that I have used in the previous post, you can download it from here

See the additional steps to show file content on the page-

    • Create a servlet to parse the 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 parse the file into outputStream

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;

import javax.servlet.*;
import javax.servlet.http.*;

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 {
String path = (request.getParameter("path"));

OutputStream os = response.getOutputStream();
//If path is null
if (path.equalsIgnoreCase("No")) {
path = "D:\Document\ItemImage\Item.jpg";
}
if (request.getParameter("path") == "") {
path = "D:\Document\ItemImage\Item.jpg";
}
InputStream inputStream = null;

try {
File outputFile = new File(path);
inputStream = new FileInputStream(outputFile);
BufferedInputStream in = new BufferedInputStream(inputStream);
int b;
byte[] buffer = new byte[10240];
while ((b = in.read(buffer, 0, 10240)) != -1) {
os.write(buffer, 0, b);
}

} catch (Exception e) {

System.out.println(e);
} finally {
if (os != null) {
os.close();
}
if (inputStream != null) {
inputStream.close();
}

}
}
}

 

    • Now open the 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 the source attribute
      So here I am using a servlet as source attribute for this inlineFrame

<af:inlineFrame id="if2"
                                                    source="/previewfileservlet?path=#{bindings.Path.inputValue == null ? 'No' : bindings.Path.inputValue}"
                                                    inlineStyle="height:350px;width:800px;" sizing="preferred"/>

here path of the document is added to pageDef bindings that is passed to the servlet as a parameter

  • Now run this application and check it View Text File-

         View PDF File-

Show uploaded file

Thanks, Happy Learning 🙂

Post Related to file handling in Oracle ADF- 

Show saved file (pdf/text/html/xml/image) content on page from database (BLOB) in ADF Application
Uploading and showing image file from absolute server path -Oracle ADF
Exporting viewObject data in text file using af:fileDownloadActionListener in Oracle ADF (12.1.3)
Creating pdf file using Apache PDFBox API in ADF Faces and opening it in new window -Oracle ADF
Download file from url using Oracle ADF & Java- Download Manager
Reading html source of a webpage (url) using Oracle ADF (af:richTextEditor) & Java

Related Posts

An Oracle ACE, Blogger, Reviewer, Technical Lead working on Oracle ADF

0 thoughts on “Show uploaded file (pdf/text/html/xml/image) content on page -Oracle ADF”

  1. Hi,

    Nice Post.

    I implemented the same using a popup and worked perfectly. But the servlet seems to be accessible even if you are not logged which leaves back many security concerns. If you copy paste the URL for servlet in a new page the file is accessible.

    I was able to add a log in check using session scope variable. Now trying to add a replay attack prevention. Adding a one time token is not working as somehow the loading the page makes multiple requests to the same servlet. Please share if you have a solution.

    Thanks in advance

Leave a Reply

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