用户操作
[即时聊天] [发私信] [加为好友]
神の熊猫ID:niubtangno1
5480次访问,排名17668(1),好友0人,关注者0人。
niubtangno1的文章
原创 19 篇
翻译 0 篇
转载 3 篇
评论 0 篇
最近评论
文章分类
    收藏
      相册
      存档
      软件项目交易
      订阅我的博客
      XML聚合  FeedSky
      订阅到鲜果
      订阅到Google
      订阅到抓虾
      订阅到BlogLines
      订阅到Yahoo
      订阅到GouGou
      订阅到飞鸽
      订阅到Rojo
      订阅到newsgator
      订阅到netvibes

      原创 ftpclient的实例收藏

      新一篇: pulgin的设置 | 旧一篇: ftpserver的实例(不是很好请多关照)

      package FTPClient;

      import java.io.BufferedReader;

      import java.io.File;
      import java.io.FileInputStream;
      import java.io.FileNotFoundException;
      import java.io.FileOutputStream;

      import java.io.IOException;
      import java.io.InputStreamReader;
      import java.io.ObjectInputStream;
      import java.io.ObjectOutputStream;

      import java.net.Socket;
      import java.net.UnknownHostException;
      import java.util.ArrayList;

      import util.CommandParser;
      import util.Message;

      public class FTPClientWorker implements Worker {
       private static Socket clientSocket = null;

       private static boolean iFlag = true;

       private boolean login_flag = false;

       private String command;

       private String perPath;

       private String currentpath;

       private static Message smsg = null;

       private String id = null;

       CommandParser myComParse;

       Message tempMsg;

       ObjectOutputStream clientobjOut;

       ObjectInputStream clientobjIn;

       ArrayList<String> temp = new ArrayList<String>();

       /** *********************构造函数**************************** */
       public FTPClientWorker(String ip) throws IOException {
        this(ip, "21");
       }

       public FTPClientWorker(String ip, String port) throws IOException {
        try {
         clientSocket = new Socket(ip, Integer.parseInt(port));
        } catch (NumberFormatException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
        } catch (UnknownHostException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
        } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
        }

       }

       /** *****************************方法******************************* */

       public void login() {
        while (iFlag) {
         String name = null;
         System.out.println("请输入您的用户名:");
         BufferedReader buffname = new BufferedReader(new InputStreamReader(
           System.in));
         try {
          name = buffname.readLine();
         } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
         }
         // System.out.println(buffname.readLine());
         String pass = null;
         System.out.println("请输入您的密码:");
         BufferedReader buffpass = new BufferedReader(new InputStreamReader(
           System.in));
         try {
          pass = buffpass.readLine();
         } catch (IOException e) {

          e.printStackTrace();
         }
         // System.out.println(buffpass.readLine());
         CommandParser myComParse = new CommandParser();

         Message tempMsg = myComParse.parse("login " + name + " " + pass);
         send(tempMsg);
         receive();
         ArrayList array = smsg.getParas();
         if (array.get(0).equals("ok")) {
          System.out.println("服务器登录成功");
          id = (String) array.get(1);
          System.out.println("您的sessionId:" + id);
          currentpath = perPath = name + "FTP";
          break;
         } else if (array.get(0).equals("fail")) {
          System.out.println("服务器登录失败");
         } else {
          System.out.println("连接超时,系统错误~!");
         }

        }
       }

       private void logout() {
        Message tempMsgout = myComParse.parse(command + " " + id);
        send(tempMsgout);
        receive();
        if (smsg.getParas().get(0).equals("ok")) {
         System.out.println("登出成功!欢迎您再次使用!");
         login_flag = true;
        } else {
         System.out.println("登出失败!系统错误");
        }

        try {
         clientSocket.close();
        } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
        }
       }

       private void clientls() {

        Message tempMsgls = myComParse.parse(command + " " + id);

        send(tempMsgls);
        receive();
        System.out.println("您当前的文件夹:" + currentpath);
        System.out.println("浏览文件夹内容总长度:" + smsg.getParas().size());
        System.out.println("浏览文件夹内容:");
        for (int i = 0; i < smsg.getParas().size(); i++) {
         System.out.println(smsg.getParas().get(i));
        }
       }

       private void clientcd() {
        Message tempMsgcd = myComParse.parse(command + " " + id);
        send(tempMsgcd);

        if (tempMsg.getParas().get(0).equals("/")
          || tempMsg.getParas().get(0).equals("\\")) {
         receive();
         ArrayList arraycd = smsg.getParas();
         if (arraycd.get(0).equals("ok")) {
          currentpath = perPath;
          System.out.println("您现在的当前目录为:" + perPath);

         } else if (arraycd.get(0).equals("fail")) {
          System.out.println("根目录出错");
         } else {
          System.out.println("连接超时,系统错误~!");
         }
        } else if (tempMsg.getParas().get(0).equals("..")) {
         receive();
         ArrayList arraycd1 = smsg.getParas();
         if (arraycd1.get(0).equals("ok")) {
          System.out.println("您已成功返回上级目录");
         } else if (arraycd1.get(0).equals("fail")) {
          System.out.println("您已经在根目录或目录不存在");
         } else {
          System.out.println("系统错误~!");
         }

        } else {
         receive();
         if (smsg.getParas().get(0).equals("ok")) {
          currentpath = currentpath + "/" + tempMsg.getParas().get(0);
          System.out.println("您现在的当前目录为:" + currentpath);

         } else if (smsg.getParas().get(0).equals("fail")) {
          System.out.println("目录出错或不存在指定目录");
         } else {
          System.out.println("连接超时,系统错误~!");
         }

        }
       }

       private void clientget() {
        Message tempMsgget = myComParse.parse(command + " " + id);
        send(tempMsgget);
        receive();
        try {
         FileOutputStream fileo = new FileOutputStream((String) smsg
           .getParas().get(1));

         byte[] fileb = smsg.getBytedata();
         if ((int) fileb.length == 0) {
          System.out.println("文件下载失败~!");
         } else {
          System.out.println("文件长度为:" + fileb.length + " 字节");
          fileo.write(fileb);
          System.out.println("文件:" + smsg.getParas().get(0) + "...下载成功!");
          System.out.println("本地文件名为:" + smsg.getParas().get(1));
          fileo.close();
         }
        } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
        }
       }

       private void clientput() {
        String fileS;
        String wholefile = temp.get(0);
        // String strp = command.substring(4, command.length());
        // System.out.println("wholefile: " + wholefile);
        int folength = wholefile.lastIndexOf("/") + 1;
        // System.out.println(folength);
        String fileC = wholefile.substring(folength, wholefile.length());// 得到文件名
        // System.out.println("fileC: " + fileC);
        if ((int) tempMsg.getParas().size() < 2) {
         fileS = fileC;
        } else {
         fileS = temp.get(1);
        }
        File filef = new File(wholefile);
        FileInputStream filei;
        try {
         filei = new FileInputStream(filef);
         byte[] fileb = new byte[(int) filef.length()];
         try {
          filei.read(fileb);
          filei.close();
          Message tempMsgput = myComParse.parse("put " + fileC + " "
            + fileS + " " + id);
          tempMsgput.setBytedata(fileb);
          send(tempMsgput);
         } catch (FileNotFoundException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
         }
        } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
        }
        receive();
        if (smsg.getParas().get(0).equals("ok")) {
         System.out.println("文件上传成功");
         System.out.println("文件长度:" + smsg.getParas().get(1));
        } else if (smsg.getParas().get(0).equals("fail")) {
         System.out.println("文件上传失败~!");
        } else {
         System.out.println("系统连接超时");
        }
       }

       private void mkdir() {
        Message tempMsgmkdir = myComParse.parse(command + " " + id);
        send(tempMsgmkdir);
        receive();
        smsg.getParas();
        if (smsg.getParas().get(0).equals("ok")) {
         System.out.println("文件夹创建成功~!");
        } else {
         System.out.println("文件夹创建失败或文件夹产生同名~!");
        }

       }

       // 发送对象
       public void send(Message tempMsg) {
        try {
         // try {
         clientobjOut = new ObjectOutputStream(clientSocket
           .getOutputStream());

         System.out.println("开始发送对象......");
         // System.out.print(tempMsg.getCommand() + tempMsg.getType()
         // + tempMsg.getParas().toString());
         clientobjOut.writeObject(tempMsg);
         clientobjOut.flush();
         System.out.println("发送完毕");
        } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
        }
       }

       // 接收对象
       public void receive() {

        try {
         System.out.println("开始接受对象");
         clientobjIn = new ObjectInputStream(clientSocket.getInputStream());
         smsg = null;
         smsg = (Message) clientobjIn.readObject();
         // System.out.println(smsg.getCommand());
         // System.out.println(smsg.getType());
         // System.out.println(smsg.getParas().toString());
        } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
        } catch (ClassNotFoundException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
        }
        System.out.println("接受完毕");

       }

       public void helpMenu() {
        System.out
          .println("命令菜单:"
            + "\nbye    :to exit the FTP environment (same as quit)"
            + "\nclose  :to terminate a connection with another computer"
            + "\nget    :get filename [localName] \n       localName为本地文件名,如果缺省,则采用同远程文件名filename一样的本地文件名"
            + "\nput    :to copy one file from the local machine to the remote machine"
            + "\nlogin  :login [username] [password] 如果未给出username或password,则提示用户输入"
            + "\nlogout :to logout(注销) from FTP server"
            + "\n?      :to request help or information about the FTP commands"
            + "\ncd     : cd [dir] dir为路径,. 表示当前,..表示上级目录"
            + "\nhelp   :same as the ?"
            + "\nls     :to list the names of the files in the current remote directory ");

       }

       /** ******************* 主进程********************************* */
       public Boolean process() {
        login();
        myComParse = new CommandParser();
        while (iFlag) {
         try {

          System.out.print("FTP>");
          BufferedReader buf = new BufferedReader(new InputStreamReader(
            System.in));
          command = buf.readLine();
          tempMsg = myComParse.parse(command);
          for (int i = 0; i < tempMsg.getParas().size(); i++) {
           temp.add((String) tempMsg.getParas().get(i));
           System.out.println((String) tempMsg.getParas().get(i));
          }
          tempMsg.getParas().clear();
          String maincommand = tempMsg.getCommand().toUpperCase();

          if (command.toUpperCase().equals("CD")
            || command.toUpperCase().equals("CD ")) {
           System.out.println("cd [dir]");
           System.out.println("dir为路径,. 表示当前,..表示上级目录");

          } else if (command.toUpperCase().equals("GET")) {
           System.out.println("get filename [localName]");
           System.out
             .println("localName为本地文件名,如果缺省,则采用同远程文件名filename一样的本地文件名");
           System.out
             .println("get ABC DEF copies file ABC in the current remote directory to (or on top of) a file named DEF in your current local directory");
           System.out
             .println("get ABC copies file ABC in the current remote directory to(or on  top of) a file with the same name, ABC, in your current local directory. ");
          } else if (command.toUpperCase().equals("?")) {
           helpMenu();
          } else if (command.toUpperCase().equals("DELETE")) {
           System.out.println("delete filename");
           System.out.println("删除文件");
          } else if (command.toUpperCase().equals("PUT")) {
           System.out.println("put filename [remoteName]");
           System.out
             .println("remoteName为远程文件名,如果缺省,则采用同远程文件名filename一样的本地文件名");
          } else if (command.toUpperCase().equals("LOGIN")) {
           System.out.println("login [username] [password]");
           System.out.println("用户登陆命令");
          } else if (command.toUpperCase().equals("MKDIR")) {
           System.out.println("mkdir [dirName]");
           System.out.println("dirName为目录名称");
          } else if (command.toUpperCase().equals("MGET")) {
           System.out.println("mget [wildcastFileName]");
           System.out.println("wildcastFileName为可以使用通配符的文件名");
          } else if (command.toUpperCase().equals("MPUT")) {
           System.out.println("mput [wildcastFileName]");
           System.out.println("wildcastFileName为可以使用通配符的文件名");
          } else if (command.toUpperCase().equals("RMDIR")) {
           System.out.println("rmdir [dirName]");
           System.out.println("dirName为目录名称");
          } else if (command.toUpperCase().equals("HELP")) {
           helpMenu();
          } else if (maincommand.equals("LS")
            || command.toUpperCase().equals("CD .")) {
           clientls();
          } else if (maincommand.equals("CD")) {
           clientcd();
          } else if (maincommand.equals("GET")) {
           clientget();
          } else if (maincommand.equals("PUT")) {
           clientput();
          } else if (maincommand.equals("LOGOUT")) {
           logout();
          } else if (maincommand.equals("MKDIR")) {
           mkdir();
          } else if (maincommand.equals("QUIT")
            || maincommand.equals("BYE")) {
           if (login_flag == true) {
            Message tempMsgexit = myComParse.parse(command + " "
              + id);
            send(tempMsgexit);
            try {
             clientobjIn.close();
            } catch (IOException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
            }
            try {
             clientobjOut.close();
             clientSocket.close();
            } catch (IOException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
            }

            System.out.println("退出程序");
            System.exit(0);
           } else {
            System.out.println("请先登出,例如:logout");
           }

          }
         } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
         }

        }

        return true;
       }
      }
       

      发表于 @ 2007年03月29日 20:30:00|编辑

      新一篇: pulgin的设置 | 旧一篇: ftpserver的实例(不是很好请多关照)

      评论:没有评论。

      Csdn Blog version 3.1a
      Copyright © 神の熊猫