Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Failed on retrieving video file from FTP site in Java.

Posted on 2007-12-05
10
Medium Priority
?
286 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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
 
LVL 9

Accepted Solution

by:
ysnky earned 750 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 750 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
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

618 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