Use the fileupload component to upload files

Source: Internet
Author: User
File Upload is very common in Web applications. It is very easy to implement the file upload function in the JSP environment, because there are many File Upload components developed using Java on the Internet, this document uses the commons-fileupload component as an example to add the file upload function for JSP applications.
The common-fileupload component is one of Apache's open-source projects. It can be downloaded from http://jakarta.apache.org/commons/fileupload. You can use this component to upload one or more files at a time and limit the file size.
Download and decompress the zip package, copy the commons-fileupload-1.0.jar to Tomcat's webapps \ your webapp \ WEB-INF \ Lib \, the directory does not exist please self-built directory.
Create a servlet: Upload. Java for file upload:
Import java. Io .*;
Import java. util .*;
Import javax. servlet .*;
Import javax. servlet. http .*;
Import org. Apache. commons. fileupload .*;

Public class upload extends httpservlet {

Private string uploadpath = "C: \ upload \"; // directory of the uploaded file
Private string temppath = "C: \ upload \ TMP \"; // temporary file directory

Public void dopost (httpservletrequest request,
httpservletresponse response)
throws ioexception, servletexception
{< BR >}< br> In the dopost () method, when the servlet receives a POST request from the browser, it uploads the file. The following is an example of Code :
Public void dopost (httpservletrequest request,
httpservletresponse response)
throws ioexception, servletexception
{< br> try {
diskfileupload Fu = new diskfileupload ();
// set the maximum file size, 4 MB
Fu. setsizemax (4194304);
// set the buffer size. Here it is 4 kb
Fu. setsizethreshold (4096);
// set the temporary directory:
Fu. setrepositorypath (temppath);

// Obtain all objects:
List fileitems = Fu. parserequest (request );
Iterator I = fileitems. iterator ();
// Process each file in sequence:
While (I. hasnext ()){
Fileitem Fi = (fileitem) I. Next ();
// Get the file name, which includes the path:
String filename = Fi. getname ();
// User and file information can be recorded here
//...
// Write the file. The temporary file name is a.txt. You can extract the file name from filename:
Fi. Write (new file (uploadpath + "a.txt "));
}
}
Catch (exception e ){
// Jump to the error page
}
}
To read the specified upload folder in the configuration file, run the following command in the init () method:
Public void Init () throws servletexception {
Uploadpath = ....
Temppath = ....
// The folder is automatically created if it does not exist:
If (! New file (uploadpath). isdirectory ())
New file (uploadpath). mkdirs ();
If (! New file (temppath). isdirectory ())
New file (temppath). mkdirs ();
}
Compile the servlet, note that you need to specify classpath to ensure that the commons-upload-1.0.jar and tomcat \ common \ Lib \ servlet-api.jar are included.
Configure servlet, open Tomcat \ webapps \ your webapp \ WEB-INF \ Web. XML with notepad, if there is no new one.
The typical configuration is as follows:
<? XML version = "1.0" encoding = "ISO-8859-1"?>
<! Doctype web-app
Public "-// Sun Microsystems, Inc. // DTD web application 2.3 // en"
Http://java.sun.com/dtd/web-app_2_3.dtd>

<Web-app>
<Servlet>
<Servlet-Name> upload </servlet-Name>
<Servlet-class> upload </servlet-class>
</Servlet>


upload
/fileupload


After configuring the servlet, start Tomcat and write a simple HTML test:

enctype = "multipart/form-Data" name = "form1">



note action = "fileupload" where fileupload is the URL-pattern specified during servlet configuration

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.