public class UploadServlet extends HttpServlet
The specifications seem to say that the values on the headers may be tokens or quoted strings.
It may be necessary to expand this algorithm to handle additional transfer encoding types. The two mentioned on the web are base64 and quoted-printable.
MSDN lists the following types: 7bit, 8bit, binary, base64, quoted-printable, and X-token.
Modifier and Type | Class and Description |
---|---|
class |
UploadServlet.Contents
Contains information on the contents of a single part of a
multipart form.
|
class |
UploadServlet.Formatters
Methods for formatting integers.
|
class |
UploadServlet.ThisPage
Contains information relating to a single HTTP request.
|
Modifier and Type | Field and Description |
---|---|
protected ServletConfig |
config
ServletConfig object for servlet.
|
protected static int |
debugLevel
Used to control amount of debugging output.
|
private static long |
serialVersionUID
Dummy id to satisfy serializable interface
|
Constructor and Description |
---|
UploadServlet() |
Modifier and Type | Method and Description |
---|---|
protected void |
ender(UploadServlet.ThisPage thisPage)
Perform common operations on the contents of the request packet
to be carried out after running the main program.
|
String |
extractBoundary(HttpServletRequest req)
Extracts the value of the boundary string from the header.
|
void |
init(ServletConfig configIn)
Called when object for handling HTTP request is created.
|
void |
loadByteArray(DatabaseProperties data,
PreparedStatement stmt,
UploadServlet.ThisPage thisPage,
int position,
String element)
Utility method for moving the contents of one of the parts into
a prepared statement as a string object.
|
void |
loadByteArray(DatabaseProperties data,
PreparedStatement stmt,
UploadServlet.ThisPage thisPage,
int position,
String element,
boolean convertFlag)
Utility method for moving the contents of one of the parts into
a prepared statement as a string object.
|
void |
loadByteArray(DatabaseProperties data,
PreparedStatement stmt,
UploadServlet.ThisPage thisPage,
int position,
String element,
boolean convertFlag,
int SQLType)
Utility method for moving the contents of one of the parts into
a prepared statement as a string object.
|
protected void |
processor(UploadServlet.ThisPage thisPage)
This method generates the web page based on the contents
of the request packet.
|
protected byte[] |
readPart(ServletInputStream input,
UploadServlet.ThisPage thisPage,
String encoding)
Obtain the byte array for this part of the request.
|
void |
service(HttpServletRequest req,
HttpServletResponse res)
Process the call to the servlet.
|
protected void |
starter(UploadServlet.ThisPage thisPage)
Perform common operations on the contents of the
request packet before running the main program.
|
protected String |
stripEOL(String input)
Strips end of line characters from a character string.
|
doDelete, doGet, doHead, doOptions, doPost, doPut, doTrace, getLastModified, service
destroy, getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, init, log, log
private static final long serialVersionUID
protected static int debugLevel
protected ServletConfig config
public UploadServlet()
public void init(ServletConfig configIn) throws ServletException
init
in interface Servlet
init
in class GenericServlet
ServletException
public void service(HttpServletRequest req, HttpServletResponse res) throws IOException
With regard to reading data from the request, a
ServletInputStream
object for binary data
can be obtained by ServletRequest.getInputStream()
or a BufferedReader
can be obtained for character data
by using
ServletRequest.getReader()
.
With regard to writing data to the response a
ServletOutputStream
object for binary
data can be obtained using ServletResponse.getOutputStream()
while a PrintWriter
object can be obtained using
ServletResponse.getWriter()
.
The ByteArrayOutputStream
class can be used as a means of collecting
the bytes contained in the attached file.
It would be desirable to have tests for enctype and method.
service
in class HttpServlet
req
- Request objectres
- Response objectIOException
protected byte[] readPart(ServletInputStream input, UploadServlet.ThisPage thisPage, String encoding) throws IOException
It may be necessary to modify this code to handle additional encoding types such as Base64.
input
- Object from which data is readthisPage
- Object containing information for this requestIOException
protected String stripEOL(String input)
input
- String to be processedpublic String extractBoundary(HttpServletRequest req)
req
- Request objectpublic void loadByteArray(DatabaseProperties data, PreparedStatement stmt, UploadServlet.ThisPage thisPage, int position, String element, boolean convertFlag, int SQLType) throws SQLException
data
- Data connection object.stmt
- Prepared statementthisPage
- Object representing this requestposition
- Position of the value in the prepared statement's argument listelement
- Name of the part in the multi-part formconvertFlag
- True means that value should be converted to upper caseSQLType
- Type of SQL column (Types.CHAR or Types.VARCHAR)SQLException
public void loadByteArray(DatabaseProperties data, PreparedStatement stmt, UploadServlet.ThisPage thisPage, int position, String element, boolean convertFlag) throws SQLException
data
- Data connection object.stmt
- Prepared statementthisPage
- Object representing this requestposition
- Position of the value in the prepared statement's argument listelement
- Name of the part in the multi-part formconvertFlag
- True means that value should be converted to upper caseSQLException
public void loadByteArray(DatabaseProperties data, PreparedStatement stmt, UploadServlet.ThisPage thisPage, int position, String element) throws SQLException
data
- Data connection object.stmt
- Prepared statementthisPage
- Object representing this requestposition
- Position of the value in the prepared statement's argument listelement
- Name of the part in the multi-part formSQLException
protected void starter(UploadServlet.ThisPage thisPage) throws IOException
The intent is to override this method in a subclass that will then be subclassed to handle the servlets for the various web pages.
thisPage
- Information on this HTTP requestIOException
protected void ender(UploadServlet.ThisPage thisPage) throws IOException
thisPage
- Information on this HTTP transactionIOException
protected void processor(UploadServlet.ThisPage thisPage) throws IOException
This method will be overwritten to enable various actions to take place as the information is uploaded.
For the result of File requests, write the contents to a file.
I am having a problem with the loading of binary files.
thisPage
- Information on this HTTP requestIOException