使用GridFTP传输文件的简单JAVA代码
2010-11-12 21:59
555 查看
首先要正确安装Java COG kit,
我使用的cog-jglobus-1.8.0-bin文件,解压后,有安装说明
安装成功后,在编写代码的之前,引入相应的JAR包。
待续吧。。。
我使用的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(); } }
待续吧。。。
相关文章推荐
- Java-流的简单使用:读取文件、写入文件(面试题:删除注释代码)
- JAVA中使用FTPClient实现文件上传下载实例代码
- 用Java代码连接ftp并传输文件
- JAVA中使用FTPClient实现文件上传下载实例代码
- Java使用FTPClient连接文件服务器并做相应操作(代码)
- Java-流的简单使用:读取文件、写入文件(面试题:删除注释代码)
- Java代码使用FTPClient实现ftp文件上传下载和删除的功能
- Java实现拖拽文件上传dropzone.js的简单使用示例代码
- JAVA中使用FTPClient实现文件上传下载实例代码
- java FTP 上传文件的简单例子
- Java压缩文件工具类ZipUtil使用方法代码示例
- xshell使用xftp传输文件,使用pure-ftpd搭建ftp服务
- 一个简单的使用wininet的http/ftp文件下载程序
- JSP基本功 pageContext对象page,request,session,application四个域对象的使用及区别JSP运行原理和九大隐式对象及下载文件、访问次数的代码总结出el表达式简单
- Java使用FTP下载文件报ConnectException
- Linux下使用socket传输文件的C语言简单实现
- Java代码实现FTP文件下载
- 一个简单的Linux内核代码整合到一个文件的Java程序
- Java FTP上传下载单个文件示例代码
- php使用ftp远程上传文件类(解决主从文件同步问题的简单方法)