|
Version: 1.1.1 | |||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectca.ucalgary.services.util.MultiPartFormOutputStream
public class 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.
Code taken from Useful Code of the Day:
http://forums.sun.com/thread.jspa?threadID=451245&forumID=31
| Constructor Summary | |
|---|---|
MultiPartFormOutputStream(OutputStream os,
String boundary)
Creates a new MultiPartFormOutputStream object using
the specified output stream and boundary. |
|
| Method Summary | |
|---|---|
void |
close()
Closes the stream. |
static String |
createBoundary()
Creates a multipart boundary string by concatenating 20 hyphens (-) and the hexadecimal (base-16) representation of the current time in milliseconds. |
static URLConnection |
createConnection(URL url)
Creates a new java.net.URLConnection object from the
specified java.net.URL. |
void |
flush()
Flushes the stream. |
String |
getBoundary()
Gets the multipart boundary string being used by this stream. |
static String |
getContentType(String boundary)
Gets the content type string suitable for the java.net.URLConnection which includes the multipart
boundary string. |
void |
writeField(String name,
boolean value)
Writes an boolean field value. |
void |
writeField(String name,
char value)
Writes an char field value. |
void |
writeField(String name,
double value)
Writes an double field value. |
void |
writeField(String name,
float value)
Writes an float field value. |
void |
writeField(String name,
int value)
Writes an int field value. |
void |
writeField(String name,
long value)
Writes an long field value. |
void |
writeField(String name,
short value)
Writes an short field value. |
void |
writeField(String name,
String value)
Writes an string field value. |
void |
writeFile(String name,
String mimeType,
File file)
Writes a file's contents. |
void |
writeFile(String name,
String mimeType,
String fileName,
byte[] data)
Writes the given bytes. |
void |
writeFile(String name,
String mimeType,
String fileName,
InputStream is)
Writes a input stream's contents. |
| Methods inherited from class java.lang.Object |
|---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public MultiPartFormOutputStream(OutputStream os,
String boundary)
MultiPartFormOutputStream object using
the specified output stream and boundary. The boundary is required
to be created before using this method, as described in the
description for the getContentType(String) method.
The boundary is only checked for null or empty string,
but it is recommended to be at least 6 characters. (Or use the
static createBoundary() method to create one.)
os - the output streamboundary - the boundarycreateBoundary(),
getContentType(String)| Method Detail |
|---|
public void writeField(String name,
boolean value)
throws IOException
name - the field name (required)value - the field value
IOException - on input/output errors
public void writeField(String name,
double value)
throws IOException
name - the field name (required)value - the field value
IOException - on input/output errors
public void writeField(String name,
float value)
throws IOException
name - the field name (required)value - the field value
IOException - on input/output errors
public void writeField(String name,
long value)
throws IOException
name - the field name (required)value - the field value
IOException - on input/output errors
public void writeField(String name,
int value)
throws IOException
name - the field name (required)value - the field value
IOException - on input/output errors
public void writeField(String name,
short value)
throws IOException
name - the field name (required)value - the field value
IOException - on input/output errors
public void writeField(String name,
char value)
throws IOException
name - the field name (required)value - the field value
IOException - on input/output errors
public void writeField(String name,
String value)
throws IOException
name - the field name (required)value - the field value
IOException - on input/output errors
public void writeFile(String name,
String mimeType,
File file)
throws IOException
java.lang.IllegalArgumentException
will be thrown.
name - the field namemimeType - the file content type (optional, recommended)file - the file (the file must exist)
IOException - on input/output errors
public void writeFile(String name,
String mimeType,
String fileName,
InputStream is)
throws IOException
java.lang.IllegalArgumentException will be thrown.
name - the field namemimeType - the file content type (optional, recommended)fileName - the file name (required)is - the input stream
IOException - on input/output errors
public void writeFile(String name,
String mimeType,
String fileName,
byte[] data)
throws IOException
java.lang.IllegalArgumentException will be thrown.
name - the field namemimeType - the file content type (optional, recommended)fileName - the file name (required)data - the file data
IOException - on input/output errors
public void flush()
throws IOException
IOException - on input/output errors
public void close()
throws IOException
IOException - on input/output errorspublic String getBoundary()
public static URLConnection createConnection(URL url)
throws IOException
java.net.URLConnection object from the
specified java.net.URL. This is a convenience method
which will set the doInput, doOutput,
useCaches and defaultUseCaches fields to
the appropriate settings in the correct order.
java.net.URLConnection object for the URL
IOException - on input/output errorspublic static String createBoundary()
getContentType(String)public static String getContentType(String boundary)
java.net.URLConnection which includes the multipart
boundary string. java.net.URLConnection class, 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 the createBoundary() method.
boundary - the boundary string
createBoundary()
|
Version: 1.1.1 | |||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||