(I) File class
This class encapsulates all the information of an uploaded file. You can obtain the file name, file size, extension, file data, and other information of the uploaded file.
The file class mainly provides the following methods:
1. saveas: Replace the file with another name.
Prototype:
Public void saveas (Java. Lang. String destfilepathname)
Or
Public void saveas (Java. Lang. String destfilepathname, int optionsaveas)
Here, destfilepathname is the name of another stored file, and optionsaveas is the option of another stored. This option has three values: saveas_physical, saveas_virtual, and saveas_auto. Saveas_physical indicates that the root directory of the operating system is the root directory of the file and the root directory of the file. saveas_virtual indicates that the root directory of the Web application is the root directory of the file, and saveas_auto indicates that the component is determined, when the root directory of the Web application has another directory for storing files, it will select saveas_virtual; otherwise, it will select saveas_physical.
For example, after saveas ("/upload/sample.zip", saveas_physical) is executed, if the web server is installed on the drive C, the actual file name stored in the file is c: \ upload \ sample.zip. However, after saveas ("/upload/sample.zip", saveas_virtual) is executed, if the root directory of the Web application is webapps/jspsmartupload, the actual file name is webapps/jspsmartupload/upload/sample.zip. Saveas ("/upload/sample.zip", saveas_auto) if the web application root directory contains the upload directory, the effect is the same as saveas ("/upload/sample.zip", saveas_virtual ), otherwise, it is the same as saveas ("/upload/sample.zip", saveas_physical ).
Suggestion: for web program development, it is best to use saveas_virtual for porting.
2. ismissing
Purpose: This method is used to determine whether a file is selected, that is, whether the corresponding form item has a value. If a file is selected, false is returned. If the file is not selected, true is returned.
Prototype: Public Boolean ismissing ()
3. getfieldname
Purpose: Obtain the name of the form item corresponding to the uploaded file in the HTML form.
Prototype: Public String getfieldname ()
4. getfilename
Purpose: get the file name (excluding the directory information)
Prototype: Public String getfilename ()
5. getfilepathname
Purpose: Take the full name of the file (with directory)
Prototype: Public String getfilepathname
6. getfileext
Purpose: Obtain the file extension (suffix)
Prototype: Public String getfileext ()
7. getsize
Purpose: take the file length (in bytes)
Prototype: Public int getsize ()
8. getbinarydata
Purpose: Take a byte of the specified displacement in the file data for file detection and other processing.
Prototype: Public byte getbinarydata (INT index ). Here, index indicates the displacement, and its value ranges from 0 to getsize ()-1.
(Ii) files
This class indicates a collection of all uploaded files. You can obtain information such as the number and size of uploaded files. You can use the following methods:
1. getcount
Purpose: obtain the number of uploaded files.
Prototype: Public int getcount ()
2. GetFile
Purpose: Obtain the file object file at the specified displacement (this is com. jspsmart. Upload. file, not Java. Io. file. Note the difference ).
Prototype: Public file GetFile (INT index ). Here, index is the specified displacement, and its value ranges from 0 to getcount ()-1.
3. getsize
Purpose: Get the total length of the uploaded file, which can be used to limit the size of the data uploaded at a time.
Prototype: Public long getsize ()
4. getcollection
Purpose: return all uploaded file objects in the form of collections so that other applications can reference and browse the uploaded file information.
Prototype: public collection getcollection ()
5. getenumeration
Purpose: return all uploaded file objects in the form of enumeration (enumeration), so that other applications can browse the uploaded file information.
Prototype: Public enumeration getenumeration ()
(Iii) Request class
The function of this class is equivalent to the built-in JSP Object Request. Only this class is provided because the value of the form item cannot be obtained through the request object for file upload forms. It must be obtained through the request object provided by the jspsmartupload component. This class provides the following methods:
1. getparameter
Purpose: obtain the value of a specified parameter. If the parameter does not exist, the return value is null.
Prototype: Public String getparameter (string name ). Here, name is the parameter name.
2. getparametervalues
Purpose: use this method to obtain the value of a parameter if it can have multiple values. It returns a string array. If the parameter does not exist, the return value is null.
Prototype: Public String [] getparametervalues (string name ). Here, name is the parameter name.
3. getparameternames
Purpose: Obtain the names of all parameters in the request object and traverse all parameters. It returns an enumerated object.
Prototype: Public enumeration getparameternames ()
(4) The smartupload class completes upload and download.
A. Shared upload and download methods:
Only one: Initialize.
Purpose: perform the initialization of upload/download. The first task is required.
Prototype: there are multiple, mainly using the following:
Public final void initialize (javax. servlet. jsp. pagecontext)
Pagecontext is a built-in JSP page object (page context ).
B. How to upload files:
1. Upload
Purpose: Upload File data. For the upload operation, the first step is to execute the initialize method, and the second step is to execute this method.
Prototype: Public void upload ()
2. Save
Purpose: Save all uploaded files to the specified directory and return the number of saved files.
Prototype: Public int save (string destpathname)
And public int save (string destpathname, int option)
Destpathname is the file storage directory, and option is the storage option. It has three values: save_physical, save_virtual, and save_auto. (Similar to the value of the saveas method in the file class) save_physical indicates that the component saves the file to the directory where the root directory of the operating system is the root directory of the file, save_virtual indicates that the component saves the file to the directory where the root directory of the Web application is used as the root directory, while save_auto indicates that the component selects the file automatically.
Note: Save (destpathname) is equivalent to save (destpathname, save_auto ).
3. getsize
Purpose: obtain the total length of the uploaded file data.
Prototype: Public int getsize ()
4. getfiles
Purpose: retrieve all uploaded files and return them as files objects. You can use the files operation method to obtain the number of uploaded files and other information.
Prototype: public files getfiles ()
5. getrequest
Purpose: Obtain the request object to obtain the value of the upload form parameter.
Prototype: public request getrequest ()
6. setallowedfileslist
Purpose: Set whether to upload a file with the specified extension. When a file name is not allowed during the upload process, the component throws an exception.
Prototype: Public void setallowedfileslist (string allowedfileslist)
Allowedfileslist is the list of file extensions that can be uploaded. Each extension is separated by a comma. To upload files without an extension, use two commas. For example, setallowedfileslist ("Doc, txt,") allows you to upload files with the doc and TXT extensions and files without the extension.
7. setdeniedfileslist
Purpose: restrict the upload of files with the specified extension. If the file extension is limited, the component throws an exception during upload.
Prototype: Public void setdeniedfileslist (string deniedfileslist)
Deniedfileslist is a list of file extensions that are not allowed to be uploaded. Each extension is separated by a comma. To prohibit the upload of files without an extension, use two commas. For example, setdeniedfileslist ("EXE, bat,") will prohibit the upload of files with EXE and bat extensions and files without extension extensions.
8. setmaxfilesize
Purpose: set the maximum length of each file that can be uploaded.
Prototype: Public void setmaxfilesize (long maxfilesize)
Maxfilesize is the maximum length that each file can upload. When the file length exceeds this length, it is not uploaded.
9. settotalmaxfilesize
Purpose: set the total length of the file that can be uploaded to limit the size of the data volume for one-time upload.
Prototype: Public void settotalmaxfilesize (long totalmaxfilesize)
Totalmaxfilesize indicates the total length of the file to be uploaded.
C. Common Methods for downloading files
1. setcontentdisposition
Purpose: append data to the content-Disposition field of the MIME file header. The jspsmartupload component automatically fills in the Content-Disposition field of the MIME file header when returning the downloaded information. If you need to add additional information, use this method.
Prototype: Public void setcontentdisposition (string contentdisposition)
Here, contentdisposition is the data to be added. If contentdisposition is null, the component automatically adds "attachment;" to indicate that the downloaded file is used as an attachment. The result is that the IE browser will prompt you to save the file, instead of opening the file automatically (ie usually decides what to do based on the downloaded file extension. If the extension is Doc, it will be opened by the word program, and if the extension is PDF, It will be opened by the acrobat program, and so on ).
2. downloadfile
Purpose: download an object.
Prototype: The following three prototypes are available: the first one is the most commonly used, and the last two are used for downloading files in special circumstances (such as changing the content type and changing the file name of another storage ).
① Public void downloadfile (string sourcefilepathname)
Among them, sourcefilepathname is the name of the file to be downloaded (full name of the file with directory)
② Public void downloadfile (string sourcefilepathname, string contenttype)
Among them, sourcefilepathname is the name of the file to be downloaded (the full name of the file with the Directory), and contenttype is the content type (MIME format file type information, which can be recognized by the browser ).
③ Public void downloadfile (string sourcefilepathname, string contenttype, string destfilename)
Among them, sourcefilepathname is the name of the file to be downloaded (the full name of the file with the Directory), contenttype is the content type (MIME format file type information, which can be recognized by the browser ), destfilename is the default file name for storing files after downloading.