Package org.tinystruct.http.servlet
Class MultipartFormData
java.lang.Object
org.tinystruct.http.servlet.MultipartFormData
The MultipartFormData class is responsible for reading the
input data of a multipart request and splitting it up into
input elements, wrapped inside a
ContentDisposition
for easy definition. To use this class, create a new instance
of MultipartFormData passing it a HttpServletRequest in the
constructor. Then use the getNextPart
method until it returns null, then you're finished. Example:
MultipartFormData iterator = new MultipartFormData(request);
ContentDisposition element;
while ((element = iterator.getNextPart()) != null) {
//do something with element
}
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected StringThe boundary for this multipart requestprotected intThe amount of data read from a request at a time.protected booleanWhether the input stream is finishedprotected jakarta.servlet.ServletInputStreamThe input stream instance for this classprotected final org.tinystruct.http.Request<jakarta.servlet.http.HttpServletRequest, jakarta.servlet.ServletInputStream> The request instance for this class -
Constructor Summary
ConstructorsConstructorDescriptionMultipartFormData(org.tinystruct.http.Request<jakarta.servlet.http.HttpServletRequest, jakarta.servlet.ServletInputStream> request) -
Method Summary
Modifier and TypeMethodDescriptionintGet the maximum amount of bytes read from a line at one timeorg.tinystruct.transfer.http.upload.ContentDispositionRetrieves the next element in the iterator if one exists.static StringparseBoundary(String contentType) Parses a content-type String for the boundary.static StringparseContentType(String contentTypeString) Parses the "Content-Type" line of a multipart form for a content typestatic StringparseDispositionFilename(String dispositionString) Retrieves the "filename" attribute from a content disposition linestatic StringparseDispositionName(String dispositionString) Retrieves the "name" attribute from a content disposition linestatic StringparseForAttribute(String attribute, String parseString) Parses a string looking for an attribute-value pair, and returns the value.protected voidHandles retrieving the boundary and setting the input streamprotected StringreadLine()Reads the input stream until it reaches a new linevoidsetBufferSize(int bufferSize) Set the maximum amount of bytes read from a line at one time
-
Field Details
-
request
protected final org.tinystruct.http.Request<jakarta.servlet.http.HttpServletRequest,jakarta.servlet.ServletInputStream> requestThe request instance for this class -
inputStream
protected jakarta.servlet.ServletInputStream inputStreamThe input stream instance for this class -
boundary
The boundary for this multipart request -
end
protected boolean endWhether the input stream is finished -
bufferSize
protected int bufferSizeThe amount of data read from a request at a time. This also represents the maximum size in bytes of a line read from the request which defaults to 4 * 1024 (4 KB)
-
-
Constructor Details
-
MultipartFormData
public MultipartFormData(org.tinystruct.http.Request<jakarta.servlet.http.HttpServletRequest, jakarta.servlet.ServletInputStream> request) throws jakarta.servlet.ServletException- Throws:
jakarta.servlet.ServletException
-
-
Method Details
-
parseBoundary
Parses a content-type String for the boundary. Appends a "--" to the beginning of the boundary, because that's the real boundary as opposed to the shortened one in the content type.- Parameters:
contentType- content type- Returns:
- boundary
-
parseContentType
Parses the "Content-Type" line of a multipart form for a content type- Parameters:
contentTypeString- A String representing the Content-Type line, with a trailing "\n"- Returns:
- The content type specified, or
nullif one can't be found.
-
parseDispositionName
Retrieves the "name" attribute from a content disposition line- Parameters:
dispositionString- The entire "Content-disposition" string- Returns:
nullif no name could be found, otherwise, returns the name- See Also:
-
parseDispositionFilename
Retrieves the "filename" attribute from a content disposition line- Parameters:
dispositionString- The entire "Content-disposition" string- Returns:
nullif no filename could be found, otherwise, returns the filename- See Also:
-
parseForAttribute
Parses a string looking for an attribute-value pair, and returns the value. For example:String parseString = "Content-Disposition: filename=\"bob\" name=\"jack\""; MultipartFormData.parseForAttribute(parseString, "name");That will return "bob".- Parameters:
attribute- The name of the attribute you're trying to getparseString- The string to retrieve the value from- Returns:
- The value of the attribute, or
nullif none could be found
-
getNextPart
public org.tinystruct.transfer.http.upload.ContentDisposition getNextPart()Retrieves the next element in the iterator if one exists.- Returns:
- a
ContentDispositionrepresenting the next element in the request data
-
getBufferSize
public int getBufferSize()Get the maximum amount of bytes read from a line at one time- Returns:
- buffer size
- See Also:
-
setBufferSize
public void setBufferSize(int bufferSize) Set the maximum amount of bytes read from a line at one time- Parameters:
bufferSize- buffer size- See Also:
-
parseRequest
protected void parseRequest() throws jakarta.servlet.ServletExceptionHandles retrieving the boundary and setting the input stream- Throws:
jakarta.servlet.ServletException- servlet exception
-
readLine
Reads the input stream until it reaches a new line- Returns:
- one line
-