views:

56

answers:

1

I want to create a progressBar for an FTP download. The server where I am downloading the file has all of its directories and files hidden. I want to display the progress of the download. Is there any way I can get the file size? Here is my current code:

   FTPclient = new FTPClient();
               FTPclient.setListHiddenFiles(true);
               FTPclient.connect(hostPart);
               FTPclient.login(userName, passWord);
               FTPclient.setFileType(FTP.BINARY_FILE_TYPE);

                InputStream instream = FTPclient.retrieveFileStream(pathExcludingHostIncludingFirstSlash);


            int l;
            byte[] tmp = new byte[2048];
            int updateCounter = 0;
            int bytesDownloaded = 0;
            while ((l = instream.read(tmp)) != -1) {
                fos.write(tmp, 0, l);
                bytesDownloaded+=2048;
                updateCounter++;
                if(updateCounter==3){
                    kilobytesDownloaded=(bytesDownloaded / 1024);
                    publishProgress((String[])null);
                    updateCounter=0;
                }
A: 

There's no way to do this reliably across all FTP services.

The FTP protocol does not provide a way to get file sizes, so you would need to resort to requesting a directory listing and unpicking the (server specific) text that you get back. Furthermore, there is no guarantee that directory listing will be enabled, or that you will have permission to list the directory.

Having said this, some FTP libraries can be configured to attempt to get a file size. For example, with the Apache FTPClient library you can (try to) use the listFiles(String) method and look at the resulting FTPFile object.

EDIT

@Kevin Brock mentions the FTP SIZE command as a possibility.

  • This is command was first defined in RFC 3659 in 2007. A lot of deployed FTP server software is much older than that.
  • SIZE is an optional command; i.e. even RFC 3659 compliant servers are not required to support it.
  • Even if SIZE is supported by a server, the current Apache FTPClient APIs do not include a method for getting the size of a file (apart from via listFiles). Of course, this may change in the future.
  • The ftp4j FTPClient class apparently does implement SIZE and make it available via its fileSize(...) method.
Stephen C
The FTP protocol does have the "SIZE" command (http://www.nsftools.com/tips/RawFTP.htm#SIZE) which is supposed to provide this. Are you saying this is not commonly implemented or that Java clients don't give you access to this?
Kevin Brock
When so I have to pass the file path to listFiles? All the files are hidden I think.
Aymon Fournier
@Aymon - then you've got a problem.
Stephen C
Yup. Oh well thanks for the info though :)
Aymon Fournier