Sun. Oct 24th, 2021

Requirement: I want to pass an URL in an inputText, then click on the Button which will download the file say for example it will download the PNG file. Sample URL is shown below.

Solution: For the solution of the above requirement follow the steps as shown below:

Step 1: Create an ADF Fusion Web Application. Create a demo.jspx page.

Step 2: Drag and drop af:panelBox and set text=”Download File from URL Demonstration” and showDisclosure=”false”

Drag and drop af:panelGroupLayout inside af:panelBox

Step 3: Inside the af:panelGroupLayout drag and drop af:inputText and set label=”File URL” , contentStyle=”width:550px;font-weight:bold;”, and binding=”#{DownloadFileBean.fileUrlBinding}”

Then, drag and drop af:spacer and set height=”10″

Now, drag and drop af:button and set text=”Click to Download” and actionListener=”#{DownloadFileBean.downloadFileActionListener}”

Inside the toolbar facet drag and drop af:group

Inside the af:group drag and drop af:outputText and set binding=”#{DownloadFileBean.connectingUrlBinding}”
and partialTriggers=”b1″

Drag and drop af:spacer and set width=”10″ and height=”10″

Drag and drop af:outputFormatted and set partialTriggers=”b1″, binding=”#{DownloadFileBean.statusMessageBinding}”, and inlineStyle=”font-weight:bold;color:red;”

Thus, the complete code for the demo.jspx is shown below.

<?xml version='1.0' encoding='UTF-8'?>
<jsp:root xmlns:jsp="" version="2.1" xmlns:f=""
    < contentType="text/html;charset=UTF-8"/>
        <af:document title="demo.jspx" id="d1">
            <af:form id="f1">
                <af:panelBox text="Download File from URL Demonstration" id="pb1" showDisclosure="false">
                    <af:panelGroupLayout id="pgl1">
                        <af:inputText label="File URL" id="it1" contentStyle="width:550px;font-weight:bold;"
                        <af:spacer id="s3" height="10"/>
                        <af:button text="Click to Download" id="b1"
                    <f:facet name="toolbar">
                        <af:group id="g1">
                            <af:outputText id="ot2" binding="#{DownloadFileBean.connectingUrlBinding}"
                            <af:spacer width="10" height="10" id="s1"/>
                            <af:outputFormatted id="of1" partialTriggers="b1"

And the compete code for the file is shown below:




import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;


public class DownloadFileBean implements Serializable {
    private RichInputText fileUrlBinding;
    private RichOutputText connectingUrlBinding;
    private RichOutputFormatted statusMessageBinding;

    public DownloadFileBean() {

    public void setFileUrlBinding(RichInputText fileUrlBinding) {
        this.fileUrlBinding = fileUrlBinding;

    public RichInputText getFileUrlBinding() {
        return fileUrlBinding;

    public void setConnectingUrlBinding(RichOutputText connectingUrlBinding) {
        this.connectingUrlBinding = connectingUrlBinding;

    public RichOutputText getConnectingUrlBinding() {
        return connectingUrlBinding;

    public void setStatusMessageBinding(RichOutputFormatted statusMessageBinding) {
        this.statusMessageBinding = statusMessageBinding;

    public RichOutputFormatted getStatusMessageBinding() {
        return statusMessageBinding;

    public void downloadFileActionListener(ActionEvent actionEvent) {
        try {
            if (fileUrlBinding.getValue() != null) {
                String fileUrl = fileUrlBinding.getValue().toString();
                if (fileUrl.startsWith("http://")) {
                    String msgNm = fileUrl.substring(7);
                    connectingUrlBinding.setValue("Connecting to " + msgNm + "....");
                    URL url = new URL(fileUrl);
                    InputStream reader = url.openStream();
                    FileOutputStream writer =
                        new FileOutputStream("C:\\Users\\Susanto\\Desktop\\Download\\MyFile." +
                    byte[] buffer = new byte[153600];
                    int totalBytesRead = 0;
                    int bytesRead = 0;
                    while ((bytesRead = > 0) {
                        writer.write(buffer, 0, bytesRead);
                        buffer = new byte[153600];
                        totalBytesRead += bytesRead;
                    statusMessageBinding.setValue("File is downloaded successfully. Please look at (C:\\Users\\Susanto\\Desktop\\Download\\)");
                } else {
                    FacesMessage errMsg = new FacesMessage("Error occured. Please contact your Administrator.");
                    errMsg.setDetail("File error");
                    FacesContext context = FacesContext.getCurrentInstance();
                    context.addMessage(fileUrlBinding.getClientId(), errMsg);
        } catch (Exception e) {
            FacesMessage errMsg = new FacesMessage("Error occured. Please contact your Administrator.");
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage(null, errMsg);

Save all and run the application. Thus, the ran application is shown below:

Provide the value for the File URL as and click on the Click to Download Button. Thus, we will get the output as shown below.

Go to the folder(C:\Users\Susanto\Desktop\Download\) where the file was downloaded and we can see the file is present in that folder as shown below:

Hence, the solution to our requirement.

If you like the post please comment, share, and do join me on Facebook. Please subscribe to my YouTube Channel for video tutorials.

Thanks & Regards,
Susanto Paul

 344 total views,  2 views today

By Susanto Paul

Susanto is an Oracle ACE, a qualified MCA, MBA, and a highly-skilled Senior Oracle Specialist. He is an enthusiastic Blogger and YouTuber who helps learners to solve their complex problems more efficiently. He has 9+ years of experience in multiple technologies like AWS, Oracle ADF, Oracle APEX, Oracle JET, Oracle VBCS, Oracle IDCS, Oracle PL/SQL, Oracle Integration Cloud, Java, JDBC, Servlets, JSP, Spring, Hibernate, HTML5, CSS3, JavaScript, TypeScript, NodesJS, Angular, MySQL, Oracle WebLogic Server, JUnit, JMeter, Selenium Web Driver, etc. He is a certified: Oracle Certified Professional Java SE 6 Programmer, Oracle ADF 11g Certified Implementation Specialist, Oracle Cloud Platform Application Integration 2020 Certified Specialist, Oracle Cloud Infrastructure Foundations 2020 Certified Associate, and Oracle Cloud Infrastructure Developer 2020 Certified Associate

Leave a Reply

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