Back to index

wims  3.65+svn20090927
FTP.java
Go to the documentation of this file.
00001 package rene.util.ftp;
00002 
00003 import java.io.*;
00004 import java.net.*;
00005 import java.util.*;
00006 
00011 public class FTP
00012 {      String Server;
00013        int Port;
00014        Socket S;
00015        BufferedReader In;
00016        PrintWriter Out;
00017        Answer A;
00018 
00023        public FTP (String server, int port)
00024        {      Server=server; Port=port;
00025        }
00026 
00027        public FTP (String server)
00028        {      this(server,21);
00029        }
00030 
00035        public void open () throws IOException,UnknownHostException
00036        {      S=new Socket(Server,Port);
00037               In=new BufferedReader(new InputStreamReader(
00038                      new DataInputStream(S.getInputStream())));
00039               Out=new PrintWriter(S.getOutputStream());
00040               if (getreply()/100!=2)
00041                      throw new IOException("Illegal reply.");
00042        }
00043 
00047        public void open (String user, String password)
00048               throws IOException,UnknownHostException
00049        {      open();
00050               if (!command("USER "+user))
00051                      throw new IOException("User not accepted.");
00052               if (!command("PASS "+password))
00053                      throw new IOException("Wrong Password");
00054        }
00055 
00059        public void close () throws IOException
00060        {      send("QUIT");
00061               In.close();
00062               Out.close();
00063               S.close();
00064        }
00065 
00070        public int getreply () throws IOException
00071        {      A=new Answer();
00072               A.get(In);
00073               //System.out.println(A.code());
00074               //System.out.println(A.text());
00075               return A.code();
00076        }
00077 
00083        public boolean command (String s) throws IOException
00084        {      send(s);
00085               return (getreply()/100<4);
00086        }
00087        
00101        public Answer answer ()
00102        {      return A; 
00103        }
00104        
00108        public void send (String s) throws IOException
00109        {      Out.println(s); Out.flush();
00110        }
00111        
00118        public Socket passive ()
00119               throws IOException,UnknownHostException, NumberFormatException
00120        {      if (!command("PASV"))
00121                      throw new IOException("Passive mode not supported.");
00122               StringTokenizer p=new StringTokenizer(A.text(),"(,)");
00123               if (!p.hasMoreTokens())
00124                      throw new IOException("Wrong answer from server.");
00125               else p.nextToken();
00126               int N[]=new int[4];
00127               for (int i=0; i<4; i++)
00128               {      N[i]=Integer.parseInt(p.nextToken());
00129               }
00130               int k=Integer.parseInt(p.nextToken());
00131               int P=k*256+Integer.parseInt(p.nextToken());
00132               String server=N[0]+"."+N[1]+"."+N[2]+"."+N[3];
00133               return new Socket(server,P);
00134        }
00135 
00136        Socket DSocket;
00137 
00142        public InputStream getFile (String file)
00143               throws IOException,UnknownHostException
00144        {      DSocket=passive();
00145               if (!command("TYPE I"))
00146                      throw new IOException("Type I not supported?");
00147               send("RETR "+file);
00148               getreply();
00149               if (A.code()/100>=4) throw new IOException("Get failed.");
00150               return DSocket.getInputStream();
00151        }
00152        
00157        public InputStream getDir (String path)
00158               throws IOException,UnknownHostException
00159        {      DSocket=passive();
00160               if (!command("TYPE A"))
00161                      throw new IOException("Type A not supported?");
00162               if (!path.equals("")) send("LIST "+path);
00163               else send("LIST");
00164               getreply();
00165               if (A.code()/100>=4) throw new IOException("ls failed.");
00166               return DSocket.getInputStream();
00167        }
00168        
00173        public InputStream getLs(String path)
00174               throws IOException,UnknownHostException
00175        {      DSocket=passive();
00176               if (!command("TYPE A"))
00177                      throw new IOException("Type A not supported?");
00178               if (!path.equals("")) send("NLST "+path);
00179               else send("NLST");
00180               getreply();
00181               if (A.code()/100>=4) throw new IOException("ls failed.");
00182               return DSocket.getInputStream();
00183        }
00184        
00189        public void getClose ()
00190               throws IOException
00191        {      DSocket.close();
00192               while (true)
00193               {      getreply();
00194                      if (A.code()==226) return;
00195                      if (A.code()>=400)
00196                             throw new IOException("Put failed.");
00197               }
00198        }
00199 
00204        public OutputStream putFile (String file)
00205               throws IOException,UnknownHostException
00206        {      DSocket=passive();
00207               if (!command("TYPE I"))
00208                      throw new IOException("Type I not supported?");
00209               send("STOR "+file);
00210               getreply();
00211               if (A.code()/100>=4) throw new IOException("Put failed.");
00212               return DSocket.getOutputStream();
00213        }
00214 
00219        public void putClose ()
00220               throws IOException
00221        {      DSocket.close();
00222               while (true)
00223               {      getreply();
00224                      if (A.code()==226) return;
00225                      if (A.code()>=400)
00226                             throw new IOException("Put failed.");
00227               }
00228        }
00229        
00233        public void changeDirectory (String dir)
00234               throws IOException
00235        {      if (!command("CWD "+dir))
00236                      throw new IOException("Directory change failed.");
00237        }
00238        
00239        public Vector getDirectory (String dir)
00240               throws IOException
00241        {      Vector v=new Vector();
00242               try
00243               {      BufferedReader In=new BufferedReader(
00244                             new InputStreamReader(getDir(dir)));
00245                      while (true)
00246                      {      String s=In.readLine();
00247                             if (s==null) break;
00248                             v.addElement(s);
00249                      }
00250                      In.close();
00251               }
00252               catch (Exception e)
00253               {      throw new IOException("Directory list failed.");
00254               }
00255               getClose();
00256               return v;
00257        }
00258        
00259        public Vector getCurrentDirectory ()
00260               throws IOException
00261        {      return getDirectory(".");
00262        }
00263 
00264        static public void main (String args[])
00265        {      try
00266               {      //FTP ftp=new FTP("mathsrv.ku-eichstaett.de");
00267                      //ftp.open("","");
00268                      FTP ftp=new FTP("localhost");
00269                      ftp.open("","");
00270                      testPut(ftp.putFile("test"));
00271                      ftp.putClose();
00272                      testGet(ftp.getFile("test"));
00273                      ftp.getClose();
00274                      Enumeration e=ftp.getCurrentDirectory().elements();
00275                      while (e.hasMoreElements())
00276                      {      System.out.println((String)e.nextElement());
00277                      }
00278                      ftp.close();
00279               }
00280               catch (Exception e)
00281               {      System.out.println(e);
00282               }      
00283        }
00284        
00285        static void testGet (InputStream in)
00286               throws IOException
00287        {      BufferedReader In=new BufferedReader(
00288                      new InputStreamReader(in));
00289               while (true)
00290               {      String s=In.readLine();
00291                      if (s==null) break;
00292                      System.out.println(s);
00293               }
00294               In.close();
00295        }
00296 
00297        static void testPut (OutputStream out)
00298               throws IOException
00299        {      PrintWriter Out=new PrintWriter(out);
00300               Out.println("Erste Zeile: Test");
00301               Out.println("Zweite Zeile: Test");
00302               Out.print("Dritte Zeile");
00303               Out.flush();
00304               Out.close();
00305        }
00306 
00307 }