QCHuangChao 发表于 2015-07-13 10:56

Java 版的ftp命令行工具,支持文件上传,下载

编写目的

自己写了个脚本用来打包发布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 这些基础命令。欢迎完整。。

代码package com.fzb.ftp;

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set;

public class FtpNIOClient {
    private SocketChannel client;
    private SelectionKey selectionKey;
    private Selector selector;
    private FtpSession ftpSession;
    private FtpLoginSuccCallBack callBack;
    private FtpUser ftpUser;
   
    public FtpNIOClient(FtpLoginSuccCallBack callBack, FtpUser ftpUser) {
      super();
      this.callBack = callBack;
      this.ftpUser = ftpUser;
    }

    public FtpNIOClient() {
      super();
    }


    public void connectServer() {
      InetSocketAddress SERVER_ADDRESS = new InetSocketAddress(
                ftpUser.getAddr(), ftpUser.getPort());
      try {
            // 打开socket通道
            SocketChannel socketChannel = SocketChannel.open();
            // 设置为非阻塞方式
            socketChannel.configureBlocking(false);
            // 打开选择器
            selector = Selector.open();
            Iterator iterator;
            Set<SelectionKey> selectionKeys;
            // 注册连接服务端socket动作
            socketChannel.register(selector, SelectionKey.OP_CONNECT);
            // 连接
            socketChannel.connect(SERVER_ADDRESS);
            // 分配缓冲区大小内存
            while (true) {
                // 选择一组键,其相应的通道已为 I/O 操作准备就绪。
                // 此方法执行处于阻塞模式的选择操作。
                selector.select();
                // 返回此选择器的已选择键集。
                selectionKeys = selector.selectedKeys();
                iterator = selectionKeys.iterator();
                while (iterator.hasNext()) {
                  selectionKey = (SelectionKey) iterator.next();
                  client = (SocketChannel) selectionKey.channel();
                  if (selectionKey.isConnectable()) {
                        if (client.isConnectionPending()) {
                            client.finishConnect();
                            ftpSession=new FtpSession(ftpUser, this);
                            if(callBack!=null){
                              ftpSession.getAttr().put("callBack",callBack);
                            }
                            System.out.println("connect finished");
                        }
                        client.register(selector, SelectionKey.OP_READ);
                  } else if (selectionKey.isReadable()) {
                        ByteBuffer input = ByteBuffer.allocate(1024 * 100);
                        client.read(input);
                        input.flip();
                        String strArr[] = new String(input.array()).trim()
                              .split("\r\n");
                        for (String string : strArr) {
                            System.out.println("resp --< " + string);
                        }
                        for (String string : strArr) {
                            ftpSession.readMsg(string);
                        }
                  } else if(selectionKey.isWritable()){
                        ftpSession.sendMsg(selectionKey.attachment().toString());
                  }
                }
                selectionKeys.clear();
            }
      } catch (Exception e) {
            e.printStackTrace();
      }
    }

    public SocketChannel getClient() {
      return client;
    }

    public SelectionKey getSelectionKey() {
      return selectionKey;
    }

    public Selector getSelector() {
      return selector;
    }
   
    public FtpSession getFtpSession(){
      return ftpSession;
    }
   

}ftp 状态码对应的方法package com.fzb.ftp;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class StatusMap {

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

    static {
      reloadMap();
    }

    private static void reloadMap() {
      try {
            map.put(220, FtpResponseImpl.class.getMethod("connectSucc",
                  String.class));
            map.put(331, FtpResponseImpl.class.getMethod("inputPassword",
                  String.class));
            map.put(230,
                  FtpResponseImpl.class.getMethod("loginSucc", String.class));
            map.put(200, FtpResponseImpl.class.getMethod("initCmdSucc",
                  String.class));
            map.put(215, FtpResponseImpl.class.getMethod("modelChanageSucc",
                  String.class));
            map.put(227, FtpResponseImpl.class.getMethod("passiveModeSucc",
                  String.class));
            map.put(150,
                  FtpResponseImpl.class.getMethod("tipsMsg", String.class));
            map.put(226, FtpResponseImpl.class.getMethod("modelChanageSucc",
                  String.class));
            map.put(250, FtpResponseImpl.class.getMethod("initCmdSucc",
                  String.class));
            map.put(550,
                  FtpResponseImpl.class.getMethod("tipsMsg", String.class));
            map.put(500,
                  FtpResponseImpl.class.getMethod("tipsMsg", String.class));
            map.put(425,
                  FtpResponseImpl.class.getMethod("tipsMsg", String.class));
            map.put(530,
                  FtpResponseImpl.class.getMethod("loginFailed", String.class));

      } catch (NoSuchMethodException e) {
            e.printStackTrace();
      } catch (SecurityException e) {
            e.printStackTrace();
      }
    }

    public static Method getMethod(Integer statusCode) {
      // just for dev
      reloadMap();
      return map.get(statusCode);
    }
}
页: [1]
查看完整版本: Java 版的ftp命令行工具,支持文件上传,下载