Jspsmartupload Upload Component

Source: Internet
Author: User
Tags command line file size file upload html form zip root directory tomcat tomcat server
js| upload One, install the article

Jspsmartupload is a fully functional file upload download component that is developed by the Www.jspsmart.com Web site and is suitable for embedding in JSP files that perform upload and download operations. This component has several features:

1, easy to use. In the JSP file just write 35 lines of Java code can be done to upload or download files, convenient.

2, can control the full upload. Using the object provided by the Jspsmartupload component and its operation method, the information of all uploaded files (including file name, size, type, extension, file data, etc.) can be obtained and conveniently accessed.

3, can upload the file in size, type and so on to make restrictions. This allows you to filter out files that do not meet your requirements.

4, download flexible. By writing only two lines of code, you can turn a Web server into a file server. Files can be downloaded using jspsmartupload, regardless of the Web server's directory or any other directory.

5, can upload the file to the database, also can download the data in the database. This functionality is for MySQL databases, because it is not generic, so this article is not going to give an example of this usage.

The Jspsmartupload component can be downloaded freely from the www.jspsmart.com website, and the name of the compressed package is jspsmartupload.zip. After downloading, extract it into Tomcat's WebApps directory with WinZip or WinRAR (described in this article as an example of Tomcat server). After decompression, the Webapps/jspsmartupload directory under the subdirectory Web-inf name changed to all uppercase Web-inf, such a change jspsmartupload class can be used. Because Tomcat is sensitive to file name capitalization, it requires that the directory of the Web application-related classes be web-inf and must be uppercase. Then restart Tomcat so that you can use the Jspsmartupload component in the JSP file.

Note that after installation as described above, only programs in the Webapps/jspsmartupload directory can use the Jspsmartupload component, and if you want all of the Tomcat server's Web applications to work with it, you must do the following:

1. Go to the command line state and switch directories to Tomcat's Webapps/jspsmartupload/web-inf directory.

2. Run Jar Pack command: jar CVF jspsmartupload.jar com

(You can also open the Resource Manager, switch to the current directory, use WinZip to compress all the files in the COM directory into Jspsmartupload.zip, and then replace the jspsmartupload.zip with the Jspsmartupload.jar file.) )

3. Copy the Jspsmartupload.jar to Tomcat's Shared/lib directory.

Second, the related class description

㈠file class

This class wraps up all the information for an uploaded file. It can get the file name, file size, extension, file data and other information of the uploaded files.

The file class mainly provides the following methods:

1, SaveAs role: To change the name of the file save.

Prototype:

public void SaveAs (java.lang.String destfilepathname)

Or

public void SaveAs (java.lang.String destfilepathname, int optionsaveas)

Where Destfilepathname is a saved file name, Optionsaveas is a saved option that has three values, respectively, Saveas_physical,saveas_virtual,saveas_auto. Saveas_physical indicates that the root directory of the operating system is saved as a file root, saveas_virtual indicates that the Web application's root directory is a separate file for the file root, and Saveas_auto that the component decides When a Web application's root directory has a directory of saved files, it chooses saveas_virtual, otherwise saveas_physical is selected.

For example, SaveAs ("/upload/sample.zip", saveas_physical) is executed, and if the Web server is installed on the C disk, the saved file name is actually c:\upload\sample.zip. When SAVEAS ("/upload/sample.zip", saveas_virtual) executes, if the root directory of the Web application is Webapps/jspsmartupload, the saved file name is actually webapps/ Jspsmartupload/upload/sample.zip. SaveAs ("/upload/sample.zip", Saveas_auto) executes when the upload directory exists under the root directory of the Web application, the effect is the same as SaveAs ("/upload/sample.zip", Saveas_ VIRTUAL), otherwise with SAVEAS ("/upload/sample.zip", saveas_physical).

Recommendation: For Web application development, it is best to use saveas_virtual for porting.

2, IsMissing

Role: This method is used to determine whether the user has selected a file, i.e., whether the corresponding form item has a value. When a file is selected, it returns false. When a file is not selected, it returns True.

Prototype: public boolean ismissing ()

3, GetFieldName

Action: Takes the name of the table item in the HTML form that corresponds to this upload file.

Prototype: Public String getfieldname ()

4, GetFileName

Function: File name (excluding directory information)

Prototype: Public String GetFileName ()

5, Getfilepathname

Function: Fetch file name (with directory)

Prototype: public String Getfilepathname

6, Getfileext

Function: Take file extension (suffix)

Prototype: Public String Getfileext ()

7, GetSize

Function: Take file Length (in bytes)

Prototype: public int getsize ()

8, Getbinarydata

Function: Take a byte from the specified displacement in the file data, and use it to detect files and other processing.

Prototype: public byte getbinarydata (int index). where index represents the displacement, and its value is between 0 and GetSize ()-1.

㈡files class

This class represents the collection of all uploaded files, by which you can get information about the number, size, and so on of the uploaded files. The following methods are available:

1, GetCount

Function: Get the number of uploaded files.

Prototype: public int getcount ()

2, GetFile

Function: Gets the file object files (this is com.jspsmart.upload.File, not java.io.File, note distinction) at the specified displacement.

Prototype: public File getFile (int index). Where index is the specified displacement, and the value is between 0 and GetCount ()-1.

3, GetSize

Function: To obtain the total length of uploaded files, can be used to limit the amount of data uploaded at a one-time size.

Prototype: public long GetSize ()

4, GetCollection

Function: To return all uploaded file objects in collection form so that other applications can reference and browse the uploaded file information.

Prototype: Public Collection getcollection ()

5, GetEnumeration

Function: All uploaded file objects are returned as enumeration (enumerated) so that other applications can browse the uploaded file information.

Prototype: Public enumeration getenumeration ()

㈢request class

The functionality of this class is equivalent to the JSP built-in object request. This class is provided only because the value of the table item cannot be obtained through the request object for the file upload form, and must be obtained through the request object provided by the Jspsmartupload component. This class provides the following methods:

1, GetParameter

Function: Gets the value of the specified parameter. When a parameter does not exist, the return value is null.

Prototype: public string GetParameter (string name). Where name is the name of the parameter.

2, Getparametervalues

Function: When a parameter can have more than one value, use this method to take its value. It returns an array of strings. When a parameter does not exist, the return value is null.

Prototype: public string[] Getparametervalues (String name). Where name is the name of the parameter.

3, Getparameternames

Function: Gets the name of all parameters in the request object to traverse all parameters. It returns an object of an enumerated type.

Prototype: Public enumeration Getparameternames ()

㈣smartupload class This class completes the upload download work.

A Upload and download the common method:

Only one: Initialize.

function: Perform upload download initialization work, must be first executed.

Prototype: There are multiple, mainly using the following:

Public final void Initialize (Javax.servlet.jsp.PageContext pagecontext)

Where, PageContext is the JSP page built-in object (page context).

B How to upload files:

1, Upload

Function: Upload file data. For the upload operation, the first step is to execute the Initialize method, and the second step is to execute the method.

Prototype: public void upload ()

2, Save

Function: Save all uploaded files to the specified directory and return the number of files saved.

Prototypes: public int Save (string destpathname) and public int Save (string destpathname,int option)

Where Destpathname saves the directory for the file, option is the save options, it has three values, respectively, Save_physical,save_virtual and Save_auto. (similar to the value of the option for the SaveAs method of the file class) Save_physical instructs the component to save the file to a directory with the operating system root as the root of the file, Save_virtual instructs the component to save the file to a directory with the Web application root as the root directory of the file. Save_auto, however, indicates that the component is automatically selected.

Note: The effect of Save (Destpathname) is equivalent to save (Destpathname,save_auto).

3, GetSize

Function: Take the total length of the uploaded file data

Prototype: public int getsize ()

4, GetFiles

function: Take all upload files, as the Files object form return, you can use the files class operation method to get the number of uploaded files and other information.

Prototype: Public Files GetFiles ()

5, Getrequest

Function: Get the Request object so that the value of the upload form parameter is obtained by this object.

Prototype: Public Request getrequest ()

6, Setallowedfileslist

Function: setting allows files with specified extensions to be uploaded, and the component throws an exception when the file name is not allowed in the upload process.

Prototype: public void Setallowedfileslist (String allowedfileslist)

Among them, Allowedfileslist is the list of file extensions that are allowed to be uploaded, separated by commas between the extension names. If you want to allow uploading of files that do not have an extension, you can use a two-comma representation. For example: Setallowedfileslist ("Doc,txt,,") will allow uploading of files with Doc and txt extensions and files with no extension.

7, Setdeniedfileslist

Role: Used to restrict uploading of files with the specified extension. If a file name extension is restricted, the component throws an exception when it is uploaded.

Prototype: public void Setdeniedfileslist (String deniedfileslist)

Among them, Deniedfileslist is the list of file extensions that are prohibited from uploading, and each extension is separated by commas. If you want to prohibit uploading files that do not have an extension, you can use a two comma to represent them. For example: Setdeniedfileslist ("Exe,bat,,") will prohibit uploading files with EXE and bat extensions and files without extensions.

8, Setmaxfilesize

Function: Sets the maximum length allowed for each file to be uploaded.

Prototype: public void Setmaxfilesize (long maxfilesize)

Among them, MaxFileSize is the maximum length allowed for each file to be uploaded, and will not be uploaded when the file exceeds this length.

9, Settotalmaxfilesize

Role: Set the total length of files allowed to upload, to limit the amount of data uploaded at once.

Prototype: public void Settotalmaxfilesize (long totalmaxfilesize)

Where Totalmaxfilesize is the total length of files that are allowed to be uploaded.

[1] [2] Next page



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.