📄 ftpserver.java
字号:
import java.io.*;import java.net.*;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){} } //构造方法 public ftpServer(Socket income, int c){ socketClient = income; counter = c; } public void run(){ int tempPort=1; int portClient; String user="",host,dir; System.out.println(initDir); dir = initDir; InetAddress clientAddress; InetAddress localAddress; try{ //得到客户端地址 clientAddress =socketClient.getInetAddress(); //本地地址 localAddress=socketClient.getLocalAddress() ; //得到客户端端口 portClient=socketClient.getPort(); host =clientAddress.toString(); host = host.substring(host.indexOf("/") + 1); //输出客户端信息 System.out.println("client no "+counter+" is "+host+":"+portClient); //创建输入流 BufferedReader in = new BufferedReader(new InputStreamReader(socketClient.getInputStream())); //创建输出流 PrintWriter out = new PrintWriter(socketClient.getOutputStream(),true); //发送欢迎信息 out.println("Welcome! 220 FTP server[JAVA FTP server]ready.\r"); boolean done = false; while(!done){ String str = in.readLine(); 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("TYPE")){ out.println("200 type set");} 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("CDUP")){ int n = dir.lastIndexOf("/"); dir = dir.substring(0,n); out.println("250 CWD command succesful"); } if(str.startsWith("CWD")){ String str1 = str.substring(3); dir = dir+"/"+str1.trim(); out.println("250 CWD command succesful"); } if(str.startsWith("QUIT")) { out.println("GOOD BYE"); done = true; } 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("XPWD")){ out.println("257 \""+dir+"\" is current directory"); } if(str.startsWith("SYS")) out.println("500 SYS not understood"); 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;l<j;l++){ str1 = 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("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;i<dirStructure.length;i++){ if( dirStructure[i].indexOf(".") == -1) // DIR {strType = "d ";} else {strType = "- ";} out2.println(strType+dirStructure[i]); } tempSocket.close(); out.println("226 transfer complete"); }catch(IOException e){ } } } socketClient.close(); } catch (Exception e){ System.out.println(e); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -