Java Reference
In-Depth Information
If you select a GIF file for the first file, the data stream that this forms will look something like
Listing 14-
5
. The listing has been edited to remove most of the bytes representing the GIF image file.
Listing 14-5: Edited view of the multipart data stream
multipart/form-data; boundary=---------------------------7d21e01ffec
-----------------------------7d21e01ffec
Content-Disposition: form-data; name="ID"
1
-----------------------------7d21e01ffec
Content-Disposition: form-data; name="submit-file";
filename="C:\Clipart\Test.gif"
Content-Type: image/gif
GIF89a_ _ ñ—......................................_#_6 1¢•6•°U ;
-----------------------------7d21e01ffec--
One way to parse a data stream in multipart MIME format is to use the JavaMail API. However, a
simpler approach is to parse the data stream yourself. This approach will be demonstrated by
developing a
BlobUploadServlet
illustrates the basics of parsing a multipart MIME document.
The MIME parts are separated by boundaries, which are unique lines of text defined in the header and
guaranteed not to occur inside any MIME part. Each MIME part is made of a header section, a blank
line, and the body or payload.
The header section contains several headers defining the content and format of the body area. Headers
have a colon separated name/value pair and, optionally, several parameters separated by semicolons.
The parameters are similar to HTML attributes, with a name = value pair.
The MIME boundary is specified in the
Content-Type
header. In the Blob upload servlet, the
getBoundary()
method parses out the boundary substring, prepends
CRLF
and two hyphens, and
returns the boundary as a String. This
read()
method, which is used to retrieve the payload Blob, uses
this boundary string.
The
read()
method creates a
PushbackInputStream
from the
ServletInputStream
and returns
input characters from the stream. If it encounters a boundary, it discards it, returning a flag to indicate
that a boundary has been reached. Since all normal characters are positive integers, a -1 is returned
when a boundary is encountered (unless it is the final boundary, in which case a -2 is returned).
The header area of each part, which, as you recall, corresponds to a field in the HTML form, contains a
Content-Disposition
header, with the value "form-data". This
Content-Disposition
header
contains the attribute "name" with the name of the field specified in the HTML form as its value. If the
field type is "file", the header will also contain the attribute "filename", with the name of the file being
uploaded.
The headers are parsed by the
parseHeader()
method, which returns a
Hashtable
of header
parameters. These are merged into the parameter
Hashtable
, since parameters such as member id
are in a different header from the file name.
Th
BlobUploadServlet
has been written to output header information to the
ServletOutputStream
, so you can see the results of parsing the
ServletInputStream
.
Listing
14-6
shows the servlet output.
Listing 14-6: Ouput of the BlobUploadServlet