现在关于JAVA语言中用的FTP的中间件很多,但是具体的地层怎么实现的呢?
本人这里提供一个例子:
import java.net.*;
import java.io.*;
public class Ftp
{
Socket ctrlSocket;
public PrintWriter ctrlOutput;
public BufferedReader ctrlInput;
PrintWriter out = new PrintWriter(System.out,true);
public void openConnection(String host) throws IOException,UnknownHostException{
try
{
ctrlSocket = new Socket(host,21);
ctrlOutput = new PrintWriter(ctrlSocket.getOutputStream());
ctrlInput = new BufferedReader(new InputStreamReader(ctrlSocket.getInputStream()));
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
public void closeConnection() throws IOException{
ctrlSocket.close();
}
public void showMenu(){
out.println("2 ls");
out.println("3 cd");
out.println("4 get");
out.println("5 put");
out.println("6 ascii");
out.println("7 binary");
out.println("9 quit");
}
public String getCommand(){
String buf = "";
BufferedReader lineread = new BufferedReader(new InputStreamReader(System.in));
while (buf.length()!=1){
try
{
buf = lineread.readLine();
}
catch (Exception ex){
ex.printStackTrace();
System.exit(1);
}
}
out.println("connect-begin" + buf);
return(buf);
}
public void doLogin(){
String loginName = "";
String password = "";
BufferedReader lineread = new BufferedReader(new InputStreamReader(System.in));
try
{
out.println("your name:");
ctrlOutput.flush();
loginName =lineread.readLine();
ctrlOutput.flush();
ctrlOutput.println("USER " + loginName);
ctrlOutput.flush();
out.println("your password");
ctrlOutput.flush();
password = lineread.readLine();
ctrlOutput.flush();
ctrlOutput.println("PASS " + password);
ctrlOutput.flush();
}
catch (Exception ex){
ex.printStackTrace();
System.exit(1);
}
}
public void doQuit(){
try
{
ctrlOutput.println("QUIT");
ctrlOutput.flush();
}
catch (Exception ex){
ex.printStackTrace();
System.exit(1);
}
}
public void doCd(){
String dirName = "";
BufferedReader lineread = new BufferedReader(new InputStreamReader(System.in));
try{
out.println("your directionary:");
dirName = lineread.readLine();
ctrlOutput.println("CWD " +dirName);
ctrlOutput.flush();
}
catch (Exception ex){
ex.printStackTrace();
System.exit(1);
}
}
public void doLs(){
try
{
int n;
byte[] buff = new byte[1024];
out.println("sock");
Socket dataSocket = dataConnection("LIST");
out.println("sock = " + dataSocket);
BufferedInputStream dataInput = new BufferedInputStream(dataSocket.getInputStream());
while ((n = dataInput.read(buff))> 0){
System.out.write(buff,0,n);
}
dataSocket.close();
}
catch (Exception ex){
ex.printStackTrace();
System.exit(1);
}
}
public Socket dataConnection(String ctrlcmd){
String cmd = "PORT ";
int i;
Socket dataSocket = null;
try
{
byte[] address =InetAddress.getLocalHost().getAddress();
ServerSocket serverDataSocket = new ServerSocket(8802,1);
for (i = 0;i < 4 ; i++) {
cmd = cmd + (address[i] &0xff) + ",";
}
cmd = cmd + (((serverDataSocket.getLocalPort())/256) & 0xff) + ","
+(serverDataSocket.getLocalPort() & 0xff);
out.println(cmd);
ctrlOutput.println(cmd);
ctrlOutput.flush();
ctrlOutput.println(ctrlcmd);
ctrlOutput.flush();
dataSocket = serverDataSocket.accept();
serverDataSocket.close();
}
catch (Exception ex){
ex.printStackTrace();
System.exit(1);
}
out.println("cmd =" + cmd +" sock = " +dataSocket);
return dataSocket;
}
public void doAscii(){
try
{
ctrlOutput.println("TYPE A");
ctrlOutput.flush();
}
catch (Exception ex){
ex.printStackTrace();
System.exit(1);
}
}
public void doBinary(){
try
{
ctrlOutput.println("TYPE I");
ctrlOutput.flush();
}
catch (Exception ex){
ex.printStackTrace();
System.exit(1);
}
}
public void doGet(){
String fileName = "";
BufferedReader lineread = new BufferedReader(new InputStreamReader(System.in));
try
{
int n;
byte[] buff = new byte[1024];
out.println("input your finename :");
fileName = lineread.readLine();
FileOutputStream outfile = new FileOutputStream(fileName);
Socket dataSocket = dataConnection("RETR " + fileName);
BufferedInputStream dataInput = new BufferedInputStream(dataSocket.getInputStream());
while ((n = dataInput.read(buff)) > 0){
outfile.write(buff,0,n);
}
dataSocket.close();
outfile.close();
}
catch (Exception ex){
ex.printStackTrace();
System.exit(1);
}
}
public void doPut(){
String fileName = "";
BufferedReader lineread = new BufferedReader(new InputStreamReader(System.in));
try
{
int n;
byte[] buff = new byte[1024];
FileInputStream sendfile = null;
out.println("input file name :");
fileName = lineread.readLine();
try
{
sendfile = new FileInputStream(fileName);
}
catch (Exception ex){
out.println("file not exist");
return ;
}
Socket dataSocket = dataConnection("STOR " +fileName);
OutputStream outstr = dataSocket.getOutputStream();
while ((n = sendfile.read(buff)) > 0){
outstr.write(buff,0,n);
}
dataSocket.close();
sendfile.close();
}
catch (Exception ex)
{
ex.printStackTrace();
System.exit(1);
}
}
public boolean execCommand(String command){
boolean cont = true;
switch (Integer.parseInt(command)){
case 2:
doLs();
break;
case 3:
doCd();
break;
case 4:
doGet();
break;
case 5 :
doPut();
break;
case 6:
doAscii();
break;
case 7:
doBinary();
break;
case 9:
doQuit();
cont = false;
break;
default:
out.println("input again");
}
return cont;
}
public void main_proc(){
boolean cont =true;
try
{
doLogin();
while(cont){
showMenu();
cont = execCommand(getCommand());
}
}
catch (Exception ex)
{
ex.printStackTrace();
System.exit(1);
}
}
public void getMsgs(){
try
{
Ctrllisten listener = new Ctrllisten(ctrlInput);
Thread listenerthread = new Thread(listener);
listenerthread.start();
}
catch (Exception ex)
{
ex.printStackTrace();
System.exit(1);
}
}
public static void main(String[] args)
{
System.out.println("Hello World!");
try
{
Ftp f = null;
if(args.length < 1)
{
System.out.println("usage:java ftp <hostname>");
return;
}
f = new Ftp();
//System.out.println("connect to " +args[0]);
f.openConnection(args[0]);
//System.out.println("connected");
f.getMsgs();
//System.out.println("main");
f.main_proc();
f.closeConnection();
System.exit(1);
}
catch (Exception ex)
{
ex.printStackTrace();
System.exit(1);
}
}
}//end of Ftp
class Ctrllisten implements Runnable{
BufferedReader ctrlInput = null;
public Ctrllisten(BufferedReader in){
ctrlInput = in;
}
public void run(){
while(true){
try{
System.out.println(ctrlInput.readLine());
}catch(Exception ex){
System.exit(1);
}
}
}
}