org.tinygroup.weblayer.webcontext.parser.impl
类 AbstractFileItem

java.lang.Object
  继承者 org.tinygroup.weblayer.webcontext.parser.impl.AbstractFileItem
所有已实现的接口:
Serializable, org.apache.commons.fileupload.FileItem, org.apache.commons.fileupload.FileItemHeadersSupport
直接已知子类:
DiskFileItem, InMemoryFormFieldItem

public abstract class AbstractFileItem
extends Object
implements org.apache.commons.fileupload.FileItem, org.apache.commons.fileupload.FileItemHeadersSupport

改进自commons-fileupload-1.2.1的同名类。

解决了如下问题:

  1. DiskFileItem类(以下简称原类)在解析form field的值时,会利用 content-type头部指定的charset值来决定其字符集编码。例如,下面的 multipart/form-data请求片段指定了myparam field值的字符集编码为 UTF-8

     ----HttpUnit-part0-aSgQ2M
     Content-Disposition: form-data; name="myparam"
     Content-Type: text/plain; charset=UTF-8
     

    然而,除了单元测试所用的httpunit/servletunit以外,几乎没有浏览器会在这里指定 content-type以及 charset。因此原类的 getString()总是得不到解码正确的字符串。

  2. 原类将内容的长度超过sizeThreshold的字段 —— 无论普通的form fields或是文件字段 —— 均存入文件 。这是一种优化。然而在某些情况下,我们希望关闭这种优化 —— 将 sizeThreshold设置成 0 ,以便让所有上传文件无论大小都被存入磁盘。然而仍然希望普通的form fields被保存在内存里。
  3. 创建文件时,希望能自动创建目录。

具体改进了如下内容:

作者:
Michael Zhou
另请参见:
序列化表格

字段摘要
static String DEFAULT_CHARSET
          Default content charset to be used when no explicit charset parameter is provided by the sender.
protected  org.apache.commons.io.output.DeferredFileOutputStream dfos
          Output stream for this item.
 
构造方法摘要
AbstractFileItem(String fieldName, String contentType, boolean isFormField, boolean saveInFile, String fileName, int sizeThreshold, boolean keepFormFieldInMemory, File repository, javax.servlet.http.HttpServletRequest request, FileUploadReName fileUploadRename)
          Constructs a new DiskFileItem instance.
 
方法摘要
 void delete()
          Deletes the underlying storage for a file item, including deleting any associated temporary disk file.
 byte[] get()
          Returns the contents of the file as an array of bytes.
 String getCharset()
          取得当前field的字符集编码。
 String getContentType()
          Returns the content type passed by the agent or null if not defined.
 String getFieldName()
          Returns the name of the field in the multipart form corresponding to this file item.
 org.apache.commons.fileupload.FileItemHeaders getHeaders()
          Returns the file item headers.
 InputStream getInputStream()
          Returns an InputStream that can be used to retrieve the contents of the file.
 String getName()
          Returns the original filename in the client's filesystem.
 OutputStream getOutputStream()
          Returns an OutputStream that can be used for storing the contents of the file.
 File getRepository()
           
 long getSize()
          Returns the size of the file.
 File getStoreLocation()
          Returns the File object for the FileItem's data's temporary location on the disk.
 String getString()
          取得字段或文件的内容。
 String getString(String charset)
          Returns the contents of the file as a String, using the specified encoding.
 boolean isFormField()
          Determines whether or not a FileItem instance represents a simple form field.
 boolean isInMemory()
          Provides a hint as to whether or not the file contents will be read from memory.
 void setCharset(String charset)
          设置当前field的字符集编码。
 void setFieldName(String fieldName)
          Sets the field name used to reference this file item.
 void setFormField(boolean state)
          Specifies whether or not a FileItem instance represents a simple form field.
 void setHeaders(org.apache.commons.fileupload.FileItemHeaders pHeaders)
          Sets the file item headers.
 String toString()
          Returns a string representation of this object.
 void write(File file)
          A convenience method to write an uploaded item to disk.
 
从类 java.lang.Object 继承的方法
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

字段详细信息

DEFAULT_CHARSET

public static final String DEFAULT_CHARSET
Default content charset to be used when no explicit charset parameter is provided by the sender. Media subtypes of the "text" type are defined to have a default charset value of "ISO-8859-1" when received via HTTP.

另请参见:
常量字段值

dfos

protected transient org.apache.commons.io.output.DeferredFileOutputStream dfos
Output stream for this item.

构造方法详细信息

AbstractFileItem

public AbstractFileItem(String fieldName,
                        String contentType,
                        boolean isFormField,
                        boolean saveInFile,
                        String fileName,
                        int sizeThreshold,
                        boolean keepFormFieldInMemory,
                        File repository,
                        javax.servlet.http.HttpServletRequest request,
                        FileUploadReName fileUploadRename)
Constructs a new DiskFileItem instance.

参数:
fieldName - The name of the form field.
contentType - The content type passed by the browser or null if not specified.
isFormField - Whether or not this item is a plain form field, as opposed to a file upload.
fileName - The original filename in the user's filesystem, or null if not specified.
sizeThreshold - The threshold, in bytes, below which items will be retained in memory and above which they will be stored as a file.
repository - The data repository, which is the directory in which files will be created, should the item size exceed the threshold.
方法详细信息

getRepository

public File getRepository()

getInputStream

public InputStream getInputStream()
                           throws IOException
Returns an InputStream that can be used to retrieve the contents of the file.

指定者:
接口 org.apache.commons.fileupload.FileItem 中的 getInputStream
返回:
An InputStream that can be used to retrieve the contents of the file.
抛出:
IOException - if an error occurs.

getContentType

public String getContentType()
Returns the content type passed by the agent or null if not defined.

指定者:
接口 org.apache.commons.fileupload.FileItem 中的 getContentType
返回:
The content type passed by the agent or null if not defined.

getCharset

public String getCharset()
取得当前field的字符集编码。


setCharset

public void setCharset(String charset)
设置当前field的字符集编码。


getName

public String getName()
Returns the original filename in the client's filesystem.

指定者:
接口 org.apache.commons.fileupload.FileItem 中的 getName
返回:
The original filename in the client's filesystem.

isInMemory

public boolean isInMemory()
Provides a hint as to whether or not the file contents will be read from memory.

指定者:
接口 org.apache.commons.fileupload.FileItem 中的 isInMemory
返回:
true if the file contents will be read from memory; false otherwise.

getSize

public long getSize()
Returns the size of the file.

指定者:
接口 org.apache.commons.fileupload.FileItem 中的 getSize
返回:
The size of the file, in bytes.

get

public byte[] get()
Returns the contents of the file as an array of bytes. If the contents of the file were not yet cached in memory, they will be loaded from the disk storage and cached.

指定者:
接口 org.apache.commons.fileupload.FileItem 中的 get
返回:
The contents of the file as an array of bytes.

getString

public String getString(String charset)
                 throws UnsupportedEncodingException
Returns the contents of the file as a String, using the specified encoding. This method uses get() to retrieve the contents of the file.

指定者:
接口 org.apache.commons.fileupload.FileItem 中的 getString
参数:
charset - The charset to use.
返回:
The contents of the file, as a string.
抛出:
UnsupportedEncodingException - if the requested character encoding is not available.

getString

public String getString()
取得字段或文件的内容。

对于form field,将使用传入的charset所指定的字符集编码。

对于文件,使用固定的ISO-8859-1字符集编码。如果想以其它编码来读取文件文本,可使用getString(charset) 方法。

指定者:
接口 org.apache.commons.fileupload.FileItem 中的 getString
返回:
字段值或文件文本

write

public void write(File file)
           throws Exception
A convenience method to write an uploaded item to disk. The client code is not concerned with whether or not the item is stored in memory, or on disk in a temporary location. They just want to write the uploaded item to a file.

This implementation first attempts to rename the uploaded item to the specified destination file, if the item was originally written to disk. Otherwise, the data will be copied to the specified file.

This method is only guaranteed to work once, the first time it is invoked for a particular item. This is because, in the event that the method renames a temporary file, that file will no longer be available to copy or rename again at a later time.

指定者:
接口 org.apache.commons.fileupload.FileItem 中的 write
参数:
file - The File into which the uploaded item should be stored.
抛出:
Exception - if an error occurs.

delete

public void delete()
Deletes the underlying storage for a file item, including deleting any associated temporary disk file. Although this storage will be deleted automatically when the FileItem instance is garbage collected, this method can be used to ensure that this is done at an earlier time, thus preserving system resources.

指定者:
接口 org.apache.commons.fileupload.FileItem 中的 delete

getFieldName

public String getFieldName()
Returns the name of the field in the multipart form corresponding to this file item.

指定者:
接口 org.apache.commons.fileupload.FileItem 中的 getFieldName
返回:
The name of the form field.
另请参见:
setFieldName(java.lang.String)

setFieldName

public void setFieldName(String fieldName)
Sets the field name used to reference this file item.

指定者:
接口 org.apache.commons.fileupload.FileItem 中的 setFieldName
参数:
fieldName - The name of the form field.
另请参见:
getFieldName()

isFormField

public boolean isFormField()
Determines whether or not a FileItem instance represents a simple form field.

指定者:
接口 org.apache.commons.fileupload.FileItem 中的 isFormField
返回:
true if the instance represents a simple form field; false if it represents an uploaded file.
另请参见:
setFormField(boolean)

setFormField

public void setFormField(boolean state)
Specifies whether or not a FileItem instance represents a simple form field.

指定者:
接口 org.apache.commons.fileupload.FileItem 中的 setFormField
参数:
state - true if the instance represents a simple form field; false if it represents an uploaded file.
另请参见:
isFormField()

getOutputStream

public OutputStream getOutputStream()
                             throws IOException
Returns an OutputStream that can be used for storing the contents of the file.

指定者:
接口 org.apache.commons.fileupload.FileItem 中的 getOutputStream
返回:
An OutputStream that can be used for storing the contensts of the file.
抛出:
IOException - if an error occurs.

getStoreLocation

public File getStoreLocation()
Returns the File object for the FileItem's data's temporary location on the disk. Note that for FileItems that have their data stored in memory, this method will return null. When handling large files, you can use File.renameTo(java.io.File) to move the file to new location without copying the data, if the source and destination locations reside within the same logical volume.

返回:
The data file, or null if the data is stored in memory.

toString

public String toString()
Returns a string representation of this object.

覆盖:
Object 中的 toString
返回:
a string representation of this object.

getHeaders

public org.apache.commons.fileupload.FileItemHeaders getHeaders()
Returns the file item headers.

指定者:
接口 org.apache.commons.fileupload.FileItemHeadersSupport 中的 getHeaders
返回:
The file items headers.

setHeaders

public void setHeaders(org.apache.commons.fileupload.FileItemHeaders pHeaders)
Sets the file item headers.

指定者:
接口 org.apache.commons.fileupload.FileItemHeadersSupport 中的 setHeaders
参数:
pHeaders - The file items headers.


Copyright © 2006–2013 开源组织. All rights reserved.