1 //////////////////////////////////////////////////////////////////////////
2 // Copyright (C) Zhou Xiao-wei, Apr. 2007.
3 // Copyright (C) Wang Cong, Apr. 2007.
4 // See AUTHORS and CREDITS to learn more.
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License version 2 as
7 // published by the Free Software Foundation.
8 // See COPYING to learn more.
9 //////////////////////////////////////////////////////////////////////////
11 import java
.awt
.Container
;
16 * This class is for FTP connection.
17 * Extends XylTelnetConnection.
18 * @author WANG Cong, Zhou Xiao-Wei
21 public class XylFTPConnection
extends XylTelnetConnection
{
24 *The size of reader's buffer.
26 private final int RDBUF_SIZE
= 1024;
29 *The size of writer's buffer.
31 private final int WRBUF_SIZE
= 1024;
34 *The status of FTP connection.
37 // 0 for not connected
38 // 1 for connected but not login
39 // 2 for login and no data transfer
40 // 3 for login and getting data down
41 // 4 for login and putting data up
44 *Presents the mode (active or passive) of FTP connection.
47 //1 for passive, 0 for active, default to 1. ;)
50 *Constructs a LocalFile for data storage.
52 private String LocalFile
;
53 // Empty string for this means stdout. ;)
56 *The data connection socket.
58 private Socket DataSocket
;
61 *The sever data socket, used by active mode.
63 private ServerSocket DataServerSocket
;
68 private BufferedInputStream FTPInStream
;
73 private BufferedOutputStream FTPOutStream
;
76 *Initializes some values.
84 DataServerSocket
= null;
88 *Gets status of current FTP connection.
89 *@return the status of connection
91 public int GetStatus(){
96 *Sets status of the connection.
99 public void SetStatus(int NewStatus
){
104 *Sets the LocalFile name.
107 public void SetLocalFile(String FileName
){
108 LocalFile
= FileName
;
112 *Tests whether the mode is passive
114 public boolean IsPassive(){
121 *Sets the passive mode.
123 public void SetPassive(){
128 *Sets the active mode.
130 public void SetActive(){
135 *Opens the data connection in active mode.
137 *@throws Exception on opening dataconnection error
139 public void OpenDataConnection() throws Exception
{
141 DataSocket
= DataServerSocket
.accept();
142 } catch(Exception e
) {
148 public void ListenForDataConnection() throws Exception
{
150 if(-1 == ReturnSelfPort())
151 throw new XylFTPException("Port is unavailable.");
152 DataServerSocket
= new ServerSocket(ReturnSelfPort());
153 DataServerSocket
.setSoTimeout(Timeout
);
154 } catch(Exception e
) {
161 *Opens the data connection in passive mode.
162 *@param Port the port of server
163 *@param Host the IP of server
165 *@throws Exception on opening DataConnection error
167 public void OpenDataConnection(String Host
, int Port
) throws Exception
{
169 DataSocket
= new Socket(Host
, Port
);
170 DataSocket
.setSoTimeout(Timeout
);
171 } catch (Exception e
) {
178 *Closes the data connection.
179 *@see XylFTPException
180 *@throws XylFTPException on closing DataConnection error
182 public void CloseDataConnection() throws Exception
{
183 if(DataSocket
!= null) {
187 if(DataServerSocket
!= null) {
188 DataServerSocket
.close();
189 DataServerSocket
= null;
194 *Uploads a file in active mode.
195 *@throws XylFTPException on sending file error
196 *@see XylFTPException
198 public void SendFileActive() throws Exception
{
200 byte [] buf
= new byte[WRBUF_SIZE
];
203 throw new XylFTPException("You are not in active mode!");
205 if (LocalFile
.equals("")) {
207 throw new XylFTPException("Can't send unnamed file!");
209 FTPInStream
= new BufferedInputStream(new FileInputStream(LocalFile
));
211 FTPOutStream
= new BufferedOutputStream(DataSocket
.getOutputStream());
212 while ((bytesRead
= FTPInStream
.read(buf
, 0, WRBUF_SIZE
)) > 0) {
213 FTPOutStream
.write(buf
, 0, bytesRead
);
215 FTPOutStream
.flush();
216 FTPOutStream
.close();
220 *Uploads a file in passive mode.
221 *@throws XylFTPException on sending file error
222 *@see XylFTPException
224 public void SendFilePassive() throws Exception
{
226 byte [] buf
= new byte[WRBUF_SIZE
];
229 throw new XylFTPException("You are not in passive mode!");
231 if (LocalFile
.equals("")) {
233 throw new XylFTPException("Can't send unnamed file!");
235 FTPInStream
= new BufferedInputStream(new FileInputStream(LocalFile
));
237 FTPOutStream
= new BufferedOutputStream(DataSocket
.getOutputStream());
238 while ((bytesRead
= FTPInStream
.read(buf
, 0, WRBUF_SIZE
)) > 0) {
239 FTPOutStream
.write(buf
, 0, bytesRead
);
241 FTPOutStream
.flush();
242 FTPOutStream
.close();
247 *Downloads a file in active mode.
248 *@throws XylFTPException on getting file error
249 *@see XylFTPException
251 public void GetFileActive() throws Exception
{
253 byte [] buf
= new byte[RDBUF_SIZE
];
256 throw new XylFTPException("You are not in active mode!");
258 if (LocalFile
.equals("")) {
259 FTPOutStream
= new BufferedOutputStream((OutputStream
)System
.out
);
261 FTPOutStream
= new BufferedOutputStream(new FileOutputStream(LocalFile
));
263 FTPInStream
= new BufferedInputStream(DataSocket
.getInputStream());
264 while ((bytesRead
= FTPInStream
.read(buf
, 0, RDBUF_SIZE
)) > 0) {
265 FTPOutStream
.write(buf
, 0, bytesRead
);
267 FTPOutStream
.flush();
268 if (!LocalFile
.equals("")) //We can NOT close the stdout.
269 FTPOutStream
.close();
274 *Downloads a file in passive mode.
275 *@throws XylFTPException on getting file error
276 *@see XylFTPException
278 public void GetFilePassive() throws Exception
{
280 byte [] buf
= new byte[RDBUF_SIZE
];
283 throw new XylFTPException("You are not in passive mode!");
285 if (LocalFile
.equals("")) {
286 FTPOutStream
= new BufferedOutputStream((OutputStream
)System
.out
);
288 FTPOutStream
= new BufferedOutputStream(new FileOutputStream(LocalFile
));
290 FTPInStream
= new BufferedInputStream(DataSocket
.getInputStream());
291 while ((bytesRead
= FTPInStream
.read(buf
, 0, RDBUF_SIZE
)) > 0) {
292 FTPOutStream
.write(buf
, 0, bytesRead
);
294 FTPOutStream
.flush();
295 if (!LocalFile
.equals("")) //We can NOT close the stdout.
296 FTPOutStream
.close();
300 public String
GetListActive() throws Exception
{
302 byte [] buf
= new byte[RDBUF_SIZE
];
305 throw new XylFTPException("You are not in active mode!");
308 ByteArrayOutputStream bArrayStream
= new ByteArrayOutputStream();
309 FTPOutStream
= new BufferedOutputStream(bArrayStream
);
311 FTPInStream
= new BufferedInputStream(DataSocket
.getInputStream());
312 while ((bytesRead
= FTPInStream
.read(buf
, 0, RDBUF_SIZE
)) > 0) {
313 FTPOutStream
.write(buf
, 0, bytesRead
);
315 FTPOutStream
.flush();
316 FTPOutStream
.close();
319 return bArrayStream
.toString();
322 public String
GetListPassive() throws Exception
{
324 byte [] buf
= new byte[RDBUF_SIZE
];
327 throw new XylFTPException("You are not in passive mode!");
330 ByteArrayOutputStream bArrayStream
= new ByteArrayOutputStream();
331 FTPOutStream
= new BufferedOutputStream(bArrayStream
);
333 FTPInStream
= new BufferedInputStream(DataSocket
.getInputStream());
334 while ((bytesRead
= FTPInStream
.read(buf
, 0, RDBUF_SIZE
)) > 0) {
335 FTPOutStream
.write(buf
, 0, bytesRead
);
337 FTPOutStream
.flush();
338 FTPOutStream
.close();
341 return bArrayStream
.toString();