1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
package test;
import sun.net.ftp.FtpClient;
import java.util.Vector;
import java.io.*;
public class SunFtpWrapper extends FtpClient {
/** Get the present working directory */
public String pwd() throws IOException {
issueCommand("PWD");
if (isValidResponse()) {
String response = getResponseString().substring(4).trim();
if (response.startsWith("\""))
response = response.substring(1);
if (response.endsWith("\""))
response = response.substring(0, response.length() - 1);
return response;
} else {
return "";
}
}
/** Go up one directory */
public boolean cdup() throws IOException {
issueCommand("CDUP");
return isValidResponse();
}
/** Create a new directory */
public boolean mkdir (String newDir) throws IOException {
issueCommand("MKDIR " + newDir);
return isValidResponse();
}
/** Delete a remote file */
public boolean deleteFile (String fileName) throws IOException {
issueCommand("DELE " + fileName);
return isValidResponse();
}
/** Get the results of the LIST command as a Vector of Strings.
* Because there's no standard format for the results of a LIST
* command, it's hard to tell what resulting data will look like.
* Just be aware that different servers have different ways of
* returning your LIST data. */
public Vector listRaw() throws IOException {
String fileName;
Vector ftpList = new Vector();
BufferedReader reader = new BufferedReader(new InputStreamReader(list()));
while ((fileName = reader.readLine()) != null) {
ftpList.add(fileName);
}
return ftpList;
}
/** Get the response code from the last command that was sent */
public int getResponseCode() throws NumberFormatException {
return Integer.parseInt(getResponseString().substring(0, 3));
}
/** Return true if the last response code was in the 200 range,
* false otherwise */
public boolean isValidResponse() {
try {
int respCode = getResponseCode();
return (respCode >= 200 && respCode < 300);
} catch (Exception e) {
return false;
}
}
/** Send a raw FTP command to the server. You can get the response
* by calling getResponseString (which returns the entire response as a
* single String) or getResponseStrings (which returns the response
* as a Vector). */
public int issueRawCommand (String command) throws IOException {
return issueCommand(command);
}
/** Download a file from the server, and save it to the specified local file */
public boolean downloadFile (String serverFile, String localFile) throws IOException {
int i = 0;
byte[] bytesIn = new byte[1024];
BufferedInputStream in = new BufferedInputStream(get(serverFile));
FileOutputStream out = new FileOutputStream(localFile);
while ((i = in.read(bytesIn)) >= 0) {
out.write(bytesIn, 0, i);
}
out.close();
return true;
}
/** Upload a file to the server */
public boolean uploadFile (String localFile, String serverFile) throws IOException {
int i = 0;
byte[] bytesIn = new byte[1024];
FileInputStream in = new FileInputStream(localFile);
BufferedOutputStream out = new BufferedOutputStream(put(serverFile));
while ((i = in.read(bytesIn)) >= 0) {
out.write(bytesIn, 0, i);
}
in.close();
out.close();
return true;
}
} |
Partager