1    package com.instantbank.collections.util;
2    
3    import java.net.InetAddress;
4    import java.net.ServerSocket;
5    import java.net.Socket;
6    import java.net.UnknownHostException;
7    import java.io.BufferedInputStream;
8    import java.io.BufferedReader;
9    import java.io.FileInputStream;
10   import java.io.FileOutputStream;
11   import java.io.IOException;
12   import java.io.InputStreamReader;
13   import java.io.OutputStream;
14   import java.io.PrintWriter;
15   
16   public class FTPServices {
17     Socket ctrlSocket;
18     public PrintWriter ctrlOutput;
19     public BufferedReader ctrlInput;
20     final int CTRLPORT = 21;
21     String serverType;
22   
23   
24     public FTPServices() { }
25   
26   
27     public void closeConnection() throws IOException {
28       try {
29         ctrlOutput.println("QUIT ");
30         ctrlOutput.flush();
31         ctrlSocket.close();
32       }
33       catch(IOException e) {
34         throw e;
35       }
36     }
37   
38   
39     public Socket dataConnection(String ctrlcmd) throws Exception {
40       String cmd = "PORT ";
41       int i;
42       Socket dataSocket = null;
43   
44       try {
45         byte[] address = InetAddress.getLocalHost().getAddress();
46   
47         ServerSocket serverDataSocket = new ServerSocket(0, 1);
48   
49         for(i = 0; i < 4; ++i) {
50           cmd = cmd + (address[i] & 0xff) + ",";
51         }
52         cmd = cmd + (((serverDataSocket.getLocalPort()) / 256) & 0xff)
53           + ","
54           + (serverDataSocket.getLocalPort() & 0xff);
55   
56         ctrlOutput.println(cmd);
57         ctrlOutput.flush();
58   
59         ctrlOutput.println(ctrlcmd);
60         ctrlOutput.flush();
61   
62         serverDataSocket.setSoTimeout(5000);
63         dataSocket = serverDataSocket.accept();
64         serverDataSocket.close();
65   
66       }
67       catch(Exception e) {
68         throw e;
69       }
70       return dataSocket;
71     }
72   
73   
74     public void doAscii() throws Exception {
75       try {
76         ctrlOutput.println("TYPE A");
77         ctrlOutput.flush();
78       }
79       catch(Exception e) {
80         throw e;
81       }
82     }
83   
84   
85     public void doBinary() throws Exception {
86       try {
87         ctrlOutput.println("TYPE I");
88         ctrlOutput.flush();
89       }
90       catch(Exception e) {
91         throw e;
92       }
93     }
94   
95   
96     public void doCd(String dirName) throws Exception {
97       try {
98         ctrlOutput.println("CWD " + dirName);
99         ctrlOutput.flush();
100      }
101      catch(Exception e) {
102        throw e;
103      }
104    }
105  
106  
107    public void doLs() throws Exception {
108      try {
109        int n;
110        byte[] buff = new byte[1024];
111  
112        Socket dataSocket = dataConnection("LIST");
113  
114        BufferedInputStream dataInput = new BufferedInputStream(dataSocket.getInputStream());
115  
116        while((n = dataInput.read(buff)) > 0) {
117          System.out.write(buff, 0, n);
118        }
119        dataSocket.close();
120      }
121      catch(Exception e) {
122        throw e;
123      }
124    }
125  
126  
127    public void doGet(String fileName, String path) throws Exception {
128  
129      try {
130        byte[] buff = new byte[1024];
131        FileServices f = new FileServices();
132        int n;
133        Socket dataSocket = null;
134  
135        FileOutputStream outfile = new FileOutputStream(fileName);
136        if(serverType.equals("windows")) {
137          dataSocket = dataConnection("RECV " + fileName);
138        }
139        else
140          if(serverType.equals("unix")) {
141          dataSocket = dataConnection("RETR " + fileName);
142        }
143  
144        BufferedInputStream dataInput = new BufferedInputStream(dataSocket.getInputStream());
145        while((n = dataInput.read(buff)) > 0) {
146          outfile.write(buff, 0, n);
147        }
148        dataSocket.close();
149        outfile.close();
150        f.copy(fileName, "", path);
151        f.delete(fileName, "");
152  
153      }
154      catch(Exception e) {
155        throw e;
156      }
157    }
158  
159  
160    public void doPut(String fileName, String path) throws Exception {
161  
162      try {
163        byte[] buff = new byte[1024];
164        FileServices f = new FileServices();
165        int n;
166        FileInputStream sendfile = null;
167        Socket dataSocket = null;
168  
169        f.copy(fileName, path, "");
170  
171        try {
172          sendfile = new FileInputStream(fileName);
173        }
174        catch(Exception e) {
175          throw e;
176        }
177  
178        if(serverType.equals("windows")) {
179          dataSocket = dataConnection("SEND " + fileName);
180        }
181        else
182          if(serverType.equals("unix")) {
183          dataSocket = dataConnection("STOR " + fileName);
184        }
185  
186        OutputStream outstr = dataSocket.getOutputStream();
187  
188        while((n = sendfile.read(buff)) > 0) {
189          outstr.write(buff, 0, n);
190        }
191        dataSocket.close();
192        sendfile.close();
193  
194        f.delete(fileName, "");
195  
196      }
197      catch(Exception e) {
198        throw e;
199      }
200    }
201  
202  
203    public FTPServices(String type) {
204      serverType = type;
205    }
206  
207  
208    public String getServerType(String type) {
209      return serverType;
210    }
211  
212  
213    public void openConnection(String host, String userName, String password) throws IOException, UnknownHostException {
214      openConnection(host, CTRLPORT, userName, password);
215    }
216  
217  
218    public void openConnection(String host, int port, String userName, String password) throws IOException, UnknownHostException {
219      try {
220        ctrlSocket = new Socket(host, port);
221        ctrlOutput = new PrintWriter(ctrlSocket.getOutputStream());
222        ctrlInput = new BufferedReader(new InputStreamReader(ctrlSocket.getInputStream()));
223  
224        ctrlOutput.println("USER " + userName);
225        ctrlOutput.flush();
226        ctrlOutput.println("PASS " + password);
227        ctrlOutput.flush();
228      }
229      catch(IOException e) {
230        throw e;
231      }
232    }
233  
234  
235    public void setServerType(String type) {
236      serverType = type;
237    }
238  
239  }
240  
241