您的位置:首页 > 编程语言 > Java开发

使用GridFTP传输文件的简单JAVA代码

2010-11-12 21:59 555 查看
首先要正确安装Java COG kit,

我使用的cog-jglobus-1.8.0-bin文件,解压后,有安装说明

安装成功后,在编写代码的之前,引入相应的JAR包。

package Transfer;

import java.io.*;
import org.globus.ftp.*;
import org.globus.gsi.*;
import org.gridforum.jgss.ExtendedGSSCredential;
import org.gridforum.jgss.ExtendedGSSManager;
import org.ietf.jgss.GSSCredential;

import org.apache.log4j.Logger;
import org.apache.log4j.Level;

/**
* @author ...
*
*/
public class MyGridFTP {

private static Logger logger = Logger.getLogger(MyGridFTP.class.getName());

private GSSCredential cred = null;
private GridFTPClient client = null;
private GlobusCredential globusCred = null;
private static int GRIDFTP_PORT = 2811;

/**
* Initiate GRIDFTP session
* @param host remote GRIDFTP host
* @param port remote GridFTP port (default is 2811)
*/
public MyGridFTP(String host, int port) {
try {
//加载代理证书,使用命grid-proxy-init来生成代理证书
File file = new File("/tmp/x509up_u500");
byte[] data = new byte[(int)file.length()];
FileInputStream in = new FileInputStream(file);
//read in the credential data
in.read(data);
in.close();

ExtendedGSSManager manager = (ExtendedGSSManager)ExtendedGSSManager.getInstance();
cred = manager.createCredential(data, ExtendedGSSCredential.IMPEXP_OPAQUE, GSSCredential.DEFAULT_LIFETIME, null, GSSCredential.INITIATE_AND_ACCEPT);

//生成传输客户端,并设置参数
client = new GridFTPClient(host , port);
client.authenticate(cred);
client.setProtectionBufferSize(16384);
client.setDataChannelAuthentication(DataChannelAuthentication.SELF);
client.setDataChannelProtection(GridFTPSession.PROTECTION_SAFE);
}
catch (Exception e) {
e.printStackTrace();
}
}

public void download(String remoteFile, String localFile, int transferType) {
try {
// 远程文件大小
long size = client.getSize(remoteFile);

// 检查远程服务器端文件是否存在,否则抛出异常
if ( client.exists(remoteFile) ) {
client.setType(transferType);

/** required to transfer multiple files **/
client.setLocalPassive();
client.setActive();

final FileOutputStream fos = new FileOutputStrea(localFile);

//使用get()方法实现下载,上传使用put()方法,第三方传输使用transfer()方法
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);

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

public void parallelDownload(String remoteFile, String localFile) {
try {

if ( client.exists(remoteFile) ) {
// transfer type must be IMAGE for parallel...
client.setType(GridFTPSession.TYPE_IMAGE);

/** extended mode is required by paralle transfers **/
client.setMode(GridFTPSession.MODE_EBLOCK);

/** required to transfer multiple files **/
client.setLocalPassive();
client.setActive();

/** set parallelism **/
// Number of parallel stremas to be opened
client.setOptions(new RetrieveOptions(2));

// 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.
DataSink sink = new FileRandomIO(new RandomAccessFile(localFile,"rw"));
long size = client.getSize(remoteFile);
client.extendedGet(remoteFile, size, sink, 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();
}
}

public void close() {
try {
client.close();
cred.dispose();
}
catch (Exception ex) {}
}

/**
* testing GridFTP class
*/
public static void main(String[] args) {
// used to display log4j debug messages
Logger.getRoot().setLevel(Level.INFO);

// remote files
String rf1 = "/tmp/file1";
String rf2 = "/tmp/file1";
String host = "gridserver";

// local files
String lf1 = "/tmp/fromserver1";
String lf2 = "/tmp/fromserver2";
int TRANSFER_TYPE = GridFTPSession.TYPE_ASCII;

MyGridFTP myGridFTP1 = new MyGridFTP(host, MyGridFTP.GRIDFTP_PORT);

// regular transfer test
//myGridFTP1.download(rf1, lf1, TRANSFER_TYPE);
//myGridFTP1.download(rf2, lf2, TRANSFER_TYPE);

// parallel transfer test
//myGridFTP1.parallelDownload(rf1, lf1);
myGridFTP1.parallelDownload(rf2, lf2);

myGridFTP1.close();
}
}


待续吧。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: