Now JSP has been applied by more and more people to the Development of background programs and the production of dynamic web pages. Today we will introduce how to use JSP to upload files.
I. rfc1867 Specification
In the past HTML, forms cannot be used to upload files, which limits the functionality of some web pages. Rfc1867 (form-based file upload in HTML) expands the form and adds a form Element &: ltinput type = File>. By using this element, the browser automatically generates an input box and a button. The input box allows you to enter the local file name and path name. The button allows the browser to open a file selection box for users to select a file. The specific form implementation is as follows:
<Form action = upload. jsp enctype = "multipart/form-Data" method = post>
Please input your name
<Input type = text name = user> <br> please give us the file
<Input type = file name = file1>
<Input type = submit value = "Send File">
</Form>
In particular, only Versions later than Netscape 3 and ie3.02 support this specification. In practice, we found that the file selection button is automatically generated by the browser, therefore, the text of the button has a lot to do with the browser version. For Chinese ie, the text of the button is "Open... "; the English Netscape displays" Browse... "; for opera, only ″... ″. Users cannot set button text by themselves, but it is a pity. In addition to browsers, network servers must also support this specification. Fortunately, most of the server software on the market is normal. Microsoft's IIS also supports this specification well after adding a plug-in.
Ii. Component Installation
After a user uploads a file, JSP is used to process the user's upload request. We can download a free component at www.jspsmart.com to implement this function. Before installing jdk1.2.2 and jsp1.1, make sure that your JSP server software supports jdk1.2.2 and jsp1.1. Then, you only need to add the directory of the component to the class path of the server.
3. Program Implementation
1. File Upload
<% @ Page Language = "Java" Import = "com. jspsmart. Upload." %>
<HTML>
<Body bgcolor = "white">
<H1> jspsmartupload <HR>
<% Int COUNT = 0 // set the variable smartupload mysmartupload = new smartupload
Mysmartupload. initializepagecontext // Initialization
Mysmartupload. Upload // upload ry
Count = mysmartupload. Save "/upload" // Save the uploaded file in the/upload directory out. printlncount + "files uploaded." // display the number of uploaded files
Catch exception e out. printlne. tostring %>
</Body>
</Html>
Note: during debugging, make sure that the directory stored in the uploaded file has writable attributes.
2. Form Processing
Because the form for uploading files uses the enctype = "multipart/form-Data" attribute, you cannot simply use the request when processing the value passed by the form in JSP. getparameter should be processed as follows:
<% @ Page Language = "Java" Import = "com. jspsmart. Upload." %>
<HTML>
<Body bgcolor = "white">
<H1> jspsmartupload <HR>
<% Smartupload myupload = new smartupload myupload. initializepagecontext myupload. Upload out. println "number of files =" + myupload. getfiles. getcount + "<br>" Out. println ″
<Br>
<Br>
<Strong> display information about requests </strong>
<Br> ″
String [] values = myupload. getrequest
. Getparametervalues "formtag" // formtag is the name of the element in the form, and the returned value is the first element in the character array. Out. printkey + "=" + values0
%>
</Body>
</Html>
3. Other API myupload. setallowedfileslist "txtdocxls ″
Set the suffix of the file that can be uploaded; myupload. setdeniedfileslist "batexecomjsp" sets the suffix of the file that cannot be uploaded;
Myupload. setmaxfilesize100000 sets the maximum number of bytes for a single file;
Myupload. settotalmaxfilesize100000 sets the maximum number of bytes for all uploaded files;
Myupload. uploadinfile "/mydata.txt" stores the content of a table in mydata.txt;
Myupload. getfiles. getcount to obtain the number of uploaded files;
Myupload. getfiles. getsize to get the total number of bytes of the uploaded file;
Myupload. getfiles. getfileid. getfilename get the file name of the specified ID number;
Myupload. getfiles. getfileid. getfilepathname to obtain the path name of the specified ID file;
Myupload. getfiles. getfile0.ismissing returns a Boolean value to determine whether the user has uploaded the file.
4. Other functions jspsmartupload not only saves uploaded files to the hard disk, but also directly inserts files into the database and implements the file download function. I believe that with your in-depth research, this function will certainly be better applied to your own practices.