Unplugged

SAP@IBM - SD,MM,PP,FI,CO,HR,BI,PS,Portal,ABAP,JAVA

原创 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;
    }

}


发表于 @ 2007年01月01日 10:38:00|收藏

新一篇: 李连杰彻底“毁掉”了自己的形象的一幕 | 旧一篇: Java版本和C++版本的简单Stack程序

用户操作
[即时聊天] [发私信] [加为好友]
JavaPro
订阅我的博客
XML聚合  FeedSky
JavaPro的公告

Welcome to the stage of the history!

文章分类
收藏
CampFire Stories
Cup Size
Pass-by-Value Please
Cool Bloggers
Andre Oosthuizen
David Orme
Euxx
James Holmes
Kimberly Horne
Martin Fowler
Martin Perez
Friends
尽享亚洲美味
Ftp
seu
sjtu
Learning resources
Eclipse
Eclipse Planet
Eclipse Plugin Central
Eclipse Wiki
EclipseZone
J2EE1.4 Tutorial
Java Forum of Sun
JavaRanch
SWT and JFace
ObserveEclipse(En)
ObserveEclipse(Jp)
Swt Snippets
SWT Tips and Samples
Tips and Tricks
Adding History to Dialogs and Wizards
Backported Splash Screen From Eclipse 3.2
Branding the launcher icon
Define and load extension points
Eclipse 3.1 OSGI classloading
Eclipse Forms Programming Guide
EclipseCon 2005 Tutorials
HOWTO get started with JFace Data Binding
Linking Views
Package Visibility
Presentations API
Showing OpenOffice documents in SWT view
Showing OpenOffice documents in SWT view
Themes HowTo
Writing JUnit Test Plugins
Unplugged Site
Ant
Commons
JUnit.org
SourceForge.net
Struts
Tomcat
Useful Links
Agile Manifesto
oreilly
Rational Rose Tutorials
存档
Csdn Blog version 3.1a
Copyright © JavaPro