File Upload instance

Source: Internet
Author: User
 

File Upload instance:

JSP page: <% @ page Language ="Java"Import ="Java. util .*"Pageencoding ="UTF-8"%>

<%

String Path = request. getcontextpath ();

String basepath = request. getscheme () + ": //" + request. getservername () + ":" + request. getserverport () + path + "/";

%>

 

<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en">

<HTML>

<Head>

<Base href ="<% = Basepath %>">

<Title> upload page </title>

<Meta http-equiv ="Pragma"Content ="No-Cache">

<Meta http-equiv ="Cache-control"Content ="No-Cache">

<Meta http-equiv ="Expires"Content ="0">

<Meta http-equiv ="Keywords"Content ="Keyword1, keyword2, keyword3">

<Meta http-equiv ="Description"Content ="This is my page">

<! --

<LinkREL="Stylesheet"Type =" text/CSS"
Href= "Styles.css">

-->

 

</Head>

<Body>

<Form action ="$ {Pagecontext. Request. contextpath}/Uploadservlet2"Method ="Post"Enctype ="Multipart/form-Data">

Upload User: <input type ="Text"Name ="User"> <Br>

Upload File 1: <input type ="File"Name ="File1"> <Br>

Upload File 2: <input type ="File"Name ="File2"> <Br>

<Input type ="Submit"Value ="Start upload">

</Form>

<H2 >$ {message} </H2>

</Body>

</Html>

Description file:

<? XML version ="1.0"Encoding =UTF-8"?>

<Web-app version ="2.5"

Xmlns =Http://java.sun.com/xml/ns/javaee"

Xmlns: xsi =Http://www.w3.org/2001/XMLSchema-instance"

Xsi: schemalocation =Http://java.sun.com/xml/ns/javaee

Http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<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> uploadservlet </servlet-Name>

<Servlet-class> com. hbsi. servlet. uploadservlet </servlet-class>

</Servlet>

<Servlet-mapping>

<Servlet-Name> uploadservlet </servlet-Name>

<URL-pattern>/uploadservlet </url-pattern>

</Servlet-mapping>

<Welcome-file-List>

<Welcome-File> index. jsp </welcome-File>

</Welcome-file-List>

</Web-app>

Selvelt:

Package com. hbsi. servlet;

 

Import java. Io. fileoutputstream;

Import java. Io. ioexception;

Import java. Io. inputstream;

Import java. util. List;

 

Import javax. servlet. servletexception;

Import javax. servlet. http. httpservlet;

Import javax. servlet. http. httpservletrequest;

Import javax. servlet. http. httpservletresponse;

 

 

Import org. Apache. commons. fileupload. fileitem;

Import org. Apache. commons. fileupload. fileuploadexception;

Import org. Apache. commons. fileupload. disk. diskfileitemfactory;

Import org. Apache. commons. fileupload. servlet. servletfileupload;

 

 

Public class uploadservlet extends httpservlet {

Private Static final long serialversionuid = 1l;

 

Public void doget (httpservletrequest request, httpservletresponse response)

Throws servletexception, ioexception {

 

Dopost (request, response );

}

 

Public void dopost (httpservletrequest request, httpservletresponse response)

Throws servletexception, ioexception {// upload a file

Try {

// (1) create a factory object fileitem

Diskfileitemfactory factory = new diskfileitemfactory ();

// (2) Get the parser

Servletfileupload upload = new servletfileupload (factory );

Upload. setheaderencoding ("UTF-8 ");

// (3) parse the request. There are several input items and there are several fileitem objects.

List <fileitem> List = upload. parserequest (request );

// (4) iterate the list set to obtain each item in the list set

For (fileitem item: List ){

// Determine the input type

If (item. isformfield ()){

// Normal input

String inputname = item. getfieldname ();

String inputvalue = item. getstring ();

System. Out. println (inputname + "" + inputvalue );

} Else {

// Input for File Upload

String filename = item. getname (); // name of the file to be uploaded

Filename = filename. substring (filename. lastindexof ("\") + 1 );

Inputstream is = item. getinputstream ();

Fileoutputstream Fos = new fileoutputstream ("d :\\" + filename );

Byte [] buff = new byte [1024];

Int Len = 0;

While (LEN = is. Read (buff)> 0 ){

FOS. Write (buff, 0, Len );

}

Is. Close ();

FOS. Close ();

Request. setattribute ("message", "uploaded successfully ");

}

}

} Catch (fileuploadexception e ){

// Todo auto-generated Catch Block

E. printstacktrace ();

Request. setattribute ("message", "Upload Failed ");

}

Request. getrequestdispatcher ("/upload. jsp"). Forward (request, response );

}

 

}

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.