Link to home
Start Free TrialLog in
Avatar of a122178
a122178

asked on

Failed on retrieving video file from FTP site in Java.

I am using the Apache common api to retrieve a video file from FTP site. Somehow, the file is empty in my linux system. I tried to retrieve xml files which is fine.

I attached my code, please help me out!!
FTPClient ftp = new FTPClient();
      ftp.connect( server );
      ftp.login( username, password );
      System.out.println("Connected to " + 
           server + ".");
      System.out.print(ftp.getReplyString());
 
      // List the files in the directory
      ftp.changeWorkingDirectory( folder );
      FTPFile[] files = ftp.listFiles();
      System.out.println( "Number of files in dir: " + files.length );
      DateFormat df = DateFormat.getDateInstance( DateFormat.SHORT );
      for( int i=0; i<files.length; i++ )
      {
        Date fileDate = files[ i ].getTimestamp().getTime();
        if( fileDate.compareTo( start.getTime() ) >= 0 &&
          fileDate.compareTo( end.getTime() ) <= 0 )
        {
          // Download a file from the FTP Server
          System.out.print( df.format( files[ i ].getTimestamp().getTime() ) );
          System.out.println( "\t" + files[ i ].getName() );
          File file = new File( destinationFolder + 
               File.separator + files[ i ].getName() );
          FileOutputStream fos = new FileOutputStream( file ); 
          ftp.retrieveFile( files[ i ].getName(), fos );
          fos.close();
          file.setLastModified( fileDate.getTime() );
        }
      }
 
      // Logout from the FTP Server and disconnect
      ftp.logout();
      ftp.disconnect();

Open in new window

Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Check the return value of retrieveFile
And make sure you're not ignoring exceptions
Avatar of a122178
a122178

ASKER

The return value is fine. And I do not see any exception.

Now, I see part of the content has been download. The target file is 17MB.. but the retrieve stop at 5.8MB without any exception.

Thanks,
Avatar of a122178

ASKER

Do you think I have to set the timeout or sth.. like that.

>>The target file is 17MB.. but the retrieve stop at 5.8MB without any exception.

Test it thoroughly without Java to eliminate general network problems
ASKER CERTIFIED SOLUTION
Avatar of ysnky
ysnky
Flag of Türkiye image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of a122178

ASKER

Not related to the binary and networking issue too.

I found out the problem. If the file size is too large, the code below should be used instead. The reason is because the restrieveFile method has a thread inside which will stop after a certain time. (maybe)


ftp.retrieveFile( files[ i ].getName(), fos );
 
// should use this 
 
InputStream is = ftp.retrieveFileStream( files[ i ].getName());

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I must say i've noticed some pretty strange behaviour from this API too. I ditched it in favour of another
i have had problem with binary files when i dont set ftp filetype to binary.