Java Reference
In-Depth Information
...
Continue in run() method in
Worker
...
// Now read the file from the disk and write it to
// the output stream to the client.
try {
// Open a stream to the file.
FileInputStream file
-
in =
new FileInputStream (file
-
name);
// Send the header.
pw
-
client
-
out.print ("HTTP/1.0 200 OK
\
r
\
n");
File file = new File (file
-
name);
Date date
=
new Date (file.lastModified ());
pw
-
client
-
out.print (
"
Date:
"
+ date +
"
\
r
\
n
"
);
pw
-
client
-
out.print (
"
Server: MicroServer 1.0
\
r
\
n
"
);
pw
-
client
-
out.print (
"
Content-length:
"
+
file
-
in.available () +
"
\
r
\
n
"
);
pw
-
client
-
out.print (
"
Content-type:
"
+
content
-
type +
"
\
r
\
n
\
r
\
n
"
);
// Create a byte array to hold the file.
byte [] data = new byte [file
-
in.available ()];
file
-
in.read (data); // Read file into byte array
client
-
out.write (data);// Write it to client stream
client
-
out.flush ();
// Flush output buffer
file
-
in.close ();
// Close file input stream
} catch (FileNotFoundException e) {
// If no such file, then send the famous 404
message. pw
-
client
-
out.println ("404 Object Not
Found");
}
} else {
pw
-
client
-
out.println ("400 Bad Request");
}
}
catch (IOException e)
{
System.out.println (
"
I/O error
"
+ e);
}
// Close client socket.
try
{
fClient.close ();
}
catch (IOException e)
{
System.out.println ("I/O error " + e);
}
// On return from run () the thread process dies.
} // run
Search WWH ::
Custom Search