You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Kurt Kurniawan <kk...@bigw.com.au> on 2005/02/28 23:48:08 UTC

[NET] Error in retrieving more than one file.

Hello all,

We have this code, where we're trying to pickup more than one file in the remote machine and put locally.
But I always get the value of ios=null on the second iteration when it's trying to pick up the second file.

The FtpClient seems to be still connected.

I can reconnect and pick that file again, but that is not an efficient way to do.

Is there any  reasons why is this happened ? Can anyone give me some hints of what happened ?

The code is just examples of what my project does, I have to do this in this way instead of using retrieveFile(java.lang.String remote, java.io.OutputStream local), because I need it to return as InputStream.

Thank you for any help.


Kurt

code:

public class ExperimentFTP {


	public static void main(String[] args) {
		System.out.println("Start process");
	
		Log log=LogFactory.getLog(ExperimentFTP.class);
		String directory="/usr/egate/tst";
		String fileregex="rm_lo_ftp*";
		Vector filesV=new Vector();
		Pattern ptrn=Pattern.compile(fileregex);
		Matcher match;
		FileOutputStream fos;
	
		FTPClient ftp=new FTPClient();
	
		try{
			ftp.connect("theHost");
			ftp.login("xxx","xxx");
		
			ftp.changeWorkingDirectory(directory);
		
			FTPFile[] files = ftp.listFiles(directory);
		
			//iterate and collect !!
			for(int f=0;f<files.length;f++){
				//log filename here !!
				log.debug("Filename: "+files[f].getName());
			
				//log --if any match then add to list.
				match=ptrn.matcher(files[f].getName());
				if(match.find()){
					filesV.add(files[f].getName());
					log.debug("Add the file name: "+files[f].getName()+" to the list");
				}
			
			}
		
			//now try to pick up the file and send.
			Enumeration e=filesV.elements();
		
			while(e.hasMoreElements()){
				String fileElement=(String)e.nextElement();
				log.debug("Writing the file: "+fileElement);
			
				InputStream ios=ftp.retrieveFileStream(fileElement);
			
				fos = new FileOutputStream("c:/myDir/"+fileElement);
			
			
				//write to the file
				byte[] buffer=new byte[4096]; //byte to hold the content make it 4KB at one read
				int bytes_read; //number of bytes in buffer
			
				log.debug("Writing the file");
			
				while((bytes_read=ios.read(buffer))!=-1){
					fos.write(buffer,0,bytes_read);
				}
			
			}
		
		
		}catch(IOException e){
		
			log.error("There is error in the process ",e);
		}
	
	}
}



CAUTION: This message may contain confidential information intended only for the use of the addressee named above. If you are not the intended
recipient of this message, any use or disclosure of this message is prohibited. If you received this message in error please notify email Administrators immediately.  You must obtain all necessary intellectual property clearances before doing anything other than displaying this message on your monitor. There is no intellectual property licence.  Any views expressed in this message are those of the individual sender and may not necessarily reflect the views of BIG W Discount Stores.

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org