当前位置: 首页 >> 程序设计 >> Java >> FTP协议Java实现
 

FTP协议Java实现

作者:      来源:http://blog.csdn.net/starshus     发表时间:2007-01-01     浏览次数:      字号:    

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

}

责任编辑 webmaster

 
 
 
 
 
评论更多>>
 
 
 
发表
 
姓名: QQ:
性别: MSN:
E-mail: 主页:
评分: 1 2 3 4 5
评论内容:
验证码:
  
  • 请遵守《互联网电子公告服务管理规定》及中华人民共和国其他各项有关法律法规。
  • 严禁发表危害国家安全、损害国家利益、破坏民族团结、破坏国家宗教政策、破坏社会稳定、侮辱、诽谤、教唆、淫秽等内容的评论 。
  • 用户需对自己在使用本站服务过程中的行为承担法律责任(直接或间接导致的)。
  • 本站管理员有权保留或删除评论内容。
  • 评论内容只代表网友个人观点,与本网站立场无关。
  •