Jspsmartupload Component Analysis

Source: Internet
Author: User
Tags mime file ranges

Understanding and mastering the classes related to the jspsmartupload component is a prerequisite for using this component. It mainly has four related classes: file, files, request, and smartuploard.

The file in the jspsmartupload component is not the file class in JDK.

  1. File class
This class encapsulates all the information of an uploaded file. You can obtain the file name, file size, extension, and file data of the uploaded file. It mainly has the following methods:

(1) saveas ()
This API is used to save an object as a file, such as an object name change. Call method:
Fileojectname. saveas (string destfilepathname) or
Fileojectname. saveas (string destfilepathname, int saveasoption)

Among them, fileobjectname is the name of the generated file object; the parameter destfilepathname is the file name to be saved; the parameter saveasoption is the Save option, which has three values: saveas_physical, saveas _ virtual, and saveas_auto. Saveas_physical indicates that the root directory of the operating system is another file stored in the root directory of the file, and saveas_virtual indicates another file stored in the root directory of the Web application, saveas_auto indicates that the component determines that when the root directory of the Web application has another directory for storing files, it selects saveas_virtual; otherwise, it selects saveas_physical. For example:

Fileojectname. saveas ("/upload/test.doc", saveas_physical)

If the Web server is installed on drive C after execution, the file name stored in the web server is actually C:/upload/test.doc. Another example is:

Fileojectname. saveas ("/upload/test.doc", saveas_virtual)

After the execution, if the root directory of the Web application is webapps/root, the file name stored in the Web application is actually webapps/root/upload/test.doc. Another example:

Fileojectname. saveas ("/upload/test.doc", saveas_auto)

If an upload directory exists in the root directory of the Web application during execution, the effect is the same as that of saveas ("/upload/test.doc", saveas_virtual). Otherwise, the effect is the same as that of the following statement: saveas ("/upload/test.doc", saveas _ physical ).

For web program development, it is best to use saveas_virtual to facilitate program migration. {# Page #]

(2) ismissing ()
It is used to determine whether a file is selected, that is, whether the corresponding form item in the submitted form has a value. If a file is selected, false is returned. If no file is selected, true is returned. Call method:
Fileojectname. ismissing ()

(3) getfieldname ()
Obtain the name of the form item corresponding to the uploaded file in the HTML form. Call method:
Fileojectname. getfieldname ()

(4) getfilename ()
Get the file name (excluding the directory information ). Call method:
Fileojectname. getfilename ()

(5) getfilepathname ()
Take the full name of the directory in the file. Call method:
Fileojectname. getfilepathname ()

(6) getfileext ()
Get the file extension, that is, the suffix of the file name. Call method:
Fileojectname. getfileext ()

(7) getsize ()
The length of the file, in bytes. Call method:
Fileojectname. getsize ()

(8) getbinarydata ()

Obtains a byte of the specified displacement in the file data for file processing. Call method:
Fileojectname. getbinarydata (INT index)
The index parameter indicates the displacement. Its value ranges from 0 to fileojectname. getsize ()-1.

  2. Files
Files class indicates a set 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 ()
Obtain the number of uploaded files. Call method:
Filesojectname. getcount ()
Filesojectname is the name of the generated files class object.

(2) GetFile ()
Get the file object file at the specified displacement, which is actually com. jspsmart. Upload. file, not Java. Io. file. Please note the difference between the two. Call method:
Filesojectname. GetFile (INT index)
The index parameter indicates the specified displacement. Its value ranges from 0 to filesojectname. getcount ()-1.

(3) getsize ()

Obtains the total length of the uploaded file, which can be used to limit the size of the data volume uploaded at a time. Call method:
Filesojectname. getsize ()

(4) getcollection ()

Return all uploaded file objects in the form of collection so that other applications can reference and browse the uploaded file information. Call method:
Filesojectname. getcollection ()

(5) getenumeration ()
Returns all uploaded file objects in the form of enumeration (enumeration) so that other applications can browse the uploaded file information. Call method:
Filesojectname. getenumeration ()

  3. Request class

The function of this class is equivalent to the built-in JSP Object Request. This class is provided because the request object provided by the jspsmartupload component cannot obtain the value of the object upload item in a file upload form. This class provides the following methods:

(1) getparameter ()
Obtains the value of a specified parameter. If the parameter does not exist, the return value is null. Call method:
Request. getparameter (string name)
The parameter name is the name of the form item corresponding to the value to be obtained.
Please note that the COM. jspsmart. Upload. Request class and the system request object are differentiated.

(2) getparametervalues ()
When a parameter can have multiple values, use this method to obtain the value. It returns a string array. If the parameter does not exist, the return value is null. Call method:
Request. getarametervalues (string name)
The parameter name is the name of the form item to obtain its value.

(3) getparameternames ()
Obtain the names of all parameters in the request object to traverse all parameters. It returns an enumerated object. Call method:
Request. getparameternames ()

4. smartupload class
This class completes the upload and download tasks. There are the following methods:

(1) initialize ()
Is used to upload and download files. Method to initialize the upload and download. The first operation is required. The format is as follows:
Smartuploadobjectname. initialize (pagecontext)
This method has no return value. The pagecontext parameter is a built-in JSP page object, that is, the page context. smartuploadobjectname is the name of the generated smartupload class object.

(2) Upload ()
The method used to upload files. Used to 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.
Smartuploadobjectname. Upload ()

(3) Save ()
The method used to upload files. Save all uploaded files to the specified directory and return the number of saved files. Call method:
Smartuploadobjectname. Save (string destpathname)
Or
Smartuploadobjectname. Save (string destpathname, int option)
The destpathname parameter is the directory where the file is saved, and option is the Save option. It has three values: save_physical, save_virtual, and save_auto. Save_physical indicates that the component saves the file to the directory where the operating system root directory 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 the root directory of the file, while save_auto indicates that the component selects the file automatically, this is similar to the option value of the saveas method of the file class. Smartuploadobjectname. Save (destpathname) is equivalent to smartuploadobjectname. Save (destpathname, save_auto ).

(4) getsize ()
The method used to upload files. Obtain the total length of the uploaded file data. Call method:
Smartuploadobjectname. getsize ()

(5) getfiles ()
The method used to upload files. Obtain all uploaded files and return them as files objects. You can use the files class operation method to obtain the number of uploaded files and other information. Call method:
Smartuploadobjectname. getfiles ()

(6) getrequest ()
The method used to upload files. Obtain the request object to obtain the value of the upload form parameter. Call method:
Smartuploadobjectname. getrequest ()

(7) setallowedfileslist ()
The method used to upload files. Sets 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. Call method:
Smartuploadobjectname. setallowedfileslist (string allowedfileslist)
The allowedfileslist parameter is a 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, smartuploadobject name. setallowedfileslist ("Doc, txt,") will allow you to upload files with the doc and TXT extensions and files without the extension.

(8) setdeniedfileslist ()
The method used to upload files. Used to restrict the upload of files with the specified extension. If the file extension is limited, the component throws an exception during upload. Call method:
Smartuploadobjectname. setdeniedfileslist (string deniedfileslist)
The deniedfileslist parameter is a list of file extensions that cannot be uploaded. Each extension is separated by a comma. To prohibit the upload of files without an extension, use two commas. For example, smartuploadobject name. setdeniedfileslist ("EXE, bat,") will prohibit the upload of files with the EXE and bat extensions and files without extensions.

(9) setmaxfilesize ()

The method used to upload files. Sets the maximum length of each file that can be uploaded. Call method:
Smartuploadobjectname. setmaxfilesize (long maxfilesize)
The maxfilesize parameter is the maximum length allowed for each file to be uploaded, in bytes. When the file exceeds this length, it cannot be uploaded.

(10) settotalmaxfilesize ()

The method used to upload files. Sets the total length of the file to be uploaded, which is used to limit the data size of one-time uploads. Call method:
Smartuploadobjectname. settotalmaxfilesize (long totalmaxfilesize)
The totalmaxfilesize parameter indicates the total length of the file to be uploaded, in bytes.
11) setcontentdisposition ()

How to download a file. 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. Call method:
Smartuploadobjectname. setcontentdisposition (string contentdisposition)
The contentdisposition parameter 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 "Save as", instead of opening the file automatically. The IE browser generally determines the operations performed based on the downloaded file extension. For example, 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, of course, the client must install corresponding application software.

(12) downloadfile ()
How to download a file. Used to download files. Call method:
Smartuploadobjectname. downloadfile (string sourcefilepathname) or
Smartuploadobjectname. downloadfile (string sourcefilepathname, string contenttype) or
Smartuploadobjectname. downloadfile (string sourcefilepathname, string contenttype, string destfilename)

Among them, the first method is the most common, and the last two are used for file downloads in special circumstances, such as changing the content type and changing the file name to be saved.

The sourcefilepathname parameter is the name of the file to be downloaded, which is the full name of the file with the Directory; The contenttype parameter is the content type (mime-format file type information, which can be recognized by the browser ); the destfilename parameter is the default file name saved as after download.

 

Jspsmartupload. jar:

Http://www.blogjava.net/Files/hijackwust/jsmartcom_zh_CN.rar

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.