Java Reference
In-Depth Information
205.160.186.76 unknown - [17/Jun/2013:22:53:58 -0500]
"GET /bgs/greenbg.gif HTTP 1.0" 200 50
This line indicates that a web browser at IP address 205.160.186.76 requested the
file /bgs/greenbg.gif from this web server at 11:53 P.M (and 58 seconds) on June 17, 2013.
The file was found (response code 200) and 50 bytes of data were successfully transferred
to the browser.
The first field is the IP address or, if DNS resolution is turned on, the hostname from
which the connection was made. This is followed by a space. Therefore, for our purposes,
parsing the logfile is easy: everything before the first space is the IP address, and ev‐
erything after it does not need to be changed.
The dotted quad format IP address is converted into a hostname using the usual methods
of java.net.InetAddress . Example 4-10 shows the code.
Example 4-10. Process web server logfiles
import java.io.* ;
import java.net.* ;
public class Weblog {
public static void main ( String [] args ) {
try ( FileInputStream fin = new FileInputStream ( args [ 0 ]);
Reader in = new InputStreamReader ( fin );
BufferedReader bin = new BufferedReader ( in );) {
for ( String entry = bin . readLine ();
entry != null ;
entry = bin . readLine ()) {
// separate out the IP address
int index = entry . indexOf ( ' ' );
String ip = entry . substring ( 0 , index );
String theRest = entry . substring ( index );
// Ask DNS for the hostname and print it out
try {
InetAddress address = InetAddress . getByName ( ip );
System . out . println ( address . getHostName () + theRest );
} catch ( UnknownHostException ex ) {
System . err . println ( entry );
}
}
} catch ( IOException ex ) {
System . out . println ( "Exception: " + ex );
}
}
}
Search WWH ::




Custom Search