Java Reference
In-Depth Information
boundary =
-----------------------------7d2104226b0
Content-Disposition: form-data; name="ID"
Content-Disposition: form-data; name="submit-file"; filename="C:\JDBC
Bible\Projects\Ch14\bather.jpg"
ID = 101
filename = C:\JDBC Bible\Projects\Ch14\bather.jpg
name = submit-file
Content-Disposition = form-data
Content-Type = image/pjpeg
...saving payload
The servlet is designed specifically to handle Blob uploads, but it can obviously be
modified to handle Clobs with minimal effort. You can use the
Content-Type
parameter to determine the uploaded file type and select the appropriate JDBC
methods when saving the data. If the uploaded file is an image, the
Content-Type
parameter will be
image/pjpeg
or
image/gif
, and so on. Similarly, if you upload a
text file, the
Content-Type
will be set automatically to
text/plain,
and MSWord
documents will have their
Content-Type
set to
application/msword
, and so on.
The method
savePayload()
parses the Blob to a byte array and saves it to the DBMS table in the
method
saveBlob()
. The
saveBlob()
method uses the member id retrieved from a preceding
header and saved in the params
Hashtable
as one of the inputs to the
PreparedStatement
used to
save the Blob to the database table. The Blob upload servlet is shown in
Listing 14-7
.
Listing 14-7: Uploading images using a Blob upload servlet
import java.io.*;
import java.util.*;
import java.sql.*;
import javax.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class BlobUploadServlet extends HttpServlet{
private static String dbUserName = "sa";
private static String dbPassword = "dba";
private static final char CR = 13;
private static final char LF = 10;