cds.tools
Class MultiPartPostOutputStream

java.lang.Object
  extended by cds.tools.MultiPartPostOutputStream

public class MultiPartPostOutputStream
extends java.lang.Object

Code pour gérer l'upload de fichier en POST (encoding type: multipart/form-data) comme le ferait un browser dans un navigateur

Author:
T. Boch [CDS]

Field Summary
 java.lang.String boundary
           
 
Constructor Summary
MultiPartPostOutputStream(java.io.OutputStream os, java.lang.String boundary)
          Create a new MultiPartPostOutputStream object
 
Method Summary
 void close()
          Closes the stream.
static java.lang.String createBoundary()
          Creates a multipart boundary string by concatenating 20 hyphens (-) and the hexadecimal (base-16) representation of the current time in milliseconds.
static java.net.URLConnection createConnection(java.net.URL url)
          Creates a new java.net.URLConnection object from the specified java.net.URL.
 java.lang.String getBoundary()
          Gets the multipart boundary string being used by this stream.
static java.lang.String getContentType(java.lang.String boundary)
          Gets the content type string suitable for the java.net.URLConnection which includes the multipart boundary string.
static void setTmpDir(java.lang.String dir)
           
 void writeField(java.lang.String name, java.lang.String value)
          Writes an string field value.
 void writeFile(java.lang.String name, java.lang.String mimeType, java.io.File file, boolean gzip)
          Writes a file's contents.
 void writeFile(java.lang.String name, java.lang.String mimeType, java.lang.String fileName, byte[] data)
          Writes the given bytes.
 void writeFile(java.lang.String name, java.lang.String mimeType, java.lang.String fileName, java.io.InputStream is, boolean gzip)
          Writes a input stream's contents.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

boundary

public java.lang.String boundary
Constructor Detail

MultiPartPostOutputStream

public MultiPartPostOutputStream(java.io.OutputStream os,
                                 java.lang.String boundary)
Create a new MultiPartPostOutputStream object

Parameters:
os - the output stream
boundary - the boundary. If null, a boundary will be created for you
Method Detail

writeField

public void writeField(java.lang.String name,
                       java.lang.String value)
                throws java.io.IOException
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:
java.io.IOException - on input/output errors

writeFile

public void writeFile(java.lang.String name,
                      java.lang.String mimeType,
                      java.io.File file,
                      boolean gzip)
               throws java.io.IOException
Writes a file's contents. If the file is null, does not exists, or is a directory, a java.lang.IllegalArgumentException will be thrown.

Parameters:
name - the field name
mimeType - the file content type (optional, recommended)
file - the file (the file must exist)
gzip - do we compress the file ?
Throws:
java.io.IOException - on input/output errors

writeFile

public void writeFile(java.lang.String name,
                      java.lang.String mimeType,
                      java.lang.String fileName,
                      java.io.InputStream is,
                      boolean gzip)
               throws java.io.IOException
Writes a input stream's contents. If the input stream is null, a java.lang.IllegalArgumentException will be thrown.

Parameters:
name - the field name
mimeType - the file content type (optional, recommended)
fileName - the file name (required)
is - the input stream
Throws:
java.io.IOException - on input/output errors

writeFile

public void writeFile(java.lang.String name,
                      java.lang.String mimeType,
                      java.lang.String fileName,
                      byte[] data)
               throws java.io.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, a java.lang.IllegalArgumentException will be thrown.

Parameters:
name - the field name
mimeType - the file content type (optional, recommended)
fileName - the file name (required)
data - the file data
Throws:
java.io.IOException - on input/output errors

close

public void close()
           throws java.io.IOException
Closes the stream.

NOTE: This method MUST be called to finalize the multipart stream.

Throws:
java.io.IOException - on input/output errors

getBoundary

public java.lang.String getBoundary()
Gets the multipart boundary string being used by this stream.

Returns:
the boundary

createConnection

public static java.net.URLConnection createConnection(java.net.URL url)
                                               throws java.io.IOException
Creates a new 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.

Returns:
a java.net.URLConnection object for the URL
Throws:
java.io.IOException - on input/output errors

createBoundary

public static java.lang.String 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(String)

getContentType

public static java.lang.String getContentType(java.lang.String boundary)
Gets the content type string suitable for the java.net.URLConnection which includes the multipart boundary string.

This method is static because, due to the nature of the 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.

Parameters:
boundary - the boundary string
Returns:
the content type string
See Also:
createBoundary()

setTmpDir

public static void setTmpDir(java.lang.String dir)


Copyright © 2009 UDS/CNRS