Java Reference
In-Depth Information
this.transmit(out, 200, "OK", buffer, getContent(physicalPath));
}
As can be seen from the above lines of code, the file is read into an array of bytes. Once
the file has been read, the transmit method is called. The transmit method actually
transmits the data to the web browser.
If the file can not be found, an error is sent to the web browser.
// File does not exist, so send file not found.
else
this.error(out, 404, "File Not Found");
Notice the last parameter sent to the transmit method. It is the content type. This
tells the web browser what type of data the file contains. The next section explains how this
is determined.
The Get Content Function
Since the transmit method needs to know what type of data is being transferred, the
getContent function should be called to determine the content type. The content type
will be a string such as image/gif for a GIF image or text/html for an HTML file.
This type is determined by the file extension, as shown in the following lines of code:
path = path.toLowerCase();
if (path.endsWith(".jpg") || path.endsWith(".jpeg"))
return "image/jpeg";
else if (path.endsWith(".gif"))
return "image/gif";
else if (path.endsWith(".png"))
return "image/png";
else
return "text/html";
The getContent function can be called to quickly determine the content type based
on the filename. Content types themselves will be discussed in greater detail in Chapter 4,
“Beyond Simple Requests.”
The Error Method
When an error occurs, the error method is called. The error method accepts three
arguments:
• The output stream
• The error code
• The error message
The error method works by constructing an HTML page that displays the error. This
code can be seen here:
Search WWH ::




Custom Search