?
Solved

problem with apache ftp

Posted on 2006-05-17
4
Medium Priority
?
273 Views
Last Modified: 2010-03-31
Hi experts

I use apache jakarta ftp  :
import org.apache.commons.net.ftp.FTPClient;

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;
            ftp.connect(ftpServer);
            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)) {
                  ftp.disconnect();
                  throw new Exception("FTP server refused connection.");
            }

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

            // Use passive mode as default because most of us are
            // behind firewalls these days.
            ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
            ftp.enterLocalPassiveMode();

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

how can I solve it ?

thanks
0
Comment
Question by:aquasw
4 Comments
 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 16697407
you could first retrieve the files and check it was there:

http://www.informit.com/guides/content.asp?g=java&seqNum=40&rl=1

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

Expert Comment

by:Mayank S
ID: 16697431
>> 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:

http://jakarta.apache.org/commons/net/api/org/apache/commons/net/ftp/FTPClient.html#retrieveFile(java.lang.String,%20java.io.OutputStream)
0
 
LVL 14

Expert Comment

by:hoomanv
ID: 16697603
>> 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
0
 

Author Comment

by:aquasw
ID: 16699616
Thanks :-)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month13 days, 9 hours left to enroll

750 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