Search code examples
javafileoutputstream

Convert filleoutputstream to a file java


Good Day

I am downloading a file using ftpclient, and is downloading successfully, I however have a problem converting this to a file

For example I am downloading a file called "helper.pdf" and I want to download the file as is to a local file called "helper.pdf"

Here is my code

FTPClient client = new FTPClient();
  FileOutputStream fos = null;
  try
  {
     client.connect(Host);
     client.login(user_ftp, password_ftp);
     if (client.changeWorkingDirectory("workingdirectory"))
     {            
        fos = new FileOutputStream(Copy_file);
        if (client.retrieveFile(Copy_file, fos))
        {
           //System.out.println("FOS : "+fos.toString());
           //ObjectInputStream restore_file = new ObjectInputStream(fos);
        }            
        fos.close();
     }
     JOptionPane.showMessageDialog(null, "Downloaded", "Info", JOptionPane.INFORMATION_MESSAGE);
  }
  catch (Exception err)
  {
     JOptionPane.showMessageDialog(null, "Error downloading update file", "Error", JOptionPane.ERROR_MESSAGE);
  }

So the question is how do I convert the FileOutputStream to a pdf or save it as a pdf file.


Solution

  • File file = new File("c:/help.pdf");
    fos = new FileOutputStream(file);
    if (client.retrieveFile("help.pdf", fos))