Groovy Documentation

org.jdesktop.html.form
[Java] Class FileParameter

java.lang.Object
  org.jdesktop.beans.AbstractBean
      org.jdesktop.http.NameValuePair
          org.jdesktop.http.Parameter
              org.jdesktop.html.form.FileParameter

public class FileParameter
extends Parameter

A special Parameter intended to be used as a Form Parameter on a FormRequest for uploading files.

FileParameter adds the contentType and fileName properties. These properties are used to construct the multipart/form-data body of a form request. It is important that the fileName refer to an actual existing file object, and that the contentType be accurate. If either of these two constraints are not met, then the request will likely fail.

The fileName must be formatted such that creating a new File will succeed in locating the file. We use the same rules as the URLConnection class for determining the contentType automatically for the file. In the common case, it will be unnecessary to indicate the contentType manually. However, in special cases you may need to specify the content type manually. The format of the contentType is a mime type, such as text/plain.

If the fileName is correctly specified, then the InputStream returned from getValueStream() will be non-null. You are responsible for closing this stream when finished reading from it.

The "value" of this Parameter is specified to be the fileName. This is enforced by overridding setValue to be a null-op, and by setFilename() calling super.setValue().

Authors:
Richard


Field Summary
private static FileNameMap MIMES

private String contentType

private boolean contentTypeSetManually

private String fileName

 
Fields inherited from class NameValuePair
name, value
 
Constructor Summary
FileParameter()

Create a new FileParameter.

FileParameter(String name, File file)

Create a new FileParameter using the given name as the name of this parameter (the key, if you will), and the given File as the file to be uploaded by the FormRequest.

FileParameter(String name, String fileName)

Create a new FileParameter using the given name as the name of this parameter (the key if you will) and the given fileName as the absolute path to the file to be uploaded by the FormRequest.

 
Method Summary
FileParameter clone()

static String computeMimeType(String filename)

Static utility method for computing the mime type based on a filename.

String getContentType()

Gets the content type of the data being uploaded.

String getFilename()

Gets the absolute path of the file being uploaded.

private static FileNameMap getMimeMap()

Static utility method for getting the FileNameMap used to map file names to mime types.

InputStream getValueStream()

Creates and returns a new InputStream based on the underlying file data.

void setContentType(String contentType)

Sets the mime-type of the content being uploaded.

void setFilename(String name)

Sets the absolute path to the file to be uploaded.

void setValue(String value)

 
Methods inherited from class Parameter
clone
 
Methods inherited from class NameValuePair
clone, getName, getValue, setName, setValue, toString
 
Methods inherited from class org.jdesktop.beans.AbstractBean
org.jdesktop.beans.AbstractBean#clone(), org.jdesktop.beans.AbstractBean#addPropertyChangeListener(java.lang.String, java.beans.PropertyChangeListener), org.jdesktop.beans.AbstractBean#addPropertyChangeListener(java.beans.PropertyChangeListener), org.jdesktop.beans.AbstractBean#removePropertyChangeListener(java.beans.PropertyChangeListener), org.jdesktop.beans.AbstractBean#removePropertyChangeListener(java.lang.String, java.beans.PropertyChangeListener), org.jdesktop.beans.AbstractBean#getPropertyChangeListeners(java.lang.String), org.jdesktop.beans.AbstractBean#getPropertyChangeListeners(), org.jdesktop.beans.AbstractBean#addVetoableChangeListener(java.lang.String, java.beans.VetoableChangeListener), org.jdesktop.beans.AbstractBean#addVetoableChangeListener(java.beans.VetoableChangeListener), org.jdesktop.beans.AbstractBean#removeVetoableChangeListener(java.lang.String, java.beans.VetoableChangeListener), org.jdesktop.beans.AbstractBean#removeVetoableChangeListener(java.beans.VetoableChangeListener), org.jdesktop.beans.AbstractBean#getVetoableChangeListeners(java.lang.String), org.jdesktop.beans.AbstractBean#getVetoableChangeListeners(), org.jdesktop.beans.AbstractBean#wait(), org.jdesktop.beans.AbstractBean#wait(long), org.jdesktop.beans.AbstractBean#wait(long, int), org.jdesktop.beans.AbstractBean#equals(java.lang.Object), org.jdesktop.beans.AbstractBean#toString(), org.jdesktop.beans.AbstractBean#hashCode(), org.jdesktop.beans.AbstractBean#getClass(), org.jdesktop.beans.AbstractBean#notify(), org.jdesktop.beans.AbstractBean#notifyAll()
 

Field Detail

MIMES

private static FileNameMap MIMES


contentType

private String contentType


contentTypeSetManually

private boolean contentTypeSetManually


fileName

private String fileName


 
Constructor Detail

FileParameter

public FileParameter()
Create a new FileParameter. You will need to specify a name and fileName at some point for this parameter to be useful.


FileParameter

public FileParameter(String name, File file)
Create a new FileParameter using the given name as the name of this parameter (the key, if you will), and the given File as the file to be uploaded by the FormRequest.
Parameters:
name
file - may be null.


FileParameter

public FileParameter(String name, String fileName)
Create a new FileParameter using the given name as the name of this parameter (the key if you will) and the given fileName as the absolute path to the file to be uploaded by the FormRequest.
Parameters:
name
fileName - may be null.


 
Method Detail

clone

FilePa
public FileParameter clone()


computeMimeType

static String computeMimeType(String filename)
Static utility method for computing the mime type based on a filename. This is made package private solely for the sake of testing.
Parameters:
filename
Returns:


getContentType

public final String getContentType()
Gets the content type of the data being uploaded. This is never null, and defaults to "content/unknown" when the mime type of the data couldn't be determined and was not set manually.
Returns:


getFilename

public final String getFilename()
Gets the absolute path of the file being uploaded.
Returns:


getMimeMap

private static final FileNameMap getMimeMap()
Static utility method for getting the FileNameMap used to map file names to mime types.
Returns:


getValueStream

public InputStream getValueStream()
Creates and returns a new InputStream based on the underlying file data. If it is not possible to create such an InputStream due to an incorrect or missing file, then null is returned.
Returns:


setContentType

public void setContentType(String contentType)
Sets the mime-type of the content being uploaded. Generally this is determined automatically when the fileName is set. In rare cases where the mime-type cannot be determined, or when you want to force the mime type, you can set the content type manually with this method.
Parameters:
contentType - if set to null, then the mime type "content/unknown" is used.


setFilename

public void setFilename(String name)
Sets the absolute path to the file to be uploaded. If null, then the filename is cleared and the parameter becomes useless for uploading. Invoking this method causes the content type to be set automatically, unless it has been set manually.
Parameters:
name


setValue

@Override
public void setValue(String value)


 

Groovy Documentation