亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 1432 | 回復: 0
打印 上一主題 下一主題

Java 版的ftp命令行工具,支持文件上傳,下載 [復制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2015-07-13 10:56 |只看該作者 |倒序瀏覽
編寫目的

自己寫了個腳本用來打包發(fā)布war但是由于上傳的使用使用的ftp(難道比較原始??),可是每次都要輸入用戶名,密碼。感覺很煩,想了想就是就自己簡單的實現(xiàn)了一個ftp的命令行工具。
可以做撒
與ftp控制臺工具類似

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

代碼中調(diào)用

導入 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. 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.             // 分配緩沖區(qū)大小內(nèi)存
  42.             while (true) {
  43.                 // 選擇一組鍵,其相應的通道已為 I/O 操作準備就緒。
  44.                 // 此方法執(zhí)行處于阻塞模式的選擇操作。
  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 狀態(tài)碼對應的方法
  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 (66.62 KB, 下載次數(shù): 45)

1.png
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復

  

北京盛拓優(yōu)訊信息技術有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP