Back to index

wims  3.65+svn20090927
SFTP.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 import javax.net.ssl.*;
00007 
00012 public class SFTP
00013 {      String Server;
00014        int Port;
00015        SSLSocket S;
00016        BufferedReader In;
00017        PrintWriter Out;
00018        Answer A;
00019 
00024        public SFTP (String server, int port)
00025        {      Server=server; Port=port;
00026        }
00027 
00028        public SFTP (String server)
00029        {      this(server,22);
00030        }
00031        
00032        public static SSLSocket getSSLSocket (String server, int port)
00033               throws IOException,UnknownHostException
00034        {      SSLSocketFactory ssf=(SSLSocketFactory)SSLSocketFactory.getDefault(); 
00035               SSLSocket S=(SSLSocket)ssf.createSocket(server,port);
00036               S.startHandshake();
00037               return S;
00038        }
00039 
00044        public void open () 
00045               throws IOException,UnknownHostException
00046        {      S=getSSLSocket(Server,Port);
00047               In=new BufferedReader(new InputStreamReader(
00048                      new DataInputStream(S.getInputStream())));
00049               Out=new PrintWriter(S.getOutputStream());
00050               if (getreply()/100!=2)
00051                      throw new IOException("Illegal reply.");
00052        }
00053 
00057        public void open (String user, String password)
00058               throws IOException,UnknownHostException
00059        {      open();
00060               if (!command("USER "+user))
00061                      throw new IOException("User not accepted.");
00062               if (!command("PASS "+password))
00063                      throw new IOException("Wrong Password");
00064        }
00065 
00069        public void close () throws IOException
00070        {      send("QUIT");
00071               In.close();
00072               Out.close();
00073               S.close();
00074        }
00075 
00080        public int getreply () throws IOException
00081        {      A=new Answer();
00082               A.get(In);
00083               //System.out.println(A.code());
00084               //System.out.println(A.text());
00085               return A.code();
00086        }
00087 
00093        public boolean command (String s) throws IOException
00094        {      send(s);
00095               return (getreply()/100<4);
00096        }
00097        
00111        public Answer answer ()
00112        {      return A; 
00113        }
00114        
00118        public void send (String s) throws IOException
00119        {      Out.println(s); Out.flush();
00120        }
00121        
00128        public SSLSocket passive ()
00129               throws IOException,UnknownHostException, NumberFormatException
00130        {      if (!command("PASV"))
00131                      throw new IOException("Passive mode not supported.");
00132               StringTokenizer p=new StringTokenizer(A.text(),"(,)");
00133               if (!p.hasMoreTokens())
00134                      throw new IOException("Wrong answer from server.");
00135               else p.nextToken();
00136               int N[]=new int[4];
00137               for (int i=0; i<4; i++)
00138               {      N[i]=Integer.parseInt(p.nextToken());
00139               }
00140               int k=Integer.parseInt(p.nextToken());
00141               int P=k*256+Integer.parseInt(p.nextToken());
00142               String server=N[0]+"."+N[1]+"."+N[2]+"."+N[3];
00143               return getSSLSocket(server,P);
00144        }
00145 
00146        SSLSocket DSocket;
00147 
00152        public InputStream getFile (String file)
00153               throws IOException,UnknownHostException
00154        {      DSocket=passive();
00155               if (!command("TYPE I"))
00156                      throw new IOException("Type I not supported?");
00157               send("RETR "+file);
00158               getreply();
00159               if (A.code()/100>=4) throw new IOException("Get failed.");
00160               return DSocket.getInputStream();
00161        }
00162        
00167        public InputStream getDir (String path)
00168               throws IOException,UnknownHostException
00169        {      DSocket=passive();
00170               if (!command("TYPE A"))
00171                      throw new IOException("Type A not supported?");
00172               if (!path.equals("")) send("LIST "+path);
00173               else send("LIST");
00174               getreply();
00175               if (A.code()/100>=4) throw new IOException("ls failed.");
00176               return DSocket.getInputStream();
00177        }
00178        
00183        public InputStream getLs(String path)
00184               throws IOException,UnknownHostException
00185        {      DSocket=passive();
00186               if (!command("TYPE A"))
00187                      throw new IOException("Type A not supported?");
00188               if (!path.equals("")) send("NLST "+path);
00189               else send("NLST");
00190               getreply();
00191               if (A.code()/100>=4) throw new IOException("ls failed.");
00192               return DSocket.getInputStream();
00193        }
00194        
00199        public void getClose ()
00200               throws IOException
00201        {      DSocket.close();
00202               while (true)
00203               {      getreply();
00204                      if (A.code()==226) return;
00205                      if (A.code()>=400)
00206                             throw new IOException("Put failed.");
00207               }
00208        }
00209 
00214        public OutputStream putFile (String file)
00215               throws IOException,UnknownHostException
00216        {      DSocket=passive();
00217               if (!command("TYPE I"))
00218                      throw new IOException("Type I not supported?");
00219               send("STOR "+file);
00220               getreply();
00221               if (A.code()/100>=4) throw new IOException("Put failed.");
00222               return DSocket.getOutputStream();
00223        }
00224 
00229        public void putClose ()
00230               throws IOException
00231        {      DSocket.close();
00232               while (true)
00233               {      getreply();
00234                      if (A.code()==226) return;
00235                      if (A.code()>=400)
00236                             throw new IOException("Put failed.");
00237               }
00238        }
00239        
00243        public void changeDirectory (String dir)
00244               throws IOException
00245        {      if (!command("CWD "+dir))
00246                      throw new IOException("Directory change failed.");
00247        }
00248        
00249        public Vector getDirectory (String dir)
00250               throws IOException
00251        {      Vector v=new Vector();
00252               try
00253               {      BufferedReader In=new BufferedReader(
00254                             new InputStreamReader(getDir(dir)));
00255                      while (true)
00256                      {      String s=In.readLine();
00257                             if (s==null) break;
00258                             v.addElement(s);
00259                      }
00260                      In.close();
00261               }
00262               catch (Exception e)
00263               {      throw new IOException("Directory list failed.");
00264               }
00265               getClose();
00266               return v;
00267        }
00268        
00269        public Vector getCurrentDirectory ()
00270               throws IOException
00271        {      return getDirectory(".");
00272        }
00273 
00274        static public void main (String args[])
00275        {      try
00276               {      SFTP ftp=new SFTP(args[0]);
00277                      ftp.open(args[1],args[2]);
00278                      Enumeration e=ftp.getCurrentDirectory().elements();
00279                      while (e.hasMoreElements())
00280                      {      System.out.println((String)e.nextElement());
00281                      }
00282                      ftp.close();
00283               }
00284               catch (Exception e)
00285               {      System.out.println(e);
00286                      e.printStackTrace();
00287               }      
00288        }
00289        
00290 }