problem with apache ftp

Posted on 2006-05-17
Last Modified: 2010-03-31
Hi experts

I use apache jakarta ftp  :

I use it to get files from ftp, but I have problem...
if the file is exist and everything is ok, it gives me the requested file.
but if the file is not exist, I don't get anythig , and I don't get any exception that tells me that there is a problem.
sometimes even the file exist I don't get anything (it returns empty byte array)
I need that this code will tell me if there is any problem to get file

      private byte[] getFileFromFtp(String remote) throws Exception {
            ftp = new FTPClient();

            int reply;
            System.out.println("Connected to " + ftpServer + ".");

            // After connection attempt, you should check the reply code to
            // verify success.
            reply = ftp.getReplyCode();

            if (!FTPReply.isPositiveCompletion(reply)) {
                  throw new Exception("FTP server refused connection.");

            if (!ftp.login(ftpUsername, ftpPassword)) {
                  throw new Exception("Cann't login to ftp.");

            // Use passive mode as default because most of us are
            // behind firewalls these days.

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ftp.retrieveFile(remote, baos);
            if (ftp.isConnected()) {
                  try {
                  } catch (IOException f) {
                        // do nothing
            return baos.toByteArray();
      } // of getFileFromFtp

how can I solve it ?

Question by:aquasw
    LVL 92

    Accepted Solution

    you could first retrieve the files and check it was there:

    or treat an empty byte array as file does not exist.
    LVL 30

    Expert Comment

    >> sometimes even the file exist I don't get anything (it returns empty byte array)

    That should be ok. Since the method does not throw any exceptions on file-not-found, it is designed to return a blank array in case data was not found I guess. It throws an exception when there FTP connection was closed, or when there was a disk I/O exception while getting the data, etc:,
    LVL 14

    Expert Comment

    >> sometimes even the file exist I don't get anything (it returns empty byte array)

    I have not used FTPClient and dont know how it works, but this problem can be due to a file access permission that does not permit read, also you should care about case sensitivity in filename

    Author Comment

    Thanks :-)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
    Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
    Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
    The viewer will learn how to implement Singleton Design Pattern in Java.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now