I. Installation
JSP SmartUpload is a free-of-charge, full-featured file upload and download component developed by www.JSPsmart.com. It is suitable for embedding JSP files for uploading and downloading. 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 JSP files.
2. Full upload control. The objects provided by the JSP SmartUpload component and their operation methods can be used to obtain information about all uploaded files, including file names, sizes, types, extensions, and file data.
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 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 databases, because it is not universal, so this article does not prepare examples to introduce this usage.
The JSP SmartUpload component can be freely downloaded from www.JSPsmart.com. The compressed package name is JSP SmartUpload.zip. after the download, use WinZip or WinRAR to decompress it to the webapps directory of Tomcat. This article takes the Tomcat server as an example ). After decompression, the webapps/JSP SmartUpload directory of the subdirectory Web-inf name to all uppercase WEB-INF, so that a change to JSP SmartUpload class can be used. Because Tomcat is case sensitive to file names, it requires that the directory of classes related to Web applications be WEB-INF and must be in uppercase. Restart Tomcat to use the JSP SmartUpload component in the JSP file.
Note: After the preceding method is installed, only the programs in the webapps/JSP SmartUpload directory can use the JSP SmartUpload component. If you want to allow all Web applications on the Tomcat server to use it, you must do the following:
1. Go to the command line status and switch the directory to the Tomcat webapps/JSP SmartUpload/WEB-INF directory.
2. run the JAR packaging command: jar cvf JSP SmartUpload. jar com can also open the resource manager, switch to the current directory, use WinZip to compress all files under the com Directory into JSP SmartUpload.zip, and rename JSP SmartUpload.zip as JSP SmartUpload. jar file .)
3. Copy JSP SmartUpload. jar to the shared/lib directory of Tomcat.
Ii. Related Categories
1). 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 saveAsjava. lang. string destFilePathName) or public void saveAsjava. lang. string destFilePathName, int optionSaveAs) Where destFilePathName is the object name of another storage, and optionSaveAs is the option of another storage, which has three values: SAVEAS_PHYSICAL, SAVEAS_VIRTUAL, 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 Web application is the root directory of the file, and the other file is saved. 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, saveAs "/uplo Ad/sample.zip ", SAVEAS_PHYSICAL) after execution, if the Web server is installed on drive c, the actual file name stored in the file is c: \ upload \ sample.zip. and saveAs "/upload/sample.zip", SAVEAS_VIRTUAL) after execution, if the root directory of the Web application is webapps/JSP SmartUpload, the file name stored in the file is actually webapps/JSP 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.
1. isMissing: 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 a file is not selected, true is returned. prototype: public boolean isMissing)
2. getFieldName: name of the form item corresponding to the uploaded file in the HTML form. Prototype: public String getFieldName)
3. getFileName function: get the file name without the directory information) prototype: public String getFileName)
4. getFilePathName: Take the full name of the file with the Directory) prototype: public String getFilePathName
5. getFileExt function: Get the extension suffix) prototype: public String getFileExt)
6. getSize: the object length is measured in bytes.) prototype: public int getSize)
7. getBinaryData: obtains a byte of the specified displacement in the file data for file detection and processing. Prototype: public byte getBinaryDataint index ). Here, index indicates the displacement, and its value ranges from 0 to getSize)-1.
2). Files class this class indicates the set of all uploaded Files, through which information such as the number and size of uploaded Files can be obtained. You can use the following methods:
1. getCount: obtains the number of uploaded files. Prototype: public int getCount)
2. getFile function: get the File object File at the specified displacement. This is com. JSPsmart. upload. File, not java. io. File. Note the difference. prototype: public File getFileint index ). Here, index is the specified displacement, and its value ranges from 0 to getCount)-1.
3. getSize: Get the total length of the uploaded file. It can be used to limit the size of the data uploaded at a time. Prototype: public long getSize)
4. getCollection: returns all uploaded file objects in the form of Collection, so that other applications can reference and browse the uploaded file information. Prototype: public Collection getCollection)
5. getEnumeration: returns all uploaded file objects in the form of Enumeration, so that other applications can browse the uploaded file information.
Prototype: public Enumeration getEnumeration)
3 ). the function of the Request class is equivalent to that of 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 JSP SmartUpload component. This class provides the following methods:
1. getParameter: Get the value of a specified parameter. If the parameter does not exist, the return value is null. prototype: public String getParameterString name ). Here, name is the parameter name.
2. getParameterValues: This method is used to obtain the value of a parameter when it can have multiple values. It returns a string array. If the parameter does not exist, the return value is null. prototype: public String [] getParameterValuesString name ). Here, name is the parameter name.
3. getParameterNames: obtains the names of all parameters in the Request object and is used to 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:
There is only one: initialize. The role is to perform the initialization of upload and download, which must be the first. Prototype: there are multiple, mainly using the following: public final void initializejavax. servlet. JSP. PageContext pageContext), where pageContext is the built-in object Page Context of the JSP page ).
B. How to upload files:
1. upload: uploads 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: saves all uploaded files to the specified directory and returns the number of saved files. Prototype: public int saveString destPathName) and public int saveString destPathName, int option) Where destPathName is the file storage directory and option is the storage option, which 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: savedestPathName) is equivalent to savedestPathName, SAVE_AUTO ).
3. getSize function: Get the total length of the uploaded file data prototype: public int getSize)
4. getFiles: Get 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: Get the Request object to obtain the value of the upload form parameter. Prototype: public Request getRequest)
6. setAllowedFilesList: sets the permission to upload files with the specified extension. When a file name is not allowed during the upload process, the component throws an exception. Prototype: public void setAllowedFilesListString allowedFilesList) Where allowedFilesList is a list of file extensions that can be uploaded, separated by commas. To upload files without an extension, use two commas. For example, setAllowedFilesList "doc, txt,") will allow you to upload files with the doc and txt extensions and files without the extension.
7. setDeniedFilesList: used to 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 setDeniedFilesListString 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,") files with exe and bat extensions and files without extensions are prohibited from being uploaded.
8. setMaxFileSize: sets the maximum length of each file that can be uploaded. Prototype: public void setMaxFileSizelong maxFileSize) Where maxFileSize is the maximum length allowed for each file to be uploaded. When the file exceeds this length, it will not be uploaded.
9. setTotalMaxFileSize: sets the total length of the file that can be uploaded, which is used to limit the size of data uploaded at a time. Prototype: public void setTotalMaxFileSizelong totalMaxFileSize) Where totalMaxFileSize is the total length of the file that can be uploaded.
C. Common Methods for downloading files
1. setContentDisposition: append data to the CONTENT-DISPOSITION field of the MIME file header. The JSP SmartUpload 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 setContentDispositionString contentDisposition) Where 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 automatically Opening this file, ie usually decides what to perform 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 ).
2. downloadFile: 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 downloadFileString sourceFilePathName) Where sourceFilePathName is the name of the file with the directory name to be downloaded)
◆ Public void downloadFileString sourceFilePathName, String contentType) Where sourceFilePathName is the name of the file to be downloaded with the full name of the Directory) and contentType is the file type information in Content-Type MIME format, can be recognized by browsers ).
◆ Public void downloadFileString sourceFilePathName, String contentType, String destFileName) Where sourceFilePathName is the full name of the file with the directory in the file name to be downloaded, and contentType is the file type information in Content-Type MIME format, can be recognized by the browser), destFileName is another default file name after download.
- Brief Introduction to JSP Technology
- Ten JSP tag libraries to be mastered
- Common Methods of form data storage in JSP Technology
- Detailed explanation of JSP technology methods
- How to Learn JSP Technology