如何用Java实现FTP服务器

如何用Java实现FTP服务器,第1张

FTP(File Transfer Protocol 文件传输协议)是Internet 上用来传送文件的协议。在Internet上通过FTP 服务器可以进行文件的上传(Upload)或下载(Download)。FTP是实时联机服务,在使用它之前必须是具有该服务的一个用户(用户名和口令),工作时客户端必须先登录到作为服务器一方的计算机上,用户登录后可以进行文件搜索和文件传送等有关操作,如改变当前工作目录、列文件目录、设置传输参数及传送文件等。使用FTP可以传送所有类型的文件,如文本文件、二进制可执行文件、图象文件、声音文件和数据压缩文件等。

FTP 命令

FTP 的主要操作都是基于各种命令基础之上的。常用的命令有:

设置传输模式,它包括ASCⅡ(文本) 和BINARY 二进制模式

目录操作,改变或显示远程计算机的当前目录(cd、dir/ls 命令)

连接操作,open命令用于建立同远程计算机的连接;close命令用于关闭连接

发送操作,put命令用于传送文件到远程计算机;mput 命令用于传送多个文件到远程计算机

获取操作,get命令用于接收一个文件;mget命令用于接收多个文件。

?

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635

import java.net.Socketimport org.apache.log4j.Logger/** * 角色——服务器A * @author Leon * */public class ServerA{ public static void main(String[] args){final String F_DIR = "c:/test"//根路径final int PORT = 22//监听端口号Logger.getRootLogger() Logger logger = Logger.getLogger("com") try{ ServerSocket s = new ServerSocket(PORT) logger.info("Connecting to server A...") logger.info("Connected Successful! Local Port:"+s.getLocalPort()+". Default Directory:'"+F_DIR+"'.") while( true ){//接受客户端请求Socket client = s.accept() //创建服务线程new ClientThread(client, F_DIR).start() }} catch(Exception e) { logger.error(e.getMessage()) for(StackTraceElement ste : e.getStackTrace()){logger.error(ste.toString()) }} }}import java.io.BufferedReaderimport java.io.Fileimport java.io.FileNotFoundExceptionimport java.io.IOExceptionimport java.io.InputStreamimport java.io.InputStreamReaderimport java.io.OutputStreamimport java.io.PrintWriterimport java.io.RandomAccessFileimport java.net.ConnectExceptionimport java.net.InetAddressimport java.net.ServerSocketimport java.net.Socketimport java.net.UnknownHostExceptionimport java.nio.charset.Charsetimport java.util.Randomimport org.apache.log4j.Logger/** * 客户端子线程类 * @author Leon * */public class ClientThread extends Thread { private Socket socketClient//客户端socket private Logger logger//日志对象 private String dir//绝对路径 private String pdir = "/"//相对路径 private final static Random generator = new Random()//随机数 public ClientThread(Socket client, String F_DIR){this.socketClient = client this.dir = F_DIR } @Override public void run() {Logger.getRootLogger() logger = Logger.getLogger("com") InputStream is = null OutputStream os = null try { is = socketClient.getInputStream() os = socketClient.getOutputStream() } catch (IOException e) { logger.error(e.getMessage()) for(StackTraceElement ste : e.getStackTrace()){logger.error(ste.toString()) }}BufferedReader br = new BufferedReader(new InputStreamReader(is,Charset.forName("UTF-8"))) PrintWriter pw = new PrintWriter(os) String clientIp = socketClient.getInetAddress().toString().substring(1)//记录客户端IPString username = "not logged in"//用户名String password = ""//口令String command = ""//命令boolean loginStuts = false//登录状态final String LOGIN_WARNING = "530 Please log in with USER and PASS first." String str = ""//命令内容字符串int port_high = 0 int port_low = 0 String retr_ip = ""//接收文件的IP地址Socket tempsocket = null //打印欢迎信息pw.println("220-FTP Server A version 1.0 written by Leon Guo") pw.flush() logger.info("("+username+") ("+clientIp+")>Connected, sending welcome message...") logger.info("("+username+") ("+clientIp+")>220-FTP Server A version 1.0 written by Leon Guo") boolean b = true while ( b ){ try {//获取用户输入的命令command = br.readLine() if(null == command) break } catch (IOException e) {pw.println("331 Failed to get command") pw.flush() logger.info("("+username+") ("+clientIp+")>331 Failed to get command") logger.error(e.getMessage()) for(StackTraceElement ste : e.getStackTrace()){ logger.error(ste.toString()) }b = false } /* * 访问控制命令 */ // USER命令 if(command.toUpperCase().startsWith("USER")){logger.info("(not logged in) ("+clientIp+")>"+command) username = command.substring(4).trim()if("".equals(username)){ pw.println("501 Syntax error") pw.flush() logger.info("(not logged in) ("+clientIp+")>501 Syntax error") username = "not logged in" }else{ pw.println("331 Password required for " + username) pw.flush() logger.info("(not logged in) ("+clientIp+")>331 Password required for " + username) }loginStuts = false } //end USER // PASS命令 else if(command.toUpperCase().startsWith("PASS")){logger.info("(not logged in) ("+clientIp+")>"+command) password = command.substring(4).trim()if(username.equals("root") &&password.equals("root")){ pw.println("230 Logged on") pw.flush() logger.info("("+username+") ("+clientIp+")>230 Logged on")// logger.info("客户端 "+clientIp+" 通过 "+username+"用户登录") loginStuts = true }else{ pw.println("530 Login or password incorrect!") pw.flush() logger.info("(not logged in) ("+clientIp+")>530 Login or password incorrect!") username = "not logged in" } } //end PASS // PWD命令 else if(command.toUpperCase().startsWith("PWD")){logger.info("("+username+") ("+clientIp+")>"+command) if(loginStuts){// logger.info("用户"+clientIp+":"+username+"执行PWD命令") pw.println("257 /""+pdir+"/" is current directory") pw.flush() logger.info("("+username+") ("+clientIp+")>257 /""+pdir+"/" is current directory") }else{ pw.println(LOGIN_WARNING) pw.flush() logger.info("("+username+") ("+clientIp+")>"+LOGIN_WARNING) } } //end PWD // CWD命令 else if(command.toUpperCase().startsWith("CWD")){logger.info("("+username+") ("+clientIp+")>"+command) if(loginStuts){ str = command.substring(3).trim() if("".equals(str)){pw.println("250 Broken client detected, missing argument to CWD. /""+pdir+"/" is current directory.") pw.flush() logger.info("("+username+") ("+clientIp+")>250 Broken client detected, missing argument to CWD. /""+pdir+"/" is current directory.") } else{//判断目录是否存在String tmpDir = dir + "/" + str File file = new File(tmpDir) if(file.exists()){//目录存在 dir = dir + "/" + str if("/".equals(pdir)){pdir = pdir + str } else{pdir = pdir + "/" + str }// logger.info("用户"+clientIp+":"+username+"执行CWD命令") pw.println("250 CWD successful. /""+pdir+"/" is current directory") pw.flush() logger.info("("+username+") ("+clientIp+")>250 CWD successful. /""+pdir+"/" is current directory") }else{//目录不存在 pw.println("550 CWD failed. /""+pdir+"/": directory not found.") pw.flush() logger.info("("+username+") ("+clientIp+")>550 CWD failed. /""+pdir+"/": directory not found.")

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=0i<dirStructure.lengthi++){

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)

}

}}


欢迎分享,转载请注明来源:夏雨云

原文地址:https://www.xiayuyun.com/zonghe/314157.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-29
下一篇2023-04-29

发表评论

登录后才能评论

评论列表(0条)

    保存