public class ftpServer extends Thread{
private Socket socketClient;
private int counter;
private static String initDir;
public static void main(String[] args){
if(args.length != 0) {
initDir = args[0];
}else{ initDir = "c:";}
int i = 1;
try{
System.out.println("ftp server started!");
//监听21号端口
ServerSocket s = new ServerSocket(21);
for(;;){
//接受客户端请求
Socket incoming = s.accept();
//创建服务线程
new ftpServer(incoming,i).start();
i++;
}
}catch(Exception e){}
}
if(str.startsWith("USER")){
user = str.substring(4);
user = user.trim();
out.println("331 Password");
}
if(str.startsWith("PASS"))
out.println("230 User "+user+" logged in.");
if(str.startsWith("CWD")){
String str1 = str.substring(3);
dir = dir+"/"+str1.trim();
out.println("250 CWD command succesful");
}
if(str.startsWith("CDUP")){
int n = dir.lastIndexOf("/");
dir = dir.substring(0,n);
out.println("250 CWD command succesful");
}
if(str.startsWith("QUIT")) {
out.println("GOOD BYE");
done = true;
}
if(str.startsWith("PORT")) {
out.println("200 PORT command successful");
int i = str.length() - 1;
int j = str.lastIndexOf(",");
int k = str.lastIndexOf(",",j-1);
String str1,str2;
str1="";
str2="";
for(int l=k+1;lstr1 = str2 + str.charAt(l);
}
for(int l=j+1;l<=i;l++){
str2 = str2 + str.charAt(l);
}
tempPort = Integer.parseInt(str1) * 16 *16 +Integer.parseInt(str2);
}
if(str.startsWith("TYPE")){
out.println("200 type set");
}
if(str.startsWith("RETR")){
out.println("150 Binary data connection");
str = str.substring(4);
str = str.trim();
RandomAccessFile outFile = new
RandomAccessFile(dir+"/"+str,"r");
Socket tempSocket = new Socket(host,tempPort);
OutputStream outSocket = tempSocket.getOutputStream();
byte byteBuffer[]= new byte[1024];
int amount;
try{
while((amount = outFile.read(byteBuffer)) != -1){
outSocket.write(byteBuffer, 0, amount);
}
outSocket.close();
out.println("226 transfer complete");
outFile.close();
tempSocket.close();
}
catch(IOException e){}
}
if(str.startsWith("STOR")){
out.println("150 Binary data connection");
str = str.substring(4);
str = str.trim();
RandomAccessFile inFile = new
RandomAccessFile(dir+"/"+str,"rw");
Socket tempSocket = new Socket(host,tempPort);
InputStream inSocket = tempSocket.getInputStream();
byte byteBuffer[] = new byte[1024];
int amount;
try{
while((amount =inSocket.read(byteBuffer) )!= -1){
inFile.write(byteBuffer, 0, amount);
}
inSocket.close();
out.println("226 transfer complete");
inFile.close();
tempSocket.close();
}
catch(IOException e){}
}
if(str.startsWith("DELE")){
str = str.substring(4);
str = str.trim();
File file = new File(dir,str);
boolean del = file.delete();
out.println("250 delete command successful");
}
if(str.startsWith("LIST")) {
try{
out.println("150 ASCII data");
Socket tempSocket = new Socket(host,tempPort);
PrintWriter out2= new PrintWriter(tempSocket.getOutputStream(),true);
File file = new File(dir);
String[] dirStructure = new String[10];
dirStructure= file.list();
String strType="";
for(int i=0;iif( dirStructure[i].indexOf(".") == -1) { strType = "d ";}
else
{strType = "- ";}
out2.println(strType+dirStructure[i]);
}
tempSocket.close();
out.println("226 transfer complete");
}
catch(IOException e){}
零七网部分新闻及文章转载自互联网,供读者交流和学习,若有涉及作者版权等问题请及时与我们联系,以便更正、删除或按规定办理。感谢所有提供资讯的网站,欢迎各类媒体与零七网进行文章共享合作。