This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes I/O and Streams and the fly likes Reading Remote File Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Reading Remote File" Watch "Reading Remote File" New topic
Author

Reading Remote File

Mike Cutter
Ranch Hand

Joined: Jun 09, 2002
Posts: 49
I am trying to read the contents of a file from a remote system. I was told to try to use the URL class through FTP, but I am not sure if this is the best way.

import java.io.*;
import java.net.*;

public class RemoteFile {
public static void main(String args[]) {
URL url;
URLConnection connection;
String urlString = "ftp://username:password:21@host.domain.com/file";
String matchString = "name";
String inputLine;
BufferedReader inStream;

try {
url = new URL(urlString);
} catch (MalformedURLException e) {
System.err.println("Exception: " + e);
return;
}
try {
connection = url.openConnection();
inStream = new BufferedReader (new InpustStreamReader(connection.getInputStream()));
while (null != (inputLine = inStream.readLine())) {
if (inputLine.indexOf(matchString) >= 0) {
System.out.println(inputLine);
}
}
inStream.close();
} catch (IOException e) {
System.err.println("Exception: " + e);
}
}
}

# java RemoteFile
Exception: sun.net.ftp.FtpProtocolException: PORT :

When I execute this program, I keep getting an exception error about the port even though I specified port 21 in the URL. Any suggestions would be helpful on how to resolve this or a better approach to read the remote file.

Thanks,

Mike
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8876
    
    8

I think you are specifying the port of your password. Try:
"ftp://username assword@host.domain.com:21/file";


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Mike Cutter
Ranch Hand

Joined: Jun 09, 2002
Posts: 49
I designated port 21 after the hostname like you said and even did it without any port designation.

I still get the FTPProtocolException error for the PORT.
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8876
    
    8

You didn't give us the whole exception:



That error has nothing to do with the FTP port (which is 21 by default, so you don't have to specify it). It has to do with the order in which commands are sent in the FTP protocol. I get that error when I hit my ISP's FTP server, which is Free BSD running WUFTP, but I don't get it when i hit ftp://cwrulug.cwru.edu, OS unknown, likely Linux. What OS/software combo are you using?
You may want to look at Jakarta Commons Net Utilities. They have a more robust and open implementation of FTP.
Mike Cutter
Ranch Hand

Joined: Jun 09, 2002
Posts: 49
I am running the RemoteFile.java program from HP-UX 11.0 and HP-UX 11.11 systems trying to FTP to a HP-UX 11.11 system just to read the contents of a file.
Mike Cutter
Ranch Hand

Joined: Jun 09, 2002
Posts: 49
The system I have been getting this PORT message is using WU-FTP. When I changed to URL to another system, the RemoteFile Java program was able to read the file. Thanks for pointing that out.

What is the difference in the URL Class syntax to connect when using WU-FTP?
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8876
    
    8

Originally posted by Mike Cutter:

What is the difference in the URL Class syntax to connect when using WU-FTP?


The URL class follows the format of a URL as specified by RFC 1738.
The problem is not with the URL class syntax. It could be a couple of things. First, WUFTP doesn't support IPv6 sockets which Java uses by default. I tried using IPv4 by setting the java.net.preferIPv4Stack system property, but that didn't work.
It could be that WUFTP has a problem with the EPSV site verb, since it's throwing an error code of 500 (unacceptable syntax).
In any case, I recommend trying the Apache Commons Net library rather than trying to decypher what Sun's doing in its FTP client.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reading Remote File