Description of SmartUpload-related classes

Source: Internet
Author: User
Tags mime file

Description of SmartUpload-related classes

(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)

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.

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.