Js
Now JSP is more and more people apply in the background program development and Dynamic Web page production, today to introduce how to use JSP to achieve file upload.
First, RFC1867 specifications
In the past HTML, the form does not implement file upload, which limits some of the functionality of some Web pages. The RFC1867 specification, which implements the form-based file upload in HTML, expands the form to add a table cell element &:ltinput type=file>. By using this element, the browser automatically generates an input box and a button that allows the user to fill in the local file name and path name, and the button lets the browser open a File selection box for the user to select the file. The specific form implementation is as follows:
Please input your Name
Please give us the File
Specifically, Netscape 3 and IE3 are the only ones to be noted. 02 later version to support this specification, and we practice to find that the file selection button is automatically generated by the browser, so the button text and browser version has a great relationship, if it is Chinese ie, the button text is "Open ..." "Netscape in English shows" Browse ... If it is opera, then only ″ ... ″。 Users can not set their own button text, can not but say is a kind of regret. In addition to the browser, the network server should be able to support this specification, fortunately, the majority of the market now the server software is not a problem, Microsoft's IIS in addition to a plug-in can be very good support for this specification.
Second, the installation of components
In the client implementation file upload, it is necessary to use JSP to deal with the user's upload request. We can go to www. Jspsmart. COM downloads a free component to implement this feature. Verify that your JSP server software supports JDK1 prior to installation. 2. 2 and JSP1. 1, and then just add the directory that contains the component to the server's class path.
III. implementation of the procedure
1. File Upload
<%@ page language=″java″import=″com. Jspsmart. Upload. ″%>
Jspsmartupload
<% int count=0//setting variable smartupload mysmartupload=new smartupload
mysmartupload. initializepagecontext//initialization
Mysmartupload. upload//upload ry
Count = Mysmartupload. save″/upload″//to save the uploaded file in the/upload directory. Printlncount +″filesuploaded. ″//shows how many files were uploaded
Catchexception Eout. Printlne. tostring%>
Note: When debugging, make sure that the directory where the uploaded file is stored has writable properties.
2. Form processing
Because the upload file's form uses the Enctype=″multipart/form-data″ property, the request can no longer be used simply when processing the value of the form passed in the JSP. GetParameter, and should be treated like this:
<%@ page language=″java″import=″com. Jspsmart. Upload. ″%>
Jspsmartupload
<% smartupload myupload=new smartuploadmyupload. Initializepagecontextmyupload. Uploadout. Println″number of files =″+ myupload. Getfiles. Getcount+″
″out. Println″
display information about requests
″
string[]values = myupload. Getrequest
. Getparametervalues″formtag″//formtag is the name of the element in the form, and the value returned is the first element in the character array. Out. Printkey +″=″+values0
%>
3. Other API Myupload. Setallowedfileslist″txtdocxls″
set the suffix name of the file that can be uploaded; Myupload. Setdeniedfileslist″batexecomjsp″ set the suffix name 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 contents of the form to MyData. TXT save;
Myupload. Getfiles. Getcount get the number of uploaded files;
Myupload. Getfiles. Getsize The total number of bytes uploaded to the file;
Myupload. Getfiles. Getfileid. Getfilename gets the filename of the specified ID number file;
Myupload. Getfiles. Getfileid. Getfilepathname gets the path name of the specified ID number file;
Myupload. Getfiles. Getfile0. Ismissing returns a Boolean value to determine whether the user uploaded the file.
Four, other functions jspsmartupload in addition to the upload file can be saved to the hard disk, but also directly to the file into the database, but also to achieve the function of downloading files, I believe that with the in-depth study, we will be able to better apply this function to their own practice.