/Users/lyon/j4p/src/net/ftp/FTPCommands.java

1    package net.ftp; 
2     
3    import futils.Futil; 
4    import sun.net.ftp.FtpClient; 
5     
6    import java.io.FileOutputStream; 
7    import java.io.IOException; 
8    import java.io.InputStream; 
9     
10   public class FTPCommands { 
11    
12       public static void getFileAndStore( 
13               String fileName, 
14               String directory, 
15               String hostName, 
16               String userName, 
17               String passWord) { 
18           try { 
19               FtpClient fc = 
20                       new FtpClient(hostName); 
21               fc.login(userName, passWord); 
22               fc.binary(); 
23               fc.cd(directory); 
24               InputStream theFile = fc.get( 
25                       fileName); 
26               int i = 0; 
27               FileOutputStream fos = new FileOutputStream( 
28                       Futil.getWriteFile( 
29                               "set output path")); 
30               while ((i = theFile.read()) != -1) { 
31                   fos.write((byte) i); 
32               } 
33               fc.closeServer(); 
34               fos.close(); 
35           } catch (IOException e) { 
36               System.err.println(e); 
37           } 
38       } 
39    
40       public static String 
41               getFileList(String directory, 
42                           String hostName, 
43                           String userName, 
44                           String passWord) { 
45           byte buf[] = new byte[8192]; 
46           String s = ""; 
47           try { 
48               FtpClient fc = 
49                       new FtpClient(hostName); 
50               fc.login(userName, 
51                        passWord); 
52               fc.binary(); 
53               fc.cd(directory); 
54               InputStream is = 
55                       fc.list(); 
56               int offset = 0; 
57               int count = buf.length; 
58               int n = 0; 
59               while ((n = is.read(buf, 
60                                   offset, count)) > 
61                      0) 
62                   s = s + new String(buf); 
63               fc.closeServer(); 
64           } catch (IOException e) { 
65               System.err.println(e); 
66           } 
67           return s; 
68       } 
69    
70       public static void listFilesToFile( 
71               String directory, 
72               String hostName, 
73               String userName, 
74               String passWord) { 
75           try { 
76               FtpClient fc = 
77                       new FtpClient(hostName); 
78               fc.login(userName, 
79                        passWord); 
80               fc.binary(); 
81               fc.cd(directory); 
82               InputStream theFile = 
83                       fc.list(); 
84               int i = 0; 
85               FileOutputStream fos = new FileOutputStream( 
86                       Futil.getWriteFile("ftp output file")); 
87               while ((i = theFile.read()) != -1) { 
88                   fos.write((byte) i); 
89               } 
90               fc.closeServer(); 
91               fos.close(); 
92           } catch (IOException e) { 
93               System.err.println(e); 
94           } 
95       } 
96   }