121 |
private static final String US_ASCII = "US-ASCII"; |
private static final String US_ASCII = "US-ASCII"; |
122 |
private static final byte[] CRLF = { 0x0d, 0x0a }; |
private static final byte[] CRLF = { 0x0d, 0x0a }; |
123 |
|
|
124 |
/** |
/** |
125 |
* The socket used to communicate with the server. |
* The socket used to communicate with the server. |
126 |
*/ |
*/ |
127 |
protected Socket socket; |
protected Socket socket; |
128 |
|
|
129 |
/** |
/** |
130 |
* The socket input stream. |
* The socket input stream. |
131 |
*/ |
*/ |
132 |
protected LineInputStream in; |
protected LineInputStream in; |
133 |
|
|
134 |
/** |
/** |
135 |
* The socket output stream. |
* The socket output stream. |
136 |
*/ |
*/ |
137 |
protected OutputStream out; |
protected OutputStream out; |
138 |
|
|
139 |
/** |
/** |
140 |
* If true, print debugging information. |
* If true, print debugging information. |
141 |
*/ |
*/ |
142 |
protected boolean debug; |
protected boolean debug; |
143 |
|
|
144 |
/** |
/** |
145 |
* The current data transfer process in use by this connection. |
* The current data transfer process in use by this connection. |
146 |
*/ |
*/ |
147 |
protected DTP dtp; |
protected DTP dtp; |
148 |
|
|
149 |
/** |
/** |
150 |
* The current representation type. |
* The current representation type. |
151 |
*/ |
*/ |
152 |
protected int representationType = TYPE_ASCII; |
protected int representationType = TYPE_ASCII; |
153 |
|
|
154 |
/** |
/** |
155 |
* The current file structure type. |
* The current file structure type. |
156 |
*/ |
*/ |
157 |
protected int fileStructure = STRUCTURE_FILE; |
protected int fileStructure = STRUCTURE_FILE; |
158 |
|
|
159 |
/** |
/** |
160 |
* The current transfer mode. |
* The current transfer mode. |
161 |
*/ |
*/ |
162 |
protected int transferMode = MODE_STREAM; |
protected int transferMode = MODE_STREAM; |
163 |
|
|
164 |
/** |
/** |
165 |
* If true, use passive mode. |
* If true, use passive mode. |
166 |
*/ |
*/ |
167 |
protected boolean passive = false; |
protected boolean passive = false; |
226 |
} |
} |
227 |
} |
} |
228 |
|
|
229 |
/** |
/** |
230 |
* Authenticate using the specified username and password. |
* Authenticate using the specified username and password. |
231 |
* If the username suffices for the server, the password will not be used |
* If the username suffices for the server, the password will not be used |
232 |
* and may be null. |
* and may be null. |
243 |
FTPResponse response = getResponse(); |
FTPResponse response = getResponse(); |
244 |
switch (response.getCode()) |
switch (response.getCode()) |
245 |
{ |
{ |
246 |
case 230: // User logged in |
case 230: // User logged in |
247 |
return true; |
return true; |
248 |
case 331: // User name okay, need password |
case 331: // User name okay, need password |
249 |
break; |
break; |
250 |
case 332: // Need account for login |
case 332: // Need account for login |
251 |
case 530: // No such user |
case 530: // No such user |
252 |
return false; |
return false; |
253 |
default:throw new FTPException(response); |
default:throw new FTPException(response); |
254 |
} |
} |
255 |
cmd = new StringBuffer(PASS).append(' ').append(password).toString(); |
cmd = new StringBuffer(PASS).append(' ').append(password).toString(); |
257 |
response = getResponse(); |
response = getResponse(); |
258 |
switch (response.getCode()) |
switch (response.getCode()) |
259 |
{ |
{ |
260 |
case 230: // User logged in |
case 230: // User logged in |
261 |
case 202: // Superfluous |
case 202: // Superfluous |
262 |
return true; |
return true; |
263 |
case 332: // Need account for login |
case 332: // Need account for login |
264 |
case 530: // Bad password |
case 530: // Bad password |
265 |
return false; |
return false; |
266 |
default: |
default: |
267 |
throw new FTPException(response); |
throw new FTPException(response); |
268 |
} |
} |
269 |
} |
} |
270 |
|
|
271 |
/** |
/** |
272 |
* Changes directory to the specified path. |
* Changes directory to the specified path. |
273 |
* @param path an absolute or relative pathname |
* @param path an absolute or relative pathname |
274 |
* @return true on success, false if the specified path does not exist |
* @return true on success, false if the specified path does not exist |
276 |
public boolean changeWorkingDirectory(String path) throws IOException |
public boolean changeWorkingDirectory(String path) throws IOException |
277 |
{ |
{ |
278 |
String cmd = new StringBuffer(CWD).append(' ').append(path).toString(); |
String cmd = new StringBuffer(CWD).append(' ').append(path).toString(); |
279 |
send(cmd); |
send(cmd); |
280 |
FTPResponse response = getResponse(); |
FTPResponse response = getResponse(); |
281 |
switch (response.getCode()) |
switch (response.getCode()) |
282 |
{ |
{ |
283 |
case 250: |
case 250: |
284 |
return true; |
return true; |
285 |
case 550:return false; |
case 550:return false; |
286 |
default:throw new FTPException(response); |
default:throw new FTPException(response); |
287 |
} |
} |
288 |
} |
} |
289 |
|
|
290 |
/** |
/** |
291 |
* Changes directory to the parent of the current working directory. |
* Changes directory to the parent of the current working directory. |
292 |
* @return true on success, false otherwise |
* @return true on success, false otherwise |
293 |
*/ |
*/ |
297 |
FTPResponse response = getResponse(); |
FTPResponse response = getResponse(); |
298 |
switch (response.getCode()) |
switch (response.getCode()) |
299 |
{ |
{ |
300 |
case 250: |
case 250: |
301 |
return true; |
return true; |
302 |
case 550:return false; |
case 550:return false; |
303 |
default:throw new FTPException(response); |
default:throw new FTPException(response); |
304 |
} |
} |
305 |
} |
} |
306 |
|
|
307 |
/** |
/** |
308 |
* Terminates an authenticated login. |
* Terminates an authenticated login. |
309 |
* If file transfer is in progress, it remains active for result response |
* If file transfer is in progress, it remains active for result response |
310 |
* only. |
* only. |
327 |
} |
} |
328 |
} |
} |
329 |
|
|
330 |
/** |
/** |
331 |
* Terminates the control connection. |
* Terminates the control connection. |
332 |
* The file transfer connection remains open for result response only. |
* The file transfer connection remains open for result response only. |
333 |
* This connection is invalid and no further commands may be issued. |
* This connection is invalid and no further commands may be issued. |
356 |
} |
} |
357 |
} |
} |
358 |
|
|
359 |
/** |
/** |
360 |
* Initialise the data transfer process. |
* Initialise the data transfer process. |
361 |
*/ |
*/ |
362 |
protected void initialiseDTP() throws IOException |
protected void initialiseDTP() throws IOException |
470 |
dtp.setTransferMode(transferMode); |
dtp.setTransferMode(transferMode); |
471 |
} |
} |
472 |
|
|
473 |
/** |
/** |
474 |
* Set passive mode. |
* Set passive mode. |
475 |
* @param flag true if we should use passive mode, false otherwise |
* @param flag true if we should use passive mode, false otherwise |
476 |
*/ |
*/ |
483 |
} |
} |
484 |
} |
} |
485 |
|
|
486 |
/** |
/** |
487 |
* Returns the current representation type of the transfer data. |
* Returns the current representation type of the transfer data. |
488 |
* @return TYPE_ASCII, TYPE_EBCDIC, or TYPE_BINARY |
* @return TYPE_ASCII, TYPE_EBCDIC, or TYPE_BINARY |
489 |
*/ |
*/ |
492 |
return representationType; |
return representationType; |
493 |
} |
} |
494 |
|
|
495 |
/** |
/** |
496 |
* Sets the desired representation type of the transfer data. |
* Sets the desired representation type of the transfer data. |
497 |
* @param type TYPE_ASCII, TYPE_EBCDIC, or TYPE_BINARY |
* @param type TYPE_ASCII, TYPE_EBCDIC, or TYPE_BINARY |
498 |
*/ |
*/ |
499 |
public void setRepresentationType(int type) throws IOException |
public void setRepresentationType(int type) throws IOException |
525 |
} |
} |
526 |
} |
} |
527 |
|
|
528 |
/** |
/** |
529 |
* Returns the current file structure type. |
* Returns the current file structure type. |
530 |
* @return STRUCTURE_FILE, STRUCTURE_RECORD, or STRUCTURE_PAGE |
* @return STRUCTURE_FILE, STRUCTURE_RECORD, or STRUCTURE_PAGE |
531 |
*/ |
*/ |
534 |
return fileStructure; |
return fileStructure; |
535 |
} |
} |
536 |
|
|
537 |
/** |
/** |
538 |
* Sets the desired file structure type. |
* Sets the desired file structure type. |
539 |
* @param structure STRUCTURE_FILE, STRUCTURE_RECORD, or STRUCTURE_PAGE |
* @param structure STRUCTURE_FILE, STRUCTURE_RECORD, or STRUCTURE_PAGE |
540 |
*/ |
*/ |
565 |
} |
} |
566 |
} |
} |
567 |
|
|
568 |
/** |
/** |
569 |
* Returns the current transfer mode. |
* Returns the current transfer mode. |
570 |
* @return MODE_STREAM, MODE_BLOCK, or MODE_COMPRESSED |
* @return MODE_STREAM, MODE_BLOCK, or MODE_COMPRESSED |
571 |
*/ |
*/ |
574 |
return transferMode; |
return transferMode; |
575 |
} |
} |
576 |
|
|
577 |
/** |
/** |
578 |
* Sets the desired transfer mode. |
* Sets the desired transfer mode. |
579 |
* @param mode MODE_STREAM, MODE_BLOCK, or MODE_COMPRESSED |
* @param mode MODE_STREAM, MODE_BLOCK, or MODE_COMPRESSED |
580 |
*/ |
*/ |
607 |
} |
} |
608 |
} |
} |
609 |
|
|
610 |
/** |
/** |
611 |
* Retrieves the specified file. |
* Retrieves the specified file. |
612 |
* @param filename the filename of the file to retrieve |
* @param filename the filename of the file to retrieve |
613 |
* @return an InputStream containing the file content |
* @return an InputStream containing the file content |
647 |
} |
} |
648 |
} |
} |
649 |
|
|
650 |
/** |
/** |
651 |
* Returns a stream for uploading a file. |
* Returns a stream for uploading a file. |
652 |
* If a file with the same filename already exists on the server, it will |
* If a file with the same filename already exists on the server, it will |
653 |
* be overwritten. |
* be overwritten. |
671 |
} |
} |
672 |
} |
} |
673 |
|
|
674 |
/** |
/** |
675 |
* Returns a stream for uploading a file. |
* Returns a stream for uploading a file. |
676 |
* If a file with the same filename already exists on the server, the |
* If a file with the same filename already exists on the server, the |
677 |
* content specified will be appended to the existing file. |
* content specified will be appended to the existing file. |
695 |
} |
} |
696 |
} |
} |
697 |
|
|
698 |
/** |
/** |
699 |
* This command may be required by some servers to reserve sufficient |
* This command may be required by some servers to reserve sufficient |
700 |
* storage to accommodate the new file to be transferred. |
* storage to accommodate the new file to be transferred. |
701 |
* It should be immediately followed by a <code>store</code> or |
* It should be immediately followed by a <code>store</code> or |
716 |
} |
} |
717 |
} |
} |
718 |
|
|
719 |
/** |
/** |
720 |
* Renames a file. |
* Renames a file. |
721 |
* @param oldName the current name of the file |
* @param oldName the current name of the file |
722 |
* @param newName the new name |
* @param newName the new name |
752 |
} |
} |
753 |
} |
} |
754 |
|
|
755 |
/** |
/** |
756 |
* Aborts the transfer in progress. |
* Aborts the transfer in progress. |
757 |
* @return true if a transfer was in progress, false otherwise |
* @return true if a transfer was in progress, false otherwise |
758 |
*/ |
*/ |
775 |
} |
} |
776 |
} |
} |
777 |
|
|
778 |
/** |
/** |
779 |
* Causes the file specified to be deleted at the server site. |
* Causes the file specified to be deleted at the server site. |
780 |
* @param filename the file to delete |
* @param filename the file to delete |
781 |
*/ |
*/ |
796 |
} |
} |
797 |
} |
} |
798 |
|
|
799 |
/** |
/** |
800 |
* Causes the directory specified to be deleted. |
* Causes the directory specified to be deleted. |
801 |
* This may be an absolute or relative pathname. |
* This may be an absolute or relative pathname. |
802 |
* @param pathname the directory to delete |
* @param pathname the directory to delete |
817 |
} |
} |
818 |
} |
} |
819 |
|
|
820 |
/** |
/** |
821 |
* Causes the directory specified to be created at the server site. |
* Causes the directory specified to be created at the server site. |
822 |
* This may be an absolute or relative pathname. |
* This may be an absolute or relative pathname. |
823 |
* @param pathname the directory to create |
* @param pathname the directory to create |
838 |
} |
} |
839 |
} |
} |
840 |
|
|
841 |
/** |
/** |
842 |
* Returns the current working directory. |
* Returns the current working directory. |
843 |
*/ |
*/ |
844 |
public String getWorkingDirectory() throws IOException |
public String getWorkingDirectory() throws IOException |
869 |
} |
} |
870 |
} |
} |
871 |
|
|
872 |
/** |
/** |
873 |
* Returns a listing of information about the specified pathname. |
* Returns a listing of information about the specified pathname. |
874 |
* If the pathname specifies a directory or other group of files, the |
* If the pathname specifies a directory or other group of files, the |
875 |
* server should transfer a list of files in the specified directory. |
* server should transfer a list of files in the specified directory. |
901 |
} |
} |
902 |
} |
} |
903 |
|
|
904 |
/** |
/** |
905 |
* Returns a directory listing. The pathname should specify a |
* Returns a directory listing. The pathname should specify a |
906 |
* directory or other system-specific file group descriptor; a null |
* directory or other system-specific file group descriptor; a null |
907 |
* argument implies the user's current working or default directory. |
* argument implies the user's current working or default directory. |
939 |
} |
} |
940 |
} |
} |
941 |
|
|
942 |
/** |
/** |
943 |
* Returns the type of operating system at the server. |
* Returns the type of operating system at the server. |
944 |
*/ |
*/ |
945 |
public String system() throws IOException |
public String system() throws IOException |
959 |
} |
} |
960 |
} |
} |
961 |
|
|
962 |
/** |
/** |
963 |
* Does nothing. |
* Does nothing. |
964 |
* This method can be used to ensure that the connection does not time |
* This method can be used to ensure that the connection does not time |
965 |
* out. |
* out. |
978 |
|
|
979 |
// -- I/O -- |
// -- I/O -- |
980 |
|
|
981 |
/** |
/** |
982 |
* Sends the specified command line to the server. |
* Sends the specified command line to the server. |
983 |
* The CRLF sequence is automatically appended. |
* The CRLF sequence is automatically appended. |
984 |
* @param cmd the command line to send |
* @param cmd the command line to send |
991 |
out.flush(); |
out.flush(); |
992 |
} |
} |
993 |
|
|
994 |
/** |
/** |
995 |
* Reads the next response from the server. |
* Reads the next response from the server. |
996 |
* If the server sends the "transfer complete" code, this is handled here, |
* If the server sends the "transfer complete" code, this is handled here, |
997 |
* and the next response is passed to the caller. |
* and the next response is passed to the caller. |
1008 |
return response; |
return response; |
1009 |
} |
} |
1010 |
|
|
1011 |
/** |
/** |
1012 |
* Reads and parses the next response from the server. |
* Reads and parses the next response from the server. |
1013 |
*/ |
*/ |
1014 |
protected FTPResponse readResponse() throws IOException |
protected FTPResponse readResponse() throws IOException |