Multi-file uploads with Ajax and Servlet implementations

Source: Internet
Author: User
Build the Environment

Here, use Maven to import the jar package:

<dependencies>
  <!--file Upload--
  <dependency>
      <groupid>commons-fileupload</ groupid>
      <artifactId>commons-fileupload</artifactId>
      <version>1.3.2</version >
  </dependency>
  <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-io</artifactId>
      <version>1.3.2</version>
  </dependency >
  <!--json-->
  <dependency>
      <groupId>org.json</groupId>
      < artifactid>json</artifactid>
      <version>20160810</version>
  </dependency>

</dependencies>



Three main jar packages, which will also use jquery and submit forms asynchronously, so jquery and Jquery.form files are required
JSP page

<%--Created by IntelliJ idea. USER:WQH DATE:2017/5/26 time:9:46 To change this template use File | Settings |
File Templates. --%> <%@ page contenttype= "Text/html;charset=utf-8" language= "java"%>  

servlet Code

File upload test, upload files will not be saved to the local L. Returns the file name directly to the page and returns the JSON format.

 Req.setcharacterencoding ("Utf-8");

        Resp.setcharacterencoding ("Utf-8");
        list<string> fileName = new arraylist<> ();
        Boolean ismultipartcontent = Servletfileupload.ismultipartcontent (req);
            Determine if the file is uploaded if (ismultipartcontent) {Diskfileitemfactory factory = new Diskfileitemfactory ();
            Servletfileupload fileUpload = new Servletfileupload (factory);
                try {list<fileitem> fileitems = fileupload.parserequest (req);
                        for (Fileitem fileitem:fileitems) {//To determine the type of field if (Fileitem.isformfield ()) {
                        Working with normal fields} else{//file fields, processing the upload field, this will not save the file locally.
                    Filename.add (Fileitem.getname ());
            }}} catch (Fileuploadexception e) {e.printstacktrace ();
            }//Convert list to JSONint index = 1;
            Jsonarray Jsonarray = new Jsonarray ();
                for (String s:filename) {jsonobject jsonobject = new Jsonobject ();
                Jsonobject.put ("file", s);
            Jsonarray.put (Jsonobject);
        } resp.getwriter (). print (Jsonarray);
 }
Test

Select the uploaded file


File upload test and return JSON format file name successfully:

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.