Java FTP Connection Problem with FileZilla
i have a weird problem with my local Testing-Setup of a simple Java URL FTP Connection. Following code fragments (removed try/catches):
URL url = new URL("ftp://127.0.0.1/subOne/subTwo/subThree/subFour");
URLConnection conn = url.openConnection();
conn.setConnectTimeout(30000);
conn.setReadTimeout(30000);
InputStream is = conn.getInputStream(); /// And here flies the IOException!
... The actual IOException-Cause is "subOne/subTwo/subThree/subFour", but the funny things happen on server side:
(000012)23.02.2011 13:01:05 - (not logged in) (127.0.0.1)> Connected, sending welcome message...
(000012)23.02.2011 13:01:05 - (not logged in) (127.0.0.1)> 220 Blabla
(000012)23.02.2011 13:01:05 - (not logged in) (127.0.0.1)> USER anonymous
(000012)23.02.2011 13:01:05 - (not logged in) (127.0.0.1)> 331 Password required for anonymous
(000012)23.02.2011 13:01:05 - (not logged in) (127.0.0.1)> PASS *************
(000012)23.02.2011 13:01:05 - anonymous (127.0.0.1)> 230 Logged on
(000012)23.02.2011 13:01:05 - anonymous (127.0.0.1)> TYPE I
(000012)23.02.2011 13:01:05 - anonymous (127.0.0.1)> 200 Type set to I
(000012)23.02.2011 13:01:05 - anonymous (127.0.0.1)> CWD das
(000012)23.02.2011 13:01:05 - anonymous (127.0.0.1)> 250 CWD successful. "/subOne" is current directory.
(000012)23.02.2011 13:01:05 - anonymous (127.0.0.1)> CWD 2011
(000012)23.02.2011 13:01:05 - anonymous (127.0.0.1)> 250 CWD successful. "/subOne/subTwo" is current directory.
(000012)23.02.2011 13:01:05 - anonymous (127.0.0.1)> CWD 02
(000012)23.02.2011 13:01:05 - anonymous (127.0.0.1)> 250 CWD su开发者_如何学编程ccessful. "/subOne/subTwo/subThree" is current directory.
(000012)23.02.2011 13:01:05 - anonymous (127.0.0.1)> EPSV ALL
(000012)23.02.2011 13:01:05 - anonymous (127.0.0.1)> 229 Entering Extended Passive Mode (|||3881|)
(000012)23.02.2011 13:01:05 - anonymous (127.0.0.1)> EPSV
(000012)23.02.2011 13:01:05 - anonymous (127.0.0.1)> 229 Entering Extended Passive Mode (|||3882|)
(000012)23.02.2011 13:01:05 - anonymous (127.0.0.1)> RETR subFour
(000012)23.02.2011 13:01:05 - anonymous (127.0.0.1)> 550 File not found
(000012)23.02.2011 13:01:05 - anonymous (127.0.0.1)> CWD subOne
(000012)23.02.2011 13:01:05 - anonymous (127.0.0.1)> 550 CWD failed. "/subOne/subTwo/subThree/subOne": directory not found.
(000012)23.02.2011 13:03:06 - anonymous (127.0.0.1)> 421 Connection timed out.
(000012)23.02.2011 13:03:06 - anonymous (127.0.0.1)> disconnected.
I don't understand at all, why the Tester is trying to get into extended passive mode and why it's added subOne after it can't retrieve subFour.
I just installed the FileZilla Server and set up the anonymous user and shared drives. I checked via browser and FileZilla-Client that the FTP-Dir is reachable (same logins of course) and that's it. Everything is installed and running on the same machine!
Don't know any further...
Thanks for any help!
This way to connect to the FTP is quite limited and obscurely documented. I can give you an answer for the EPSV first. The connection is established by internal implementation which in my JDK happens to be sun.net.www.protocol.ftp.FtpURLConnection
.
When connecting to the server first EPSV
and PASV
will be tried (default is the passive mode) then it will fall back to the active mode - PORT
- if the passive mode cannot be established. You can see the implementation details here.
The essential comment explaining one of your questions is:
/**
* Here is the idea:
*
* - First we want to try the new (and IPv6 compatible) EPSV command
* But since we want to be nice with NAT software, we'll issue the
* EPSV ALL cmd first.
* EPSV is documented in RFC2428
* - If EPSV fails, then we fall back to the older, yet OK PASV command
* - If PASV fails as well, then we throw an exception and the calling method
* will have to try the EPRT or PORT command
*/
As for your second question... the unsuccessful retrieval of subFour... Well, at first quick look it seems to behave that way because it is buggy. But I cannot really install the proper environment to verify that now. Further you have the exception. I guess that the problem is initiated on line 455 when it tries to navigate to the full path again. The full source of the FTP connection is here.
373 public InputStream getInputStream() throws IOException {
...
390 try {
391 decodePath(url.getPath());
392 if (filename == null || type == DIR) {
...
399 } else {
400 if (type == ASCII)
401 ftp.ascii();
402 else
403 ftp.binary();
404 cd(pathname);
405 is = new FtpInputStream(ftp, ftp.get(filename));
406 }
407
408
...
453 } catch (FileNotFoundException e) {
454 try {
455 cd(fullpath);
456 /* if that worked, then make a directory listing
457 and build an html stream with all the files in
458 the directory */
459 ftp.ascii();
460
461 is = new FtpInputStream(ftp, ftp.list());
462 msgh.add("content-type", "text/plain");
463 } catch (IOException ex) {
464 throw new FileNotFoundException(fullpath);
465 }
466 }
...
469 }
I would advise you to use the Apache Commons Net library for FTP operations. It is much more advanced and straightforward to use.
Cheers and happy debugging if you feel like!
精彩评论