43 |
public class FTPURLConnection extends URLConnection |
public class FTPURLConnection extends URLConnection |
44 |
{ |
{ |
45 |
|
|
46 |
/** |
/** |
47 |
* The connection managing the protocol exchange. |
* The connection managing the protocol exchange. |
48 |
*/ |
*/ |
49 |
protected FTPConnection connection; |
protected FTPConnection connection; |
50 |
|
|
51 |
/** |
/** |
52 |
* Constructs an FTP connection to the specified URL. |
* Constructs an FTP connection to the specified URL. |
53 |
* @param url the URL |
* @param url the URL |
54 |
*/ |
*/ |
55 |
public FTPURLConnection(URL url) |
public FTPURLConnection(URL url) |
56 |
{ |
{ |
57 |
super(url); |
super(url); |
58 |
} |
} |
59 |
|
|
60 |
/** |
/** |
61 |
* Establishes the connection. |
* Establishes the connection. |
62 |
*/ |
*/ |
63 |
public void connect() |
public void connect() throws IOException |
64 |
throws IOException |
{ |
65 |
{ |
if (connected) |
66 |
if (connected) |
return; |
67 |
return; |
String host = url.getHost(); |
68 |
String host = url.getHost(); |
int port = url.getPort(); |
69 |
int port = url.getPort(); |
String username = url.getUserInfo(); |
70 |
String username = url.getUserInfo(); |
String password = null; |
71 |
String password = null; |
if (username != null) |
72 |
if (username!=null) |
{ |
73 |
{ |
int ci = username.indexOf(':'); |
74 |
int ci = username.indexOf(':'); |
if (ci != -1) |
75 |
if (ci!=-1) |
{ |
76 |
{ |
password = username.substring(ci + 1); |
77 |
password = username.substring(ci+1); |
username = username.substring(0, ci); |
78 |
username = username.substring(0, ci); |
} |
79 |
} |
} |
80 |
} |
connection = new FTPConnection(host, port); |
81 |
connection = new FTPConnection(host, port); |
if (username != null && !connection.authenticate(username, password)) |
82 |
if (username!=null && !connection.authenticate(username, password)) |
throw new SecurityException("Authentication failed"); |
83 |
throw new SecurityException("Authentication failed"); |
} |
|
} |
|
84 |
|
|
85 |
/** |
/** |
86 |
* This connection supports doInput. |
* This connection supports doInput. |
87 |
*/ |
*/ |
88 |
public void setDoInput(boolean doinput) |
public void setDoInput(boolean doinput) |
89 |
{ |
{ |
90 |
doInput = doinput; |
doInput = doinput; |
91 |
} |
} |
92 |
|
|
93 |
/** |
/** |
94 |
* This connection supports doOutput. |
* This connection supports doOutput. |
95 |
*/ |
*/ |
96 |
public void setDoOutput(boolean dooutput) |
public void setDoOutput(boolean dooutput) |
97 |
{ |
{ |
98 |
doOutput = dooutput; |
doOutput = dooutput; |
99 |
} |
} |
100 |
|
|
101 |
/** |
/** |
102 |
* Returns an input stream that reads from this open connection. |
* Returns an input stream that reads from this open connection. |
103 |
*/ |
*/ |
104 |
public InputStream getInputStream() |
public InputStream getInputStream() throws IOException |
105 |
throws IOException |
{ |
106 |
{ |
String dir = url.getPath(); |
107 |
String dir = url.getPath(); |
String filename = url.getFile(); |
108 |
String filename = url.getFile(); |
connection.setRepresentationType(FTPConnection.TYPE_BINARY); |
109 |
connection.setRepresentationType(FTPConnection.TYPE_BINARY); |
connection.setPassive(true); |
110 |
connection.setPassive(true); |
if (!connection.changeWorkingDirectory(dir)) |
111 |
if (!connection.changeWorkingDirectory(dir)) |
throw new FileNotFoundException(dir); |
112 |
throw new FileNotFoundException(dir); |
if (filename != null) |
113 |
if (filename!=null) |
return connection.retrieve(filename); |
114 |
return connection.retrieve(filename); |
else |
115 |
else |
return connection.list(null); |
116 |
return connection.list(null); |
// TODO provide a means to close the connection |
117 |
// TODO provide a means to close the connection |
} |
|
} |
|
118 |
|
|
119 |
/** |
/** |
120 |
* Returns an output stream that writes to this connection. |
* Returns an output stream that writes to this connection. |
121 |
*/ |
*/ |
122 |
public OutputStream getOutputStream() |
public OutputStream getOutputStream() throws IOException |
123 |
throws IOException |
{ |
124 |
{ |
String dir = url.getPath(); |
125 |
String dir = url.getPath(); |
String filename = url.getFile(); |
126 |
String filename = url.getFile(); |
connection.setRepresentationType(FTPConnection.TYPE_BINARY); |
127 |
connection.setRepresentationType(FTPConnection.TYPE_BINARY); |
connection.setPassive(true); |
128 |
connection.setPassive(true); |
if (!connection.changeWorkingDirectory(dir)) |
129 |
if (!connection.changeWorkingDirectory(dir)) |
throw new FileNotFoundException(dir); |
130 |
throw new FileNotFoundException(dir); |
if (filename != null) |
131 |
if (filename!=null) |
return connection.store(filename); |
132 |
return connection.store(filename); |
else |
133 |
else |
throw new FileNotFoundException(filename); |
134 |
throw new FileNotFoundException(filename); |
// TODO provide a means to close the connection |
135 |
// TODO provide a means to close the connection |
} |
136 |
} |
|
137 |
|
// TODO allow user to configure the FTPConnection using setRequestProperty |
138 |
|
|
|
// TODO allow user to configure the FTPConnection using setRequestProperty |
|
|
|
|
139 |
} |
} |