-
Bug
-
Resolution: Duplicate
-
P2
-
None
-
1.3.0
-
sparc
-
solaris_2.6
Name: dfC67450 Date: 03/13/2000
It's impossible to establish ftp connection to desirable port on host
using java.net.URLConnection class. It always try to connect to
default ftp port (21) on host, ignoring port specified in the given url.
After doing:
URL url = new URL("ftp://hostName:12345/fileName");
URLConnection conn = url.openConnection();
conn.connect();
conn will try to connect to port# 21, not 12345 on hostName.
This behaviour is undocumented and seeming incorrect.
Here is the test demonstrating the bug:
---------------------------------------------
import java.net.*;
import java.io.*;
public class Test {
public static void main (String args[]){
Server server = null;
try {
server = new Server();
server.start();
String protocol = "ftp";
URL url = new URL(protocol + "://" + server.getHost() + ":"
+ (server.getPort()) + "/test");
System.out.println("URL: " + url);
URLConnection conn = url.openConnection();
conn.connect();
} catch (IOException e) {
System.out.println("Unexpected: ");
e.printStackTrace(System.out);
}
server.interrupt();
}
}
class Server extends Thread {
ServerSocket server = null;
Server() throws IOException {
InetAddress localHost = InetAddress.getLocalHost();
server = new ServerSocket(0, 1, localHost);
}
int getPort() {
return server.getLocalPort();
}
String getHost() {
return server.getInetAddress().getHostName();
}
public void run() {
try {
System.out.println("Server started. " + getHost() + ":" + getPort());
Socket soc = server.accept();
System.out.println("Connection established!!!");
System.out.println("Test passed");
server.close();
soc.close();
} catch (InterruptedIOException e) {
System.out.println("Test failed: No connection established");
} catch (IOException e) {
System.out.println("Unexpected: " + e);
}
}
}
----------------------------------------------------------
The aim of the test to detect whether or not URLConnection object
connect to desirable port. Server listens for incomming connection
and if anybody connects the test will pass. If none, the test
will fail.
In JDK this test will fail with ftp protocol:
------------- output (with ftp)---------
Server started. helmet:61713
URL: ftp://helmet:61713/test
Unexpected:
sun.net.ftp.FtpLoginException: user
at sun.net.ftp.FtpClient.login(FtpClient.java:286)
at sun.net.www.protocol.ftp.FtpURLConnection.connect(FtpURLConnection.java:91)
at Test.main(Test.java:18)
Test failed: No connection established
-----------------------------------------
and will pass with with http protocol:
------------- output (with http)---------
Server started. helmet:61726
URL: http://helmet:61726/test
Connection established!!!
Test passed
-----------------------------------------
Source for sun.www.protocol.ftp.FtpURLConnection.connect():
public synchronized void connect() throws IOException {
...
try {
ftp = new FtpClient(host);
^^^^^^ - only host parameter passed
} catch (UnknownHostException e) {
// Maybe do something smart here, like use a proxy like iftp.
// Just keep throwing for now.
throw e;
}
...
}
This bug can be fixed easily, because of FtpClient class already has
constructor for both parameters host and port.
======================================================================
- duplicates
-
JDK-4192854 FTP Content Handler suggestion....
- Resolved