|
||||||||||
| 上一个类 下一个类 | 框架 无框架 | |||||||||
| 摘要: 嵌套 | 字段 | 构造方法 | 方法 | 详细信息: 字段 | 构造方法 | 方法 | |||||||||
java.lang.Objectorg.tinygroup.weblayer.webcontext.parser.impl.AbstractFileItem
public abstract class AbstractFileItem
改进自commons-fileupload-1.2.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()总是得不到解码正确的字符串。sizeThreshold的字段 —— 无论普通的form fields或是文件字段 ——
均存入文件 。这是一种优化。然而在某些情况下,我们希望关闭这种优化 —— 将 sizeThreshold设置成
0 ,以便让所有上传文件无论大小都被存入磁盘。然而仍然希望普通的form fields被保存在内存里。具体改进了如下内容:
charset参数,而不是content-type来解码form
field。但该参数对于文件型字段无效。getCharSet()方法,添加getCharset()和
setCharset()方法。getString()方法,对form field使用指定的charset来解码。keepFormFieldInMemory属性。getOutputStream()方法,,当
keepFormFieldInMemory == true时,不将form fields写入文件,即将
threshold设置成Integer.MAX_VALUE。File.createTempFile()来生成临时文件,删除原getTempFile()
方法,及相关的getUniqueId()方法、counter field、
tempFile field。FileItem对象。
| 字段摘要 | |
|---|---|
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)
|
|
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 |
| 字段详细信息 |
|---|
public static final String DEFAULT_CHARSET
protected transient org.apache.commons.io.output.DeferredFileOutputStream dfos
| 构造方法详细信息 |
|---|
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)
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.
public AbstractFileItem(String fieldName,
String contentType,
boolean isFormField,
boolean saveInFile,
String fileName,
int sizeThreshold,
boolean keepFormFieldInMemory,
File repository,
javax.servlet.http.HttpServletRequest request)
| 方法详细信息 |
|---|
public File getRepository()
public InputStream getInputStream()
throws IOException
InputStream that can be used to
retrieve the contents of the file.
org.apache.commons.fileupload.FileItem 中的 getInputStreamInputStream that can be used to
retrieve the contents of the file.
IOException - if an error occurs.public String getContentType()
null if not
defined.
org.apache.commons.fileupload.FileItem 中的 getContentTypenull if not
defined.public String getCharset()
public void setCharset(String charset)
public String getName()
org.apache.commons.fileupload.FileItem 中的 getNamepublic boolean isInMemory()
org.apache.commons.fileupload.FileItem 中的 isInMemorytrue if the file contents will be read from memory;
false otherwise.public long getSize()
org.apache.commons.fileupload.FileItem 中的 getSizepublic byte[] get()
org.apache.commons.fileupload.FileItem 中的 get
public String getString(String charset)
throws UnsupportedEncodingException
get() to retrieve the contents of the
file.
org.apache.commons.fileupload.FileItem 中的 getStringcharset - The charset to use.
UnsupportedEncodingException - if the requested character encoding is not available.public String getString()
对于form field,将使用传入的charset所指定的字符集编码。
对于文件,使用固定的ISO-8859-1字符集编码。如果想以其它编码来读取文件文本,可使用getString(charset) 方法。
org.apache.commons.fileupload.FileItem 中的 getString
public void write(File file)
throws Exception
org.apache.commons.fileupload.FileItem 中的 writefile - The File into which the uploaded item should be
stored.
Exception - if an error occurs.public void delete()
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 中的 deletepublic String getFieldName()
org.apache.commons.fileupload.FileItem 中的 getFieldNamesetFieldName(java.lang.String)public void setFieldName(String fieldName)
org.apache.commons.fileupload.FileItem 中的 setFieldNamefieldName - The name of the form field.getFieldName()public boolean isFormField()
FileItem instance represents a
simple form field.
org.apache.commons.fileupload.FileItem 中的 isFormFieldtrue if the instance represents a simple form field;
false if it represents an uploaded file.setFormField(boolean)public void setFormField(boolean state)
FileItem instance represents a
simple form field.
org.apache.commons.fileupload.FileItem 中的 setFormFieldstate - true if the instance represents a simple form
field; false if it represents an uploaded file.isFormField()
public OutputStream getOutputStream()
throws IOException
OutputStream that can be used for
storing the contents of the file.
org.apache.commons.fileupload.FileItem 中的 getOutputStreamOutputStream that can be used for
storing the contensts of the file.
IOException - if an error occurs.public File getStoreLocation()
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.
null if the data is stored in
memory.public String toString()
Object 中的 toStringpublic org.apache.commons.fileupload.FileItemHeaders getHeaders()
org.apache.commons.fileupload.FileItemHeadersSupport 中的 getHeaderspublic void setHeaders(org.apache.commons.fileupload.FileItemHeaders pHeaders)
org.apache.commons.fileupload.FileItemHeadersSupport 中的 setHeaderspHeaders - The file items headers.
|
||||||||||
| 上一个类 下一个类 | 框架 无框架 | |||||||||
| 摘要: 嵌套 | 字段 | 构造方法 | 方法 | 详细信息: 字段 | 构造方法 | 方法 | |||||||||