IBM®
跳转到主要内容
    中国 [选择]    使用条款
 
 
Select a scope:Search for:    
    首页    产品    服务与解决方案     支持与下载    个性化服务    
跳转到主要内容

developerWorks 中国  >  Grid computing  >

Transferring files with GridFTP

Listing 3: Transferring data

developerWorks

Return to article.


Listing 3: Transferring data
        
				
        
	/**
	 * Transfer a file from a remote host
	 * @param remoteFile Remote host file must exist
	 * @param localFile Whre should the remote file be stored
	 * @transferType FTP transfer type: (ASCII/BINARY)
	 */
	public void download(String remoteFile, String localFile, int transferType) {
		try {
			// remote file size
			long size = client.getSize(remoteFile);

			// check if remote file exists, if not an exeception will be thrown...
			if ( client.exists(remoteFile) ) {
				client.setType(transferType);
				
        
				/** required to transfer multiple files **/
				client.setLocalPassive();
				client.setActive();
				
				final FileOutputStream fos = new FileOutputStream(localFile);

				// get file, use the DataSink interface to write incoming data
				// Implement it to provide your own ways of storing data.
				// It must be thread safe; in parallel transfer mode several streams may attempt to write.
				client.get(remoteFile, new DataSink(){
					public synchronized void write(Buffer buffer)
							throws IOException
					{
						System.err.println("received " + buffer.getLength() + " bytes");
						fos.write(buffer.getBuffer());
					}
					public void close()	throws IOException
					{
						// close File output streams
						fos.flush();
						fos.close();
					};
				}, null);

				// transfer ok
				logger.info("Successfully transfered: " + remoteFile + " to " + localFile + " size: " + size);
			}
			else {
				System.err.println(remoteFile +  " doesn't exist");
			}
		}
		catch (Exception e) {
			e.printStackTrace();
		}
    }


      

Return to article.

    关于 IBM 隐私条约 联系 IBM 使用条款