Solved

Failed on retrieving video file from FTP site in Java.

Posted on 2007-12-05
10
273 Views
Last Modified: 2013-12-29
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

0
Comment
Question by:a122178
  • 5
  • 3
  • 2
10 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 20416559
Check the return value of retrieveFile
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20416565
And make sure you're not ignoring exceptions
0
 
LVL 3

Author Comment

by:a122178
ID: 20416720
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,
0
 
LVL 3

Author Comment

by:a122178
ID: 20416730
Do you think I have to set the timeout or sth.. like that.

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20418017
>>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
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 9

Accepted Solution

by:
ysnky earned 250 total points
ID: 20418112
you have to set binary transfer mode to download binary files

add this
ftp.setFileType(FTP.BINARY_FILE_TYPE);
after "System.out.print(ftp.getReplyString());" line.
0
 
LVL 3

Author Comment

by:a122178
ID: 20421645
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

0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 250 total points
ID: 20421721
>>The reason is because the restrieveFile method has a thread inside which will stop after a certain time. (maybe)

Sounds very mysterious ;-)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20421729
I must say i've noticed some pretty strange behaviour from this API too. I ditched it in favour of another
0
 
LVL 9

Expert Comment

by:ysnky
ID: 20425791
i have had problem with binary files when i dont set ftp filetype to binary.
0

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.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
maven java path setting 5 50
groupSum6 challenge 6 48
topping2 challenge 13 62
Groovy:unable to resolve class error 2 31
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

746 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

9 Experts available now in Live!

Get 1:1 Help Now