Apache Commons fileupload Implementation file upload

Source: Internet
Author: User
Tags file upload static class

The Apache Commons-fileupload.jar is placed under the web-inf\lib of the application and can be used. The following example describes how to use its file upload feature.

The FileUpload version used is 1.2 and the environment is eclipse3.3+myeclipse6.0. FileUpload is based on Commons io, so make sure that the Commons IO jar package (used Commons-io-1.3.2.jar) is under Web-inf\lib before entering the project.

This article is an example project that can be downloaded in the final attachment of the article.

Example 1

In the simplest case, by servletfileupload the static class to parse the request, the factory class Fileitemfactory will handle all the fields in the Mulipart class's form, not just the file field. GetName () Gets the file name, getString () Gets the form data content, Isformfield () can determine whether it is a normal form item.

Demo1.html

   <meta http-equiv="Content-Type" content="text/html; charset=GB18030">
   <title>File upload</title>
<body>
    //必须是multipart的表单数据。
   <form name="myform" action="demo1.jsp" method="post"
    enctype="multipart/form-data">
    Your name: <br>
    <input type="text" name="name" size="15"><br>
    File:<br>
    <input type="file" name="myfile"><br>
    <br>
    <input type="submit" name="submit" value="Commit">
   </form>
</body>

demo1.jsp

<%@ page language= "java" contenttype= "text/html; charset=gb18030"
pageencoding= "GB18030"%>
<%@ page import= "org.apache.commons.fileupload.*"%>
<%@ page import= org.apache.commons.fileupload.servlet.* "%>
<%@ page import=" org.apache.commons.fileupload.disk.* "%>
<%@ page import= "java.util.*"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en"
<%
Boolean ismultipart = Servletfileupload. Ismultipartcontent (Request)//Check whether the input requests are multipart form data.
if (Ismultipart = = True) {
Fileitemfactory factory = new Diskfileitemfactory ()//Create a diskfileitemfacto for the request Ry object to parse the request. After parsing is performed, all form items are saved in a list.
Servletfileupload upload = new Servletfileupload (factory);
list<fileitem> items = upload.parserequest (request);
Iterator<fileitem> ITR = Items.iterator ();      
while (Itr.hasnext ()) {
Fileitem item = (Fileitem) itr.next ();
Check if the current item is a plain form item orPass the file.
if (Item.isformfield ()) {//If it is a plain form item, display the contents of the form.
String fieldName = Item.getfieldname ();
if (fieldname.equals ("name"))//Correspondence demo1.html type= "text" name= "name"
Out.print ("The field name is" + IT Em.getstring ());//Displays the contents of the form.
Out.print ("<br>");
} else {//If the file is uploaded, displays the file name.
Out.print ("The Upload file name is" + item.getname ());
Out.print ("<br>");   
}
}
} else {
Out.print ("The enctype must be Multipart/form-data");

%>
<meta http-equiv= "Content-type" content= text/html; charset= GB18030 "
<title>file upload</title>
<body>
</body>

Results:

The field name Isjeff

The upload file name isd:\c language Exam sample/job title. rar

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.