求用java写一个ftp服务器客户端程序。

软件名:MYFTP(分服务器端和客户端两部分)功能:实现类似FTP的功能(在FTP服务器与客户端之间传送文件)。描述:服务器端:1.指定一个目录作为FTP目录(之后所有的文件操作均在此目录下进行), 2.接收客户端的命令请求并提供相应的服务。客户端:完成与FTP用户的人机界面,以命令的方式向服务器请求服务,主要的命令有:  1.CONNECT IP地址 端口号 指定FTP服务器的IP地址和端口号,连接到服务器。 响应:连接成功/失败。2.LOGIN 用户名用户登录(假设在服务器端有用户管理功能)。响应:若用户不存在,提示“无此用户”;若用户存在,提示“密码:”;用户输入密码后,若正确提示“登录成功”;若不成功,提示“密码错”。3.DIR显示FTP服务器的FTP目录的文件列表响应:目录列表(只要文件名即可)。4. GET 文件名将FTP目录下的文件下载到本地。响应:传送成功/失败/无此文件。5. PUT 文件名将本地文件上传到FTP目录。响应:传送成功/失败/无此文件。6. HELP显示MYFTP软件的功能(此命令与网络通信无关)。7. BYE退出MYFTP软件。

import java.io.*;
import java.net.*;public class ftpServer extends Thread{ public static void main(String args[]){
String initDir;
initDir = "D:/Ftp";
ServerSocket server;
Socket socket;
String s;
String user;
String password;
user = "root";
password = "123456";
try{
System.out.println("MYFTP服务器启动....");
System.out.println("正在等待连接....");
//监听21号端口
server = new ServerSocket(21);
socket = server.accept();
System.out.println("连接成功");
System.out.println("**********************************");
System.out.println("");

InputStream in =socket.getInputStream();
OutputStream out = socket.getOutputStream();

DataInputStream din = new DataInputStream(in);
DataOutputStream dout=new DataOutputStream(out);
System.out.println("请等待验证客户信息....");

while(true){
s = din.readUTF();
if(s.trim().equals("LOGIN "+user)){
s = "请输入密码:";
dout.writeUTF(s);
s = din.readUTF();
if(s.trim().equals(password)){
s = "连接成功。";
dout.writeUTF(s);
break;
}
else{s ="密码错误,请重新输入用户名:";<br> dout.writeUTF(s);<br> <br> }
}
else{
s = "您输入的命令不正确或此用户不存在,请重新输入:";
dout.writeUTF(s);
}
}
System.out.println("验证客户信息完毕...."); while(true){
System.out.println("");
System.out.println("");
s = din.readUTF();
if(s.trim().equals("DIR")){
String output = "";
File file = new File(initDir);
String[] dirStructure = new String[10];
dirStructure= file.list();
for(int i=0;i<dirStructure.length;i++){
output +=dirStructure[i]+"\n";
}
s=output;
dout.writeUTF(s);
}
else if(s.startsWith("GET")){
s = s.substring(3);
s = s.trim();
File file = new File(initDir);
String[] dirStructure = new String[10];
dirStructure= file.list();
String e= s;
int i=0;
s ="不存在";
while(true){
if(e.equals(dirStructure[i])){
s="存在";
dout.writeUTF(s);
RandomAccessFile outFile = new RandomAccessFile(initDir+"/"+e,"r");
byte byteBuffer[]= new byte[1024];
int amount;
while((amount = outFile.read(byteBuffer)) != -1){
dout.write(byteBuffer, 0, amount);break;
}break;

}
else if(i<dirStructure.length-1){
i++;
}
else{
dout.writeUTF(s);
break;
}
}
}
else if(s.startsWith("PUT")){
s = s.substring(3);
s = s.trim();
RandomAccessFile inFile = new RandomAccessFile(initDir+"/"+s,"rw");
byte byteBuffer[] = new byte[1024];
int amount;
while((amount =din.read(byteBuffer) )!= -1){
inFile.write(byteBuffer, 0, amount);break;
}
}
else if(s.trim().equals("BYE"))break;
else{
s = "您输入的命令不正确或此用户不存在,请重新输入:";
dout.writeUTF(s);
}
}

din.close();
dout.close();
in.close();
out.close();
socket.close();
}
catch(Exception e){
System.out.println("MYFTP关闭!"+e);

}
}}
温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答