Solved

Failed on retrieving video file from FTP site in Java.

Posted on 2007-12-05
10
276 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Printing to a specific printer tray (HP 806dn printer) 3 83
replace a word with other 1 40
object oriented programming comparison 5 56
table example 4 24
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

867 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

16 Experts available now in Live!

Get 1:1 Help Now