|
OpMath | |||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Object | +--com.opmath.util.http.MultiForm
A framework for extraction of data from an HTTP "multipart/form-data" POST operation.
In these scenarios, the browser uploads any text or binary file type to the server. The HTML form should look like:
<FORM METHOD="POST" ACTION="http://sun.local:8080/test/servlet/upload" ENCTYPE="multipart/form-data">
<INPUT TYPE="file" NAME="USERFILE" SIZE="40" MAXLENGTH="128">
</FORM>
<INPUT TYPE="input" NAME="TEXT" SIZE="40" MAXLENGTH="128">
<INPUT TYPE="submit" VALUE="Upload">
NOTE: Because MultiForm is required to stream the data from the HTTP request to the server-side file output, it cannot provide random access to the request parameters. An example of the correct use of MultiForm is as follows:
pMF = new MultiForm(req);
for (int i = 0; i < params.length; i++) {
switch (paramIndex(pMF.getNextParam()))
out.println(uploadDMLDoc.toString(uploadSet));
{
case PARAM_FILE:
}
outStrm = new FileOutputStream(IMG_PATH + pMF.getParamFilename());
pMF.writeParamValue(outStrm);
outStrm.close();
uploadSet.appendTo("imageFile", pMF.getParamFilename());
break;
case PARAM_TEXT:
uploadSet.appendTo("text", pMF.getParamValue());
break;
default:
throw new Exception("unknown parameter: [" + pMF.getParamName() + "]");
}
It is the programer's responsibility to ensure that MultiForm methods are invoked in the appropriate
order. In particular, writeParamValue() should follow the identification of the file name
parameter, and getParamValue() should follow other paramerer names.
NOTE: This code is loosely based on on the techniques described in [Williamson, Alan R., "Java Servlets by Example", Manning, 1999]. However, we would advise against using the multipart code described in that book, since it contains a number of serious bugs.
Copyright: (c) 2002 The Open Math Company Limited
The Terms of Distribution of this software are stipulated in the javadoc Overview for this project.
MultiFormException| Constructor Summary | |
MultiForm(HttpServletRequest req)
Constructs a MultiForm instance. |
|
| Method Summary | |
String |
getNextParam()
Used to identify the next parameter returned by the Web browser in the input stream. |
String |
getParamFilename()
Returns the name of the file which was uploaded via the Web browser. |
String |
getParamName()
Returns the name of the most recent parameter in the input stream - as encountered by getNextParam(). |
String |
getParamValue()
Returns the value of the most recent parameter in the input stream - as encountered by getNextParam(). |
void |
writeParamValue(OutputStream outStrm)
Used to write the uploaded file's contents to a file on the server. |
| Methods inherited from class java.lang.Object |
|
| Constructor Detail |
public MultiForm(HttpServletRequest req)
throws MultiFormException
MultiFormException is thrown if this cannot be done.
req - the javax.servlet.ServletRequest for the input stream.MultiFormException - An exception thrown if
the ServletRequest was ill-formed.| Method Detail |
public String getParamFilename()
NOTE: If the file has not yet been identified by the MultiPart (typically because the file parameter has
not yet been encountered in the stream) then getParamFilename() returns null.
String containing the client's name for the file being uploaded.public String getParamName()
getNextParam().
NOTE: If getNextParam() has not been invoked, then getParamName() returns
null.
String containing the name of the most recently encountered parameter.
public String getParamValue()
throws MultiFormException
getNextParam().
NOTE 1: Involking getParamValue() before getNextParam() will put the
input stream out of sync, and is likely to have disasterous consequences.
NOTE 2: Do not use getParamValue() to access the uploaded file itself. This method is
useful for accessing only the "conventional", single part parameters which may have been sent by
the Web browser.
String containing the value of the most recently encountered parameter.MultiFormException - An exception thrown if
the ServletRequest was ill-formed.
public String getNextParam()
throws MultiFormException
NOTE: getNextParam() has a side-effect of stepping through the input stream.
String containing the name of the most recently encountered parameter.MultiFormException - An exception thrown if
the ServletRequest was ill-formed.
public void writeParamValue(OutputStream outStrm)
throws MultiFormException
outStrm - the OutputStream for the file contents.MultiFormException - An exception thrown if
the ServletRequest was ill-formed.
|
OpMath | |||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||