FTP 协议(rfc939) 简单的实验(in Java)

Java代码:
FTPClient.java
package  org;

import  java.io.BufferedInputStream;
import  java.io.ByteArrayOutputStream;
import  java.io.IOException;
import  java.io.InputStream;
import  java.io.InputStreamReader;
import  java.io.OutputStreamWriter;
import  java.io.Reader;
import  java.io.Writer;
import  java.net.Socket;
import  java.util.ArrayList;
import  java.util.List;
import  java.util.StringTokenizer;

public   class  FtpClient  {

    
public static final int DEFAULT_PORT = 1968;

    
private static int print(Reader in) throws IOException {
        
int c;
        
int prev = 0, current = 0;
        
int result = 0;
        
int count = 0, k1 = 0, k2 = 0, k3 = 0;
        
while (prev != ' ' && current != ' '{
            prev 
= current;
            c 
= in.read();
            current 
= c;
            
if (count < 3{
                
switch (count) {
                
case 0:
                    k1 
= current - '0';
                    
break;
                
case 1:
                    k2 
= current - '0';
                    
break;
                
case 2:
                    k3 
= current - '0';
                    
break;
                }

            }

            count
++;
            System.out.write(current);
        }

        result 
= k1 * 100 + k2 * 10 + k3;
        
return result;
    }


    
private static void printData(Reader in) throws IOException {
        
int c = 0;

        
while ((c = in.read()) != -1{
            System.out.print((
char) c);
        }

        System.out.println();
    }


    
private static void inputCommand(Writer out, String command) throws IOException {
        out.write(command);
        out.write(
" /r/n");
        out.flush();
    }


    
public static void main(String[] args) {
        String hostname 
= "192.168.1.101";

        Socket commandConn 
= null;

        Socket dataConn 
= null;

        
try {

            commandConn 
= new Socket(hostname, DEFAULT_PORT);

            Writer out 
= new OutputStreamWriter(commandConn.getOutputStream(),
                    
"8859_1");

            InputStream raw 
= commandConn.getInputStream();
            BufferedInputStream buffer 
= new BufferedInputStream(raw);
            InputStreamReader commandIn 
= new InputStreamReader(buffer,
                    
"8859_1");

            System.out.println(
"login in");
            
int i = print(commandIn);// 220

            inputCommand(out, 
"USER winters");
            System.out.println(
"USER winters");
            i 
= print(commandIn);// 331

            inputCommand(out, 
"PASS 123456");
            System.out.println(
"PASS 123456");
            i 
= print(commandIn);// 230

            inputCommand(out, 
"SYST");
            System.out.println(
"SYST");
            i 
= print(commandIn);// 215

            inputCommand(out, 
"PWD");
            System.out.println(
"PWD");
            i 
= print(commandIn);// 257

            inputCommand(out, 
"TYPE A");
            System.out.println(
"TYPE A");
            i 
= print(commandIn);// 200

            inputCommand(out, 
"PASV");
            System.out.println(
"PASV");
            
            
int port = getPortNumber(commandIn);
            dataConn 
= new Socket(hostname, port);
            BufferedInputStream dataBuffer 
= new BufferedInputStream(dataConn
                    .getInputStream());
            InputStreamReader dataIn 
= new InputStreamReader(dataBuffer,
                    
"8859_1");

            System.out.println(
"Connected " + hostname + " on " + port);

            
/**
             * LIST
             
*/


            inputCommand(out, 
"LIST");
            System.out.println(
"LIST");
            System.out.print(
"commandIn : ");
            i 
= print(commandIn);// 150

            System.out.print(
"dataIn : ");
            printData(dataIn);
// abc

            i 
= print(commandIn);// 226

            
/** ************************************* */

            inputCommand(out, 
"PASV");
            System.out.println(
"PASV");
            port 
= getPortNumber(commandIn);

            dataConn 
= new Socket(hostname, port);
            dataBuffer 
= new BufferedInputStream(dataConn.getInputStream());
            dataIn 
= new InputStreamReader(dataBuffer, "8859_1");

            System.out.println(
"Connected " + hostname + " on " + port);

            
/**
             * RETR
             
*/


            inputCommand(out, 
"RETR 1.txt");
            System.out.println(
"RETR 1.txt");

            System.out.print(
"commandIn : ");
            i 
= print(commandIn);// 150

            System.out.print(
"dataIn : ");
            printData(dataIn);
// abc

            i 
= print(commandIn);// 226

        }
 catch (IOException e) {
            System.err.println(e);
        }
 finally {
            
try {
                
if (commandConn != null)
                    commandConn.close();
                
if (dataConn != null)
                    dataConn.close();
            }
 catch (IOException e) {
                System.err.println(e);
            }

        }


    }


    
private static int getPortNumber(InputStreamReader in) throws IOException {
        
int c;
        
int prev = 0, current = 0;
        
boolean start = false;
        ByteArrayOutputStream out 
= new ByteArrayOutputStream();
        
while (prev != ' ' && current != ' '{
            prev 
= current;
            c 
= in.read();
            current 
= c;

            
if (prev == '(')
                start 
= true;
            
if (current == ')')
                start 
= false;
            
if (start)
                out.write(current);
            
// System.out.println("==" + out.toString() + "==");
            System.out.write(current);
        }


        String ipWithPort 
= out.toString();
        StringTokenizer stk 
= new StringTokenizer(ipWithPort, ",");
        List list 
= new ArrayList();
        
while (stk.hasMoreTokens()) {
            list.add(stk.nextToken());
        }

        String[] str 
= (String[]) list.toArray(new String[list.size()]);
        
int a = Integer.parseInt(str[4]);
        
int b = Integer.parseInt(str[5]);
        
int port = a * 256 + b;
        
// System.out.println("debug: port == " + port);
        return port;
    }

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值