Package anderix.net
Class MultiPartFormOutputStream
java.lang.Object
anderix.net.MultiPartFormOutputStream
MultiPartFormOutputStream is used to write
"multipart/form-data" to a java.net.URLConnection for
POSTing. This is primarily for file uploading to HTTP servers.- Since:
- JDK1.3
-
Constructor Summary
ConstructorsConstructorDescriptionMultiPartFormOutputStream(OutputStream os, String boundary) Creates a newMultiPartFormOutputStreamobject using the specified output stream and boundary. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes the stream.static StringCreates a multipart boundary string by concatenating 20 hyphens (-) and the hexadecimal (base-16) representation of the current time in milliseconds.static URLConnectioncreateConnection(URL url) Creates a newjava.net.URLConnectionobject from the specifiedjava.net.URL.voidflush()Flushes the stream.Gets the multipart boundary string being used by this stream.static StringgetContentType(String boundary) Gets the content type string suitable for thejava.net.URLConnectionwhich includes the multipart boundary string.voidwriteField(String name, boolean value) Writes an boolean field value.voidwriteField(String name, char value) Writes an char field value.voidwriteField(String name, double value) Writes an double field value.voidwriteField(String name, float value) Writes an float field value.voidwriteField(String name, int value) Writes an int field value.voidwriteField(String name, long value) Writes an long field value.voidwriteField(String name, short value) Writes an short field value.voidwriteField(String name, String value) Writes an string field value.voidWrites a file's contents.voidWrites the given bytes.voidwriteFile(String name, String mimeType, String fileName, InputStream is) Writes a input stream's contents.
-
Constructor Details
-
MultiPartFormOutputStream
Creates a newMultiPartFormOutputStreamobject using the specified output stream and boundary. The boundary is required to be created before using this method, as described in the description for thegetContentType(String)method. The boundary is only checked fornullor empty string, but it is recommended to be at least 6 characters. (Or use the static createBoundary() method to create one.)- Parameters:
os- the output streamboundary- the boundary- See Also:
-
-
Method Details
-
writeField
Writes an boolean field value.- Parameters:
name- the field name (required)value- the field value- Throws:
IOException- on input/output errors
-
writeField
Writes an double field value.- Parameters:
name- the field name (required)value- the field value- Throws:
IOException- on input/output errors
-
writeField
Writes an float field value.- Parameters:
name- the field name (required)value- the field value- Throws:
IOException- on input/output errors
-
writeField
Writes an long field value.- Parameters:
name- the field name (required)value- the field value- Throws:
IOException- on input/output errors
-
writeField
Writes an int field value.- Parameters:
name- the field name (required)value- the field value- Throws:
IOException- on input/output errors
-
writeField
Writes an short field value.- Parameters:
name- the field name (required)value- the field value- Throws:
IOException- on input/output errors
-
writeField
Writes an char field value.- Parameters:
name- the field name (required)value- the field value- Throws:
IOException- on input/output errors
-
writeField
Writes an string field value. If the value is null, an empty string is sent ("").- Parameters:
name- the field name (required)value- the field value- Throws:
IOException- on input/output errors
-
writeFile
Writes a file's contents. If the file is null, does not exists, or is a directory, ajava.lang.IllegalArgumentExceptionwill be thrown.- Parameters:
name- the field namemimeType- the file content type (optional, recommended)file- the file (the file must exist)- Throws:
IOException- on input/output errors
-
writeFile
public void writeFile(String name, String mimeType, String fileName, InputStream is) throws IOException Writes a input stream's contents. If the input stream is null, ajava.lang.IllegalArgumentExceptionwill be thrown.- Parameters:
name- the field namemimeType- the file content type (optional, recommended)fileName- the file name (required)is- the input stream- Throws:
IOException- on input/output errors
-
writeFile
public void writeFile(String name, String mimeType, String fileName, byte[] data) throws IOException Writes the given bytes. The bytes are assumed to be the contents of a file, and will be sent as such. If the data is null, ajava.lang.IllegalArgumentExceptionwill be thrown.- Parameters:
name- the field namemimeType- the file content type (optional, recommended)fileName- the file name (required)data- the file data- Throws:
IOException- on input/output errors
-
flush
Flushes the stream. Actually, this method does nothing, as the only write methods are highly specialized and automatically flush.- Throws:
IOException- on input/output errors
-
close
Closes the stream.
NOTE: This method MUST be called to finalize the multipart stream.- Throws:
IOException- on input/output errors
-
getBoundary
Gets the multipart boundary string being used by this stream.- Returns:
- the boundary
-
createConnection
Creates a newjava.net.URLConnectionobject from the specifiedjava.net.URL. This is a convenience method which will set thedoInput,doOutput,useCachesanddefaultUseCachesfields to the appropriate settings in the correct order.- Parameters:
url- the URL- Returns:
- a
java.net.URLConnectionobject for the URL - Throws:
IOException- on input/output errors
-
createBoundary
Creates a multipart boundary string by concatenating 20 hyphens (-) and the hexadecimal (base-16) representation of the current time in milliseconds.- Returns:
- a multipart boundary string
- See Also:
-
getContentType
Gets the content type string suitable for thejava.net.URLConnectionwhich includes the multipart boundary string.
This method is static because, due to the nature of thejava.net.URLConnectionclass, once the output stream for the connection is acquired, it's too late to set the content type (or any other request parameter). So one has to create a multipart boundary string first before using this class, such as with thecreateBoundary()method.- Parameters:
boundary- the boundary string- Returns:
- the content type string
- See Also:
-