How to upload files in web programs?

Source: Internet
Author: User
Tags mime file ranges

How to upload files in web programs I found some documents yesterday and wrote an example of uploading files based on the documents for you. For more information, download and study it yourself.

(Create an upload directory in the operating system, deploy the program to Tomcat, and start tomcar to input "http: // localhost: 8080/uploadfile/upload. JSP ") jspsmartupload is a free-of-charge, full-featured file upload and download component developed by www.jspsmart.com. It is suitable for embedding a JSP file for uploading and downloading. If you are interested, you can download it from their website. However, I put their source programs in my example. You can refer to their principles or change them. If it is inconvenient, you can create a jar package by yourself.

The following are the characteristics of jspsmartupload: (The following documents are not my summary. I copied them, and I don't know who they are. But it is useful) 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 jspsmartupload component and their operation methods can be used to obtain information about all uploaded files (including file names, sizes, types, extensions, and file data) for 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. You can use jspsmartupload 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. Related class instructions
 
(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)
 
Its
In, destfilepathname is another stored file name, And optionsaveas is another stored option, which has three values:
Saveas_physical, saveas_virtual, saveas_auto. Saveas_physical indicates to use the root directory of the operating system as the file
Another file is saved in the root directory. saveas_virtual indicates that the root directory of the Web application is used as the root directory of the file and another file is saved. saveas_auto indicates that the component is determined. When the Web Application
If the root directory of the program has another directory for storing files, it will select saveas_virtual; otherwise, it will select saveas_physical.
 
Example
For example, if the web server is installed on disk C after saveas ("/upload/sample.zip", saveas_physical) is executed
C:/upload/sample.zip. However, after saveas ("/upload/sample.zip", saveas_virtual) is executed
The root directory of the application is webapps/jspsmartupload, and the actual file name is webapps/jspsmartupload/upload.
/Sample.zip. When saveas ("/upload/sample.zip", saveas_auto) is executed
In the upload directory, the effect is the same as that of saveas ("/upload/sample.zip", saveas_virtual). Otherwise, the effect is the same as that of 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 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)
 
Its
, Destpathname is the file storage directory, option is the Save option, it has three values, namely 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
Directory, save_virtual indicates that the component saves the file to the directory where the web application root directory is the root directory of the file, and save_auto indicates that the component automatically selects the file.
 
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)
Its
Allowedfileslist is the list of file extensions that can be uploaded. Each extension is separated by a comma. To upload files without an extension, you can use two comma-separated tables.
. 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)
Its
Deniedfileslist is a list of file extensions that are not allowed to be uploaded. Each extension is separated by a comma. If you want to disable the upload of files without an extension, you can use two commas (,) for the table.
. 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)
Its
, Contentdisposition is the data to be added. If contentdisposition is null, the component automatically adds
Add "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.
The extension determines what operations to perform. 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
 
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.