public abstract class AbstractFileItem extends Object implements org.apache.commons.fileupload.FileItem, org.apache.commons.fileupload.FileItemHeadersSupport
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
对象。Modifier and Type | Field and Description |
---|---|
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.
|
Constructor and Description |
---|
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. |
Modifier and Type | Method and Description |
---|---|
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.
|
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 File getRepository()
public InputStream getInputStream() throws IOException
InputStream
that can be used to
retrieve the contents of the file.getInputStream
in interface org.apache.commons.fileupload.FileItem
InputStream
that can be used to
retrieve the contents of the file.IOException
- if an error occurs.public String getContentType()
null
if not
defined.getContentType
in interface org.apache.commons.fileupload.FileItem
null
if not
defined.public String getCharset()
public void setCharset(String charset)
public String getName()
getName
in interface org.apache.commons.fileupload.FileItem
public boolean isInMemory()
isInMemory
in interface org.apache.commons.fileupload.FileItem
true
if the file contents will be read from memory;
false
otherwise.public long getSize()
getSize
in interface org.apache.commons.fileupload.FileItem
public byte[] get()
get
in interface org.apache.commons.fileupload.FileItem
public String getString(String charset) throws UnsupportedEncodingException
get()
to retrieve the contents of the
file.getString
in interface org.apache.commons.fileupload.FileItem
charset
- The charset to use.UnsupportedEncodingException
- if the requested character encoding is not available.public String getString()
对于form field,将使用传入的charset
所指定的字符集编码。
对于文件,使用固定的ISO-8859-1
字符集编码。如果想以其它编码来读取文件文本,可使用getString(charset)
方法。
getString
in interface org.apache.commons.fileupload.FileItem
public void write(File file) throws Exception
write
in interface org.apache.commons.fileupload.FileItem
file
- 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.delete
in interface org.apache.commons.fileupload.FileItem
public String getFieldName()
getFieldName
in interface org.apache.commons.fileupload.FileItem
setFieldName(java.lang.String)
public void setFieldName(String fieldName)
setFieldName
in interface org.apache.commons.fileupload.FileItem
fieldName
- The name of the form field.getFieldName()
public boolean isFormField()
FileItem
instance represents a
simple form field.isFormField
in interface org.apache.commons.fileupload.FileItem
true
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.setFormField
in interface org.apache.commons.fileupload.FileItem
state
- 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.getOutputStream
in interface org.apache.commons.fileupload.FileItem
OutputStream
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
FileItem
s 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()
public org.apache.commons.fileupload.FileItemHeaders getHeaders()
getHeaders
in interface org.apache.commons.fileupload.FileItemHeadersSupport
public void setHeaders(org.apache.commons.fileupload.FileItemHeaders pHeaders)
setHeaders
in interface org.apache.commons.fileupload.FileItemHeadersSupport
pHeaders
- The file items headers.Copyright © 2006–2018 TinyGroup. All rights reserved.