ftpsession.java
上传用户:lybcsm
上传日期:2007-04-26
资源大小:26k
文件大小:11k
- package ftpserver;
- import java.io.*;
- import java.util.*;
- import java.net.*;
- import java.text.*;
- import java.text.SimpleDateFormat;
- public class ftpsession implements Runnable {
- private ftpframe frame;
- private String linesep;
- private Socket cs;
- private Socket ds;
- boolean blnBinary;
- BufferedReader inData;
- PrintWriter outData;
- File rootPath=new File(".");
- File currentPath=rootPath;
- String loginname="";
- //Constructor
- public ftpsession(Socket cs,ftpframe frame){
- this.cs=cs;
- this.frame=frame;
- //Line Separator
- linesep=System.getProperty("line.separator");
- }
- public void showMessage(String Msg){
- outData.println(Msg);
- frame.textMessage.append(Msg+linesep);
- frame.textMessage.setCaretPosition(frame.textMessage.getText().length());
- }
- public void resetDefault(){
- blnBinary=false;
- currentPath=rootPath;
- }
- public void run(){
- try{
- inData=new BufferedReader(
- new InputStreamReader(cs.getInputStream()));
- outData=new PrintWriter(cs.getOutputStream(),true);
- showMessage("220 Java FTP Server-<version 1.0>-ready.");
- String ftpCmd="";
- //Process FTP client command
- while(!(ftpCmd.toLowerCase().startsWith("quit"))){
- try{
- ftpCmd=inData.readLine();
- ftpCommand(ftpCmd);
- }
- catch(IOException ex){
- ex.printStackTrace();
- ftpCmd="quit";
- }
- }
- //Close FTP Session
- try{
- cs.close();
- }
- catch(IOException e){
- e.printStackTrace();
- }
- cs=null;
- inData=null;
- outData=null;
- }
- catch(Exception e){
- e.printStackTrace();
- }
- }
- void ftpCommand(String cmd){
- if(cmd==null) cmd="";
- StringTokenizer ftpCmdtok=new StringTokenizer(cmd.toLowerCase());
- String ftpCmd="";
- try{
- ftpCmd=ftpCmdtok.nextToken();
- SimpleDateFormat formatter=new SimpleDateFormat("EEE,d MMM yyyy hh:mm:ss z");
- System.out.println(formatter.format(new Date())+""+cmd);
- //syst
- if(ftpCmd.equals("syst")){
- showMessage("215 Java FTP Server.");
- }
- //user:Login
- else if("user".indexOf(ftpCmd)!=-1){
- try{
- loginname=ftpCmdtok.nextToken();
- showMessage("331 Password required for"+loginname);
- }
- catch(Exception e){
- showMessage("500 User syntax.");
- }
- }
- //pass: Verify password
- else if("pass".indexOf(ftpCmd)!=-1){
- //Add the logic of verifying password here
- showMessage("230 User"+loginname+"logged in.");
- resetDefault();
- }
- //port
- else if(ftpCmd.equals("port")){
- try{
- StringTokenizer arg=new
- StringTokenizer(ftpCmdtok.nextToken(),",",false);
- String clientIP="";
- String ipString="";
- int cmdPort;
- if(ds!=null){
- ds.close();
- ds=null;
- }
- clientIP=arg.nextToken();
- ipString=clientIP;
- //get Client IP
- for(int i=0;i<3;i++){
- String hIP=arg.nextToken();
- clientIP=clientIP+"."+hIP;
- ipString=ipString+","+hIP;
- }
- String p1=arg.nextToken();
- ipString=ipString+","+p1;
- cmdPort=new Integer(p1).intValue();
- cmdPort *=256;
- String p2= arg.nextToken();
- ipString=ipString+","+p2;
- cmdPort+=new Integer(p2).intValue();
- //Establish data connection
- ds=new Socket(clientIP,cmdPort);
- showMessage("PORT"+ipString);
- showMessage("200 PORT command successful.");
- }
- catch(Exception e){
- showMessage("500 Port number syntax.");
- }
- }
- //type
- else if(ftpCmd.equals("type")){
- try{
- String arg=ftpCmdtok.nextToken();
- //Binary
- if("i".indexOf(arg)!=-1){
- blnBinary=true;
- showMessage("200 Type set to I.");
- }
- //ASCII
- else if("a".indexOf(arg)!=-1){
- blnBinary=false;
- showMessage("200 Type set to A.");
- }
- else showMessage("501 Type"+arg+"syntax.");
- }
- catch(Exception e){
- showMessage("500 TYPE syntax.");
- }
- }
- //mode
- else if(ftpCmd.equals("mode")){
- try{
- String arg=ftpCmdtok.nextToken();
- if(arg.equals("s"))
- showMessage("200 MODE S.");
- else showMessage("501 MODE S not found.");
- }
- catch(Exception e){
- showMessage("500 MODE syntax.");
- }
- }
- //stru
- else if(ftpCmd.equals("stru")){
- try{
- String arg=ftpCmdtok.nextToken();
- if(arg.equals("f"))
- showMessage("200 STRU F.");
- else showMessage("501 STRU F not found.");
- }
- catch(Exception e){
- showMessage("500 STRU syntax.");
- }
- }
- //list
- else if("list".indexOf(ftpCmd)!=-1){
- String arg;
- if(ftpCmdtok.hasMoreTokens())
- arg=ftpCmdtok.nextToken();
- else arg=".";
- ftpList(arg);
- }
- //nlst
- else if("nlst".indexOf(ftpCmd)!=-1){
- String arg;
- if(ftpCmdtok.hasMoreTokens())
- arg=ftpCmdtok.nextToken();
- else arg=".";
- ftpList(arg);
- }
- else if(ftpCmd.equals("cdup"))
- changeDirectory(currentPath.getParent());
- else if(ftpCmd.equals("cwd")){
- try{
- String arg=ftpCmdtok.nextToken();
- changeDirectory(arg);
- }
- catch(Exception e){
- showMessage("500 CWD syntax.");
- }
- }
- //xpwd:Current Directory(pwd)
- else if("xpwd".indexOf(ftpCmd)!=-1){
- showMessage("200 PWD"+currentPath);
- }
- //stor:PUT
- else if(ftpCmd.equals("stor")){
- try{
- String arg=ftpCmdtok.nextToken();
- File path=new File(currentPath,arg);
- if(!path.getCanonicalPath().startsWith(rootPath.getCanonicalPath()))
- throw new SecurityException();
- if(blnBinary)
- putLocalBinaryFile(path,arg);
- else putLocalASCIIFile(path,arg);
- }
- catch(Exception ex){
- showMessage("500 STOR syntax.");
- }
- }
- //retr:GET
- else if(ftpCmd.equals("retr")){
- try{
- String arg=ftpCmdtok.nextToken();
- File path=new File(currentPath,arg);
- if(!path.getCanonicalPath().startsWith(rootPath.getCanonicalPath()))
- throw new SecurityException();
- getRemoteFile(path,arg);
- }
- catch(Exception ex){
- showMessage("500 RETR syntax.");
- }
- }
- //help:Remote Help (remotehelp)
- else if("help".indexOf(ftpCmd)!=-1){
- String strhelp=
- "Java FTP Server -<version 1.0>-ready."+linesep+"Help Message";
- showMessage("200 Remotehelp"+strhelp);
- }
- //noop:No Operation
- else if(ftpCmd.equals("noop"))
- showMessage("200 OK.");
- //quit
- else if(ftpCmd.equals("quit")){
- showMessage("211 Service closing control connection.Goodbye.");
- resetDefault();
- }
- else showMessage("502"+ftpCmd+"nod found.");
- }
- catch(Exception e){
- showMessage("500"+ftpCmd+"Systax.");
- }
- }
- void ftpList(String arg){
- try{
- File path=new File(currentPath,arg);
- if(!path.getCanonicalPath().startsWith(rootPath.getCanonicalPath()))
- throw new SecurityException();
- if(path.isDirectory()){
- String[] filename=path.list();
- PrintWriter outFile=new
- PrintWriter(ds.getOutputStream(),true);
- if(blnBinary)
- showMessage("150 Opening BINARY mode data connection for"+path+".");
- else showMessage("150 Opening ASCII mode data connection for"+path+".");
- for(int i=0;i<filename.length;i++){
- outFile.println(filename[i]);
- }
- outFile.flush();
- outFile.close();
- showMessage("226 Transfer complete.");
- }
- else showMessage("550 Can not find the file specified.");
- }
- catch(Exception e){
- showMessage("550 Can not find the file specified.");
- }
- }
- void changeDirectory(String arg){
- try{
- File path=new File(currentPath,arg);
- if(!path.isDirectory()){
- showMessage("550"+path+"is not a directory.");
- }
- else if(!path.getCanonicalPath().startsWith(rootPath.getCanonicalPath()))
- showMessage("550 Not a subdirectory of"+rootPath.getCanonicalPath());
- else {
- currentPath=new File(path.getCanonicalPath());
- showMessage("250 CWD command successful. OK."+currentPath);
- }
- }
- catch(Exception e){
- showMessage("500"+e);
- }
- }
- void putLocalBinaryFile(File file,String arg){
- byte abyte[]= new byte[1024];
- int inbytes;
- try{
- InputStream dataIn=ds.getInputStream();
- FileOutputStream fileOut=new FileOutputStream(file);
- if(blnBinary)
- showMessage("150 Opening BINARY mode data connection for"+arg+".");
- else showMessage("150 Opening ASCII mode data connection for"+arg+".");
- do{
- inbytes=dataIn.read(abyte);
- if(inbytes!=-1)
- fileOut.write(abyte,0,inbytes);
- }
- while(inbytes!=-1);
- fileOut.flush();
- fileOut.close();
- showMessage("226 Closing data connection.Transfer Complete.");
- }
- catch(Exception e){
- showMessage("550 Exception "+e);
- }
- }
- void putLocalASCIIFile(File file,String arg){
- String strdata;
- try{
- BufferedReader dataIn=new BufferedReader(new InputStreamReader(ds.getInputStream()));
- PrintWriter fileOut=new PrintWriter(
- new FileOutputStream(file),false);
- if(blnBinary)
- showMessage("150 Opening BINARY mode data connection for"+arg+".");
- else showMessage("150 Opening ASCII mode data connection for"+arg+".");
- do{
- strdata=dataIn.readLine();
- if(strdata!=null)
- fileOut.println(strdata);
- }
- while(strdata!=null);
- fileOut.flush();
- fileOut.close();
- showMessage("226 Closing data connection.Transfer Complete.");
- }
- catch(Exception e){
- showMessage("550 Exception "+e);
- }
- }
- void getRemoteFile(File file,String arg){
- byte abyte[]=new byte[1024];
- int inbytes;
- try{
- FileInputStream fileIn=new FileInputStream(file);
- OutputStream dataOut=ds.getOutputStream();
- if(blnBinary)
- showMessage("150 Opening BINARY mode data connection for"+arg+".");
- else showMessage("150 Opening ASCII mode data connection for"+arg+".");
- do{
- inbytes=fileIn.read(abyte);
- if(inbytes!=-1)
- dataOut.write(abyte,0,inbytes);
- }
- while(inbytes!=-1);
- dataOut.flush();
- dataOut.close();
- showMessage("226 Closing data connection.Transfer Complete.");
- }
- catch(Exception e){
- showMessage("550 Exception"+e);
- }
- }
- }