Easy implementation with JSP file upload

Source: Internet
Author: User
Tags count file upload implement
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. initializepagecontext//initialization
Mysmartupload. upload//upload ry
Count = Mysmartupload. save″/upload″//to save the uploaded file in the/upload directory. Printlncount +″filesuploaded. ″//shows how many files were uploaded
Catchexception Eout. Printlne. 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 smartuploadmyupload. Initializepagecontextmyupload. Uploadout. 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. Printkey +″=″+values0
%>

3. Other API Myupload. Setallowedfileslist″txtdocxls″

 set the suffix name of the file that can be uploaded; Myupload. Setdeniedfileslist″batexecomjsp″ set the suffix name of the file that cannot be uploaded;

Myupload. Setmaxfilesize100000 sets the maximum number of bytes for a single file;

Myupload. Settotalmaxfilesize100000 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. Getfileid. Getfilename gets the filename of the specified ID number file;

Myupload. Getfiles. Getfileid. Getfilepathname gets the path name of the specified ID number file;

Myupload. Getfiles. Getfile0. 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.




Related Article

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.