Download file from url using Oracle ADF & Java- Download Manager

Sharing is Caring

In this tutorial, I will show you how to download file from its URL (web)
as- http://www.tutorialspoint.com/java/java_tutorial.pdf
This will work as a simple download manager, you can add file URL to download it and save it.
This tutorial makes use of FileHandling and java.net.URL class in java.

  • User Interface is very simple to design as it has only one input text and one button, so create a Fusion web application, and a bounded task flow with a page fragment in it
  • Now drag an Input text to enter URL and a button to perform an action on it from the Component Palette.
  • Bind input text to bean to get its value from the page

 private RichInputText fileUrlBind;

    public void setFileUrlBind(RichInputText fileUrlBind) {
        this.fileUrlBind = fileUrlBind;
    }

    public RichInputText getFileUrlBind() {
        return fileUrlBind;
    }

 

  • Now create actionListener to managed bean and write code for downloading the file from given URL- see the code
public void DownloadFileButton(ActionEvent actionEvent) {
        try {
            if (fileUrlBind.getValue() != null) {
                String fileUrl = fileUrlBind.getValue().toString();
                if (fileUrl.startsWith("http://")) {
                    String msgNm = fileUrl.substring(7);
cnctmsgBind.setValue("Connecting to " + msgNm + "....");
URL url = new URL(fileUrl);
url.openConnection();
InputStream reader = url.openStream();

FileOutputStream writer =
                        new FileOutputStream("C:/javadrive." + fileUrl.substring(fileUrl.lastIndexOf(".")));
                    byte[] buffer = new byte[153600];
                    int totalBytesRead = 0;
                    int bytesRead = 0;

dwnldMsgBind.setValue("Reading file 150KB blocks at a time");
                    while ((bytesRead = reader.read(buffer)) > 0) {
writer.write(buffer, 0, bytesRead);
buffer = new byte[153600];
totalBytesRead += bytesRead;
                    }

alerMsgBind.setValue("File is downloaded successfully, look at your c drive :-)");
writer.close();
reader.close();
                } else {
FacesMessage errMsg = new FacesMessage("Something went wrong");
errMsg.setSeverity(FacesMessage.SEVERITY_ERROR);
errMsg.setDetail("Example- http://www.javadrive.co.in/java.pdf");
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(fileUrlBind.getClientId(), errMsg);
                }
            }
        } catch (MalformedURLException e) {
FacesMessage errMsg = new FacesMessage("Something went wrong");
errMsg.setSeverity(FacesMessage.SEVERITY_ERROR);
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, errMsg);
e.printStackTrace();
        } catch (IOException e) {
FacesMessage errMsg = new FacesMessage("Something went wrong");
errMsg.setSeverity(FacesMessage.SEVERITY_ERROR);
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, errMsg);
e.printStackTrace();
        }
    }

  • Now Run your application and see downloaded file in your c drive as a path is hardcoded in bean
Download file

 

  •  See your downloaded file in c drive

Cheers 🙂 Happy Learning

Related Posts

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

Leave a Reply

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