2007年1月12日金曜日

FTP协议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 件のコメント: