Reference: full introduction to jspSmartUpload upload/download

Source: Internet
Author: User

Author: Unknown Source: Unknown join time: New Software Park
I. Installation

Jsp (preferred for SUN Enterprise Applications) SmartUpload is developed by www. jsp (preferred for SUN Enterprise Applications) a free-of-charge full-featured file upload/download component developed on the smart.com website, suitable for embedding jsp for performing upload/download operations (preferred for SUN Enterprise applications) file. This component has the following features:

1. Easy to use. You can easily upload or download files by writing only three or five lines of JAVA code in the jsp (preferred for SUN Enterprise Applications) file.

2. Full upload control. The objects provided by the SmartUpload component of jsp (preferred for SUN Enterprise applications) and their operation methods can be used to obtain information about all uploaded files (including file names, sizes, types, extensions, and file data ), easy access.

3. restrict the size and type of uploaded files. This filters out non-conforming files.

4. Flexible download. Only two lines of code can be written to convert the Web server into a file server. Jsp (preferred for SUN Enterprise Applications) SmartUpload can be used to download files in the Web server directory or any other directory.

5. You can upload files to the database or download data from the database. This function is intended for MySQL (the best combination with PHP) databases. Because it is not universal, this article does not provide examples to introduce this usage.

. After the download, use WinZip or WinRAR to decompress it to the webapps directory of tomcat (a very useful JSP running platform) (This article uses tomcat (a very useful JSP running platform) server ). After unzipping, change the name of the child ke yuneb-inf under the smartupload directory of webapps/jsp (the preferred choice for SUN Enterprise applications) to a fully capitalized WEB-INF) only the SmartUpload class can be used. Because tomcat (a good JSP running platform) is case sensitive to file names, it requires that the directory of classes related to the Web application be a WEB-INF and must be in uppercase. Restart tomcat (a very useful JSP running platform), so that jsp can be used in the jsp (preferred for SUN Enterprise Applications) file (preferred for SUN Enterprise applications) smartUpload component.

Note: After the above installation, only the programs under the smartupload directory of webapps/jsp (preferred for SUN Enterprise Applications) can use the SmartUpload component of jsp (preferred for SUN Enterprise applications, if you want all Web applications of the tomcat (a very useful JSP running platform) server to use it, you must do the following:

1. Go to the command line status and switch the directory to the smartupload/WEB-INF directory of webapps/JSP (preferred for SUN Enterprise Applications) of tomcat (a good jsp running platform.

2. Run the JAR packaging command: jar cvf jsp (preferred for SUN Enterprise Applications) SmartUpload. jar com

The secret name is changed to the jsp (preferred for SUN Enterprise Applications) SmartUpload. jar file .)

3. Copy the SmartUpload. jar file of jsp (preferred for SUN Enterprise applications) to the shared/lib directory of tomcat (a useful JSP operating platform.

Ii. Related Categories

(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 disk c, the actual file name stored in another file is c: uploadsample.zip. However, after saveAs ("/upload/sample.zip", SAVEAS_VIRTUAL) is executed, if the root directory of the Web application is webapps/jsp (preferred for SUN Enterprise Applications) smartupload, the file name stored in the file is actually webapps/jsp (preferred for SUN Enterprise Applications) smartupload/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 the first choice for com. jsp (SUN Enterprise Applications) smart. 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 functionality of this class is equivalent to the built-in Object request of jsp (preferred for SUN Enterprise applications. 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 jsp (preferred for SUN Enterprise applications) the Request object provided by the SmartUpload 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 (preferred for SUN Enterprise Applications). PageContext pageContext)

Among them, pageContext is jsp (preferred for SUN Enterprise applications)

Related Article

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.