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.

http://susantotech.com/wp-content/uploads/2021/02/ADF_P82_16202021_1.png

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=""
          xmlns:af="">
    <jsp:directive.page contentType="text/html;charset=UTF-8"/>
    <f:view>
        <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;"
                                      binding="#{DownloadFileBean.fileUrlBinding}"/>
                        <af:spacer id="s3" height="10"/>
                        <af:button text="Click to Download" id="b1"
                                   actionListener="#{DownloadFileBean.downloadFileActionListener}"/>
                    </af:panelGroupLayout>
                    <f:facet name="toolbar">
                        <af:group id="g1">
                            <af:outputText id="ot2" binding="#{DownloadFileBean.connectingUrlBinding}"
                                           partialTriggers="b1"/>
                            <af:spacer width="10" height="10" id="s1"/>
                            <af:outputFormatted id="of1" partialTriggers="b1"
                                                binding="#{DownloadFileBean.statusMessageBinding}"
                                                inlineStyle="font-weight:bold;color:red;"/>
                        </af:group>
                    </f:facet>
                </af:panelBox>
            </af:form>
        </af:document>
    </f:view>
</jsp:root>

And the compete code for the DownloadFileBean.java file is shown below:

package susantotech.com;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.Serializable;

import java.net.URL;

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

import oracle.adf.view.rich.component.rich.input.RichInputText;
import oracle.adf.view.rich.component.rich.output.RichOutputFormatted;
import oracle.adf.view.rich.component.rich.output.RichOutputText;

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("")) {
                    String msgNm = fileUrl.substring(7);
                    connectingUrlBinding.setValue("Connecting to " + msgNm + "....");
                    URL url = new URL(fileUrl);
                    url.openConnection();
                    InputStream reader = url.openStream();
                    FileOutputStream writer =
                        new FileOutputStream("C:\\Users\\Susanto\\Desktop\\Download\\MyFile." +
                                             fileUrl.substring(fileUrl.lastIndexOf(".")));
                    byte[] buffer = new byte[153600];
                    int totalBytesRead = 0;
                    int bytesRead = 0;
                    while ((bytesRead = reader.read(buffer)) > 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\\)");
                    writer.close();
                    reader.close();
                } else {
                    FacesMessage errMsg = new FacesMessage("Error occured. Please contact your Administrator.");
                    errMsg.setSeverity(FacesMessage.SEVERITY_ERROR);
                    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.");
            errMsg.setSeverity(FacesMessage.SEVERITY_ERROR);
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage(null, errMsg);
            e.printStackTrace();
        }
    }
}

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

Provide the value for the File URL as http://susantotech.com/wp-content/uploads/2021/02/ADF_P82_16202021_1.png 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 *

satta king chart