public class

FtpURLConnection

extends URLConnection
java.lang.Object
   ↳ java.net.URLConnection
     ↳ sun.net.www.URLConnection
       ↳ sun.net.www.protocol.ftp.FtpURLConnection

Class Overview

This class Opens an FTP input (or output) stream given a URL. It works as a one shot FTP transfer :

  • Login
  • Get (or Put) the file
  • Disconnect
You should not have to use it directly in most cases because all will be handled in a abstract layer. Here is an example of how to use the class :

URL url = new URL("ftp://ftp.sun.com/pub/test.txt");

UrlConnection con = url.openConnection();

InputStream is = con.getInputStream();

...

is.close();

See Also

Summary

Nested Classes
class FtpURLConnection.FtpInputStream For FTP URLs we need to have a special InputStream because we need to close 2 sockets after we're done with it : - The Data socket (for the file). 
class FtpURLConnection.FtpOutputStream For FTP URLs we need to have a special OutputStream because we need to close 2 sockets after we're done with it : - The Data socket (for the file). 
[Expand]
Inherited Fields
From class sun.net.www.URLConnection
From class java.net.URLConnection
Public Constructors
FtpURLConnection(URL url)
Creates an FtpURLConnection from a URL.
Public Methods
synchronized void connect()
Connects to the FTP server and logs in.
int getConnectTimeout()
Returns setting for connect timeout.
InputStream getInputStream()
Get the InputStream to retreive the remote file.
OutputStream getOutputStream()
Get the OutputStream to store the remote file.
Permission getPermission()
Gets the Permission associated with the host & port.
int getReadTimeout()
Returns setting for read timeout.
String getRequestProperty(String key)
Returns the value of the named general request property for this connection.
void setConnectTimeout(int timeout)
Sets a specified timeout value, in milliseconds, to be used when opening a communications link to the resource referenced by this URLConnection.
void setReadTimeout(int timeout)
Sets the read timeout to a specified timeout, in milliseconds.
void setRequestProperty(String key, String value)
Sets the general request property.
[Expand]
Inherited Methods
From class sun.net.www.URLConnection
From class java.net.URLConnection
From class java.lang.Object

Public Constructors

public FtpURLConnection (URL url)

Creates an FtpURLConnection from a URL.

Parameters
url The URL to retrieve or store.

Public Methods

public synchronized void connect ()

Connects to the FTP server and logs in.

Throws
if the login is unsuccessful
FtpProtocolException if an error occurs
UnknownHostException if trying to connect to an unknown host
IOException

public int getConnectTimeout ()

Returns setting for connect timeout.

0 return implies that the option is disabled (i.e., timeout of infinity).

Returns
  • an int that indicates the connect timeout value in milliseconds

public InputStream getInputStream ()

Get the InputStream to retreive the remote file. It will issue the "get" (or "dir") command to the ftp server.

Returns
  • the InputStream to the connection.
Throws
IOException if already opened for output
FtpProtocolException if errors occur during the transfert.

public OutputStream getOutputStream ()

Get the OutputStream to store the remote file. It will issue the "put" command to the ftp server.

Returns
  • the OutputStream to the connection.
Throws
IOException if already opened for input or the URL points to a directory
FtpProtocolException if errors occur during the transfert.

public Permission getPermission ()

Gets the Permission associated with the host & port.

Returns
  • The Permission object.

public int getReadTimeout ()

Returns setting for read timeout. 0 return implies that the option is disabled (i.e., timeout of infinity).

Returns
  • an int that indicates the read timeout value in milliseconds

public String getRequestProperty (String key)

Returns the value of the named general request property for this connection.

Parameters
key the keyword by which the request is known (e.g., "accept").
Returns
  • the value of the named general request property for this connection.
Throws
IllegalStateException if already connected

public void setConnectTimeout (int timeout)

Sets a specified timeout value, in milliseconds, to be used when opening a communications link to the resource referenced by this URLConnection. If the timeout expires before the connection can be established, a java.net.SocketTimeoutException is raised. A timeout of zero is interpreted as an infinite timeout.

Some non-standard implmentation of this method may ignore the specified timeout. To see the connect timeout set, please call getConnectTimeout().

Parameters
timeout an int that specifies the connect timeout value in milliseconds

public void setReadTimeout (int timeout)

Sets the read timeout to a specified timeout, in milliseconds. A non-zero value specifies the timeout when reading from Input stream when a connection is established to a resource. If the timeout expires before there is data available for read, a java.net.SocketTimeoutException is raised. A timeout of zero is interpreted as an infinite timeout.

Some non-standard implementation of this method ignores the specified timeout. To see the read timeout set, please call getReadTimeout().

Parameters
timeout an int that specifies the timeout value to be used in milliseconds

public void setRequestProperty (String key, String value)

Sets the general request property. If a property with the key already exists, overwrite its value with the new value.

Parameters
key the keyword by which the request is known (e.g., "accept").
value the value associated with it.
Throws
IllegalStateException if already connected