Common methods of FileItem class:
1. boolean isFormField()
The isFormField method is used to determine whether the data encapsulated by the FileItem class object is a normal text form field or a file form field. If it is a normal form field, it returns true, otherwise it returns false. Therefore, this method can be used to determine whether it is a normal form field or a file upload form field.
2. String getName()
The getName method is used to get the file name in the file upload field.
Note that the file names obtained in IE or FireFox are different. In IE, it is an absolute path, and in FireFox, it is just a file name.
3. String getFieldName()
The getFieldName method is used to return the value of the form tag name attribute. As in the above example, the value of <input type="text" name="column" />.
4. void write(File file)
The write method is used to save the body content saved in the FileItem object to a specified file. If the main content in the FileItem object is saved in a temporary file, after the method is successfully completed, the temporary file may be cleared. This method can also write the content of a normal form field into a file, but its main purpose is to save the uploaded file content in the local file system.
5. String getString()
The getString method is used to return the data stream content saved in the FileItem object as a string. It has two overloaded definitions:
public java.lang.String getString()
public java.lang.String getString(java.lang.String encoding)
throws java.io.UnsupportedEncodingException
The former uses the default character set encoding to convert the body content into a string, and the latter uses the character set encoding specified by the parameter to convert the body content into a string. If Chinese garbled occurs when reading the content of a normal form field element, please call the second getString method and pass the correct character set encoding name for it.
6. String getContentType()
The getContentType method is used to obtain the type of uploaded file, that is, the value of the form field element description header attribute "Content-Type", such as "image/jpeg". If the FileItem class object corresponds to a normal form field, the method will return null.
7. boolean isInMemory()
The isInMemory method is used to determine whether the data content encapsulated by the FileItem object is stored in memory or in a temporary file. If it is stored in memory, it returns true, otherwise it returns false.
8. void delete()
The delete method is used to clear the main content stored in the FileItem class object. If the main content is saved in a temporary file, the delete method will delete the temporary file.
Although temporary files will be automatically cleared when the FileItem object is collected by the garbage collector, calling the delete method in time can clear temporary files earlier and free up system storage resources. In addition, when an exception occurs in the system, it is still possible that some temporary files will be permanently saved on the hard disk.
9. InputStream getInputStream()
Returns the data content of the uploaded file in the form of a stream.
10. long getSize()
Returns the size in bytes of the uploaded file.
The above article briefly talks about the common methods of FileItem are all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.