commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Pablo Perez (JIRA)" <j...@apache.org>
Subject [jira] Commented: (NET-12) [net] FTPClient.retrieveFile() results in 0 byte files
Date Wed, 19 Sep 2007 13:46:43 GMT

    [ https://issues.apache.org/jira/browse/NET-12?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_12528767
] 

Pablo Perez commented on NET-12:
--------------------------------

Rory,

The issue seems to be solved.

This is what I did. First of all, I printed the value returned by getReplyCode().

So, the value that the server was returning was 550.

This meant that I was trying to get a file that didn't exist.

Solution:
Correct the path to the file in the server when doing retrieveFile().
retrieveFile() MUST receive the FULL path to the file. For example: /home/my_username/some_folder/my_file.txt

Maybe this should be clarified in the documentation.

Thanks anyway, Rory!

> [net] FTPClient.retrieveFile() results in 0 byte files
> ------------------------------------------------------
>
>                 Key: NET-12
>                 URL: https://issues.apache.org/jira/browse/NET-12
>             Project: Commons Net
>          Issue Type: Bug
>    Affects Versions: 1.4
>         Environment: Operating System: HP-UX
> Platform: HP
>            Reporter: Steven LeVander
>
> FTPClient's retrieveFile() always returns false and results in 0 byte files
> FTPClient's completePendingCommand() always never returns when uncommented out
> Attempted client on Win2000 and Server on HPUX 11(?), also client and server 
> both on HPUX.
> /lib contains commons-net-1.4.0.jar and jakarta-oro-2.0.8.jar.
> Thanks,
> Steve
> Relative Source Code:
> import org.apache.commons.net.ftp.*;
> import java.io.BufferedInputStream;
> import java.io.BufferedOutputStream;
> import java.io.File;
> import java.io.FileOutputStream;
> import java.io.PrintWriter;
> import java.rmi.dgc.VMID;
> import java.text.DateFormat;
> import java.util.Calendar;
> import java.util.GregorianCalendar;
> import java.util.Date;
> import java.util.zip.ZipEntry;
> import java.util.zip.ZipOutputStream;
> 	private void get(Calendar criteriaDateTime) throws Exception {
>     	FTPClient ftp = null;
>     	FTPFile[] serverFiles = null;
>     	boolean isLoggedIn = false;
> 		Exception exception = null;
> 		Date serverFileDate;
> 		try {
> 	    	// Connect and logon to FTP Server
> 			ftp = new FTPClient();
> 			ftp.connect(server);
> 			isLoggedIn = ftp.login(userId, password);
> 			if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())){
> 				throw new Exception("FTP server refused 
> connection.");
> 			}
> 			Logger.log("ftp post-login reply: " + ftp.getReplyString
> ());
> 			// Use passive mode assuming we are behind a firewall.
> 			ftp.enterLocalPassiveMode();
> 			// default to binary transfer
> 			ftp.setFileType(FTP.BINARY_FILE_TYPE);
> 			Logger.log("ftp status: " + ftp.getStatus());
> 			
> 			// get list of files in FTP server directory
> 			FTPListParseEngine engine = ftp.initiateListParsing
> (serverDirectory);
> 		    if(engine.hasNext()) {
> 		    	serverFiles = engine.getFiles();
> 				Logger.log("Number of files in server 
> directory: " + serverFiles.length);
> 				// what if the file is removed by another 
> process?
> 				
> 				for (int i = 0; i < serverFiles.length; i++) {
> 	
> 					if(serverFiles[i].isFile()){
> 						serverFileDate = serverFiles
> [i].getTimestamp().getTime();
> 		
> 						// select files form the server 
> if file attributes have not changed in x seconds
> 						if (serverFileDate.compareTo
> (criteriaDateTime.getTime()) <= 0) {
> 							if(isZipClientFile){
> 								doFtpZip(ftp, 
> serverFiles[i]);
> 							}
> 							else{
> 								doFtpOnly(ftp, 
> serverFiles[i]);
> 							}
> 		
> 							// delete server file 
> if requested
> 							if(isDeleteServerFile){
> 								ftp.deleteFile
> (serverFiles[i].getName());
> 							}
> 						}
> 					}
> 				}
> 		    }
> 		} catch (Exception e) {
>             Logger.handleError(this.getClass().getName(), "get", e);
> 			exception = e;
> 		}
> 		finally{
> 			// Logout and disconnect from the FTP Server 
> 			if(ftp != null){
> 				if(isLoggedIn)
> 					ftp.logout();
> 				if(ftp.isConnected())
> 					ftp.disconnect();
> 			}
> 		}
> 		if(exception != null){
> 			throw exception;
> 		}
> 	}
> 	private void doFtpOnly(FTPClient ftp, FTPFile serverFile) throws 
> Exception {
> 		DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
> 		int count;
> 		byte data[] = new byte[BUFFER];
> 		FileOutputStream fos = null;
> 		Exception exception = null;
> 		try{
> 			Logger.log("FTP Download: " +
> 					serverFile.getName() +
> 					" with timestamp " +
> 					df.format(serverFile.getTimestamp
> ().getTime()));
> 			
> 			fos = new FileOutputStream(
> 					clientDirectory +
> 					File.separator +
> 					serverFile.getName());
> 			
> 			// stream the ftp contents into an inputstream
> 			// this always returns false
> 			if(!ftp.retrieveFile(serverFile.getName(), fos)){
> 				// throw new Exception("false return from 
> ftp.retrieveFile()");
> 				Logger.log("false return from ftp.retrieveFile
> ()");
> 			}
> 			/*  this command freezes every time when running on 
> Windows */
> 			Logger.log("to ftp.completePendingCommand()");
> 			if(!ftp.completePendingCommand()){
> 				throw new Exception("completePendingCommand() 
> returned false");
> 			}
> 			Logger.log("did ftp.completePendingCommand()");
> 		}
> 		catch(Exception e){
>             Logger.handleError(this.getClass().getName(), "doFtpOnly", e);
> 			exception = e;
> 		}
> 		finally{
> 			// close input stream
> 			if(fos != null)
> 				fos.close();
> 			Logger.log("FTP end");
> 		}
> 		if(exception != null){
> 			throw exception;
> 		}
> 	}

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


Mime
View raw message