Java Web file upload

Source: Internet
Author: User

1. Upload components

① Commons fileupload of Apache

② Javazoom uploadbean

③ Jspsmartupload

 

2. Download The fileupload component of commons.

Address: http://commons.apache.org/fileupload/

Download commons Io Components

Address: http://commons.apache.org/io/

 

3. Use instances

Step 1: create a file upload page: fileupload. jsp

Note the form's enctype attribute, which is important for details, see: http://www.cnblogs.com/yinger/archive/2011/09/11/2173689.html

<% @ Page Language = "Java" Import = "Java. util. *" pageencoding = "UTF-8" %> <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en"> <HTML> 

 

Step 2: Create a servlet for processing file uploads: fileuploadservlet

Package COM. yinger. servlet; import Java. io. file; import Java. io. ioexception; import Java. util. iterator; import Java. util. list; import javax. servlet. servletconfig; import javax. servlet. servletcontext; import javax. servlet. servletexception; import javax. servlet. HTTP. httpservlet; import javax. servlet. HTTP. httpservletrequest; import javax. servlet. HTTP. httpservletresponse; import Org. apache. commons. fileupload. file Item; import Org. apache. commons. fileupload. disk. diskfileitemfactory; import Org. apache. commons. fileupload. servlet. servletfileupload; public class fileuploadservlet extends httpservlet {private servletcontext SC; private string savepath; Private Static final long serialversionuid = encrypt; Public void doget (httpservletrequest request, response) throws servletexception, Ioexception {dopost (request, response);} public void dopost (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {request. setcharacterencoding ("UTF-8"); diskfileitemfactory factory = new diskfileitemfactory (); servletfileupload = new servletfileupload (factory ); // generate a servlet object for processing file uploads through the factory. Try {list items = servletfileupload. parsereque ST (request); // parse request iterator = items. iterator (); While (iterator. hasnext () {fileitem item = (fileitem) iterator. next (); If (item. isformfield () {// form parameter field system. out. println ("form parameter name:" + item. getfieldname () + ", form parameter value:" + item. getstring ("UTF-8");} else {If (item. getname ()! = NULL &&! Item. getname (). equals ("") {// an uploaded file system. out. println ("file name:" + item. getname (); system. out. println ("file size:" + item. getsize (); system. out. println ("file type:" + item. getcontenttype (); file tempfile = new file (item. getname (); // the file name obtained by getname contains the file = new file (SC. getrealpath ("/") + savepath, tempfile. getname (); item. write (File); // write the uploaded file to the file request. setattribute ("message", "File Uploaded successfully! ");} Else {request. setattribute (" message "," No File Uploaded! ") ;}}} Catch (exception e) {e. printstacktrace (); Request. setattribute (" message "," An error occurred while uploading the file! ");} Request. getrequestdispatcher ("/fileuploadresult. JSP "). forward (request, response); // redirection, output result} @ override public void Init (servletconfig config) throws servletexception {savepath = config. getinitparameter ("savepath"); SC = config. getservletcontext ();}}

 

Step 3: modify web. xml

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web=http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd 
Xsi: schemalocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version = "2.5"> <display-Name> </display-Name> <servlet> <description> This is the description of my J2EE component </Description> <display-Name> This is the display name of my J2EE component </display-Name> <servlet-Name> fileuploadservlet </servlet-Name> <servlet-class> COM. yinger. servlet. fileuploadser Vlet </servlet-class> <init-param> <! -- Initialize servlet parameters and configure the storage path of the uploaded file, here is the current application's uplods directory --> <param-Name> savepath </param-Name> <param-value> uploads </param-value> </init-param> </servlet> <servlet-mapping> <servlet-Name> fileuploadservlet </servlet-Name> <URL-pattern>/servlet/fileuploadservlet </url-pattern> </servlet- mapping> <welcome-file-List> <welcome-File> index. JSP </welcome-File> </welcome-file-List> </Web-app>

 

At the same time, create the folder uploads under the root directory of the project (you can create it in webroot)

Step 4: Create a page showing the upload result: fileuploadresult. jsp

<% @ Page Language = "Java" pageencoding = "UTF-8" %> <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en"> <HTML> 

 

Effect display

 

 

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.