免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1372 | 回复: 0

Java 版的ftp命令行工具,支持文件上传,下载 [复制链接]

论坛徽章:
0
发表于 2015-07-13 10:56 |显示全部楼层
编写目的

自己写了个脚本用来打包发布war但是由于上传的使用使用的ftp(难道比较原始??),可是每次都要输入用户名,密码。感觉很烦,想了想就是就自己简单的实现了一个ftp的命令行工具。
可以做撒
与ftp控制台工具类似

java -jar simpleftpclient.jar --host 203.195.167.55 --userName test --password K~wqYDZpTg2

代码中调用

导入 simpleftpclient.jar

FtpUser user = new FtpUser();
user.setAddr(host);
user.setPassword(password);
user.setUserName(userName);
new FtpManager().sendCmd(user,cmd.replace("*"," "));


代码提交在git 上欢迎查看
http://git.oschina.net/94fzb/simpleftpclient
目前支持命令 dir put get cd exit 这些基础命令。欢迎完整。。
1.png
代码
  1. package com.fzb.ftp;

  2. import java.net.InetSocketAddress;
  3. import java.nio.ByteBuffer;
  4. import java.nio.channels.SelectionKey;
  5. import java.nio.channels.Selector;
  6. import java.nio.channels.SocketChannel;
  7. import java.util.Iterator;
  8. import java.util.Set;

  9. public class FtpNIOClient {
  10.     private SocketChannel client;
  11.     private SelectionKey selectionKey;
  12.     private Selector selector;
  13.     private FtpSession ftpSession;
  14.     private FtpLoginSuccCallBack callBack;
  15.     private FtpUser ftpUser;
  16.      
  17.     public FtpNIOClient(FtpLoginSuccCallBack callBack, FtpUser ftpUser) {
  18.         super();
  19.         this.callBack = callBack;
  20.         this.ftpUser = ftpUser;
  21.     }

  22.     public FtpNIOClient() {
  23.         super();
  24.     }


  25.     public void connectServer() {
  26.         InetSocketAddress SERVER_ADDRESS = new InetSocketAddress(
  27.                 ftpUser.getAddr(), ftpUser.getPort());
  28.         try {
  29.             // 打开socket通道
  30.             SocketChannel socketChannel = SocketChannel.open();
  31.             // 设置为非阻塞方式
  32.             socketChannel.configureBlocking(false);
  33.             // 打开选择器
  34.             selector = Selector.open();
  35.             Iterator iterator;
  36.             Set<SelectionKey> selectionKeys;
  37.             // 注册连接服务端socket动作
  38.             socketChannel.register(selector, SelectionKey.OP_CONNECT);
  39.             // 连接
  40.             socketChannel.connect(SERVER_ADDRESS);
  41.             // 分配缓冲区大小内存
  42.             while (true) {
  43.                 // 选择一组键,其相应的通道已为 I/O 操作准备就绪。
  44.                 // 此方法执行处于阻塞模式的选择操作。
  45.                 selector.select();
  46.                 // 返回此选择器的已选择键集。
  47.                 selectionKeys = selector.selectedKeys();
  48.                 iterator = selectionKeys.iterator();
  49.                 while (iterator.hasNext()) {
  50.                     selectionKey = (SelectionKey) iterator.next();
  51.                     client = (SocketChannel) selectionKey.channel();
  52.                     if (selectionKey.isConnectable()) {
  53.                         if (client.isConnectionPending()) {
  54.                             client.finishConnect();
  55.                             ftpSession=new FtpSession(ftpUser, this);
  56.                             if(callBack!=null){
  57.                                 ftpSession.getAttr().put("callBack",callBack);
  58.                             }
  59.                             System.out.println("connect finished");
  60.                         }
  61.                         client.register(selector, SelectionKey.OP_READ);
  62.                     } else if (selectionKey.isReadable()) {
  63.                         ByteBuffer input = ByteBuffer.allocate(1024 * 100);
  64.                         client.read(input);
  65.                         input.flip();
  66.                         String strArr[] = new String(input.array()).trim()
  67.                                 .split("\r\n");
  68.                         for (String string : strArr) {
  69.                             System.out.println("resp --< " + string);
  70.                         }
  71.                         for (String string : strArr) {
  72.                             ftpSession.readMsg(string);
  73.                         }
  74.                     } else if(selectionKey.isWritable()){
  75.                         ftpSession.sendMsg(selectionKey.attachment().toString());
  76.                     }
  77.                 }
  78.                 selectionKeys.clear();
  79.             }
  80.         } catch (Exception e) {
  81.             e.printStackTrace();
  82.         }
  83.     }

  84.     public SocketChannel getClient() {
  85.         return client;
  86.     }

  87.     public SelectionKey getSelectionKey() {
  88.         return selectionKey;
  89.     }

  90.     public Selector getSelector() {
  91.         return selector;
  92.     }
  93.      
  94.     public FtpSession getFtpSession(){
  95.         return ftpSession;
  96.     }
  97.      

  98. }
复制代码
ftp 状态码对应的方法
  1. package com.fzb.ftp;

  2. import java.lang.reflect.Method;
  3. import java.util.HashMap;
  4. import java.util.Map;

  5. public class StatusMap {

  6.     private static Map<Integer, Method> map = new HashMap<Integer, Method>();

  7.     static {
  8.         reloadMap();
  9.     }

  10.     private static void reloadMap() {
  11.         try {
  12.             map.put(220, FtpResponseImpl.class.getMethod("connectSucc",
  13.                     String.class));
  14.             map.put(331, FtpResponseImpl.class.getMethod("inputPassword",
  15.                     String.class));
  16.             map.put(230,
  17.                     FtpResponseImpl.class.getMethod("loginSucc", String.class));
  18.             map.put(200, FtpResponseImpl.class.getMethod("initCmdSucc",
  19.                     String.class));
  20.             map.put(215, FtpResponseImpl.class.getMethod("modelChanageSucc",
  21.                     String.class));
  22.             map.put(227, FtpResponseImpl.class.getMethod("passiveModeSucc",
  23.                     String.class));
  24.             map.put(150,
  25.                     FtpResponseImpl.class.getMethod("tipsMsg", String.class));
  26.             map.put(226, FtpResponseImpl.class.getMethod("modelChanageSucc",
  27.                     String.class));
  28.             map.put(250, FtpResponseImpl.class.getMethod("initCmdSucc",
  29.                     String.class));
  30.             map.put(550,
  31.                     FtpResponseImpl.class.getMethod("tipsMsg", String.class));
  32.             map.put(500,
  33.                     FtpResponseImpl.class.getMethod("tipsMsg", String.class));
  34.             map.put(425,
  35.                     FtpResponseImpl.class.getMethod("tipsMsg", String.class));
  36.             map.put(530,
  37.                     FtpResponseImpl.class.getMethod("loginFailed", String.class));

  38.         } catch (NoSuchMethodException e) {
  39.             e.printStackTrace();
  40.         } catch (SecurityException e) {
  41.             e.printStackTrace();
  42.         }
  43.     }

  44.     public static Method getMethod(Integer statusCode) {
  45.         // just for dev
  46.         reloadMap();
  47.         return map.get(statusCode);
  48.     }
  49. }
复制代码
1.png
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP