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!!
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();
Check the return value of retrieveFile
And make sure you're not ignoring exceptions
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,
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,
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
Test it thoroughly without Java to eliminate general network problems
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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)
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());
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.