?
Solved

Failed on retrieving video file from FTP site in Java.

Posted on 2007-12-05
10
Medium Priority
?
283 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month14 days, 10 hours left to enroll

771 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