Implementation of AJAX multi-file upload based on JSP

Source: Internet
Author: User

Recent project development, encountered a multi-file upload problem, that is, without refreshing the page, upload multiple files to the server. The following summary is shared:

This paper mainly uses JSP-based Ajax,jquery,servlet and other technologies.

1.upload.jsp

When you click Upload, the corresponding fileupload function is called, and the file is asynchronously routed through Ajax to the servlet for processing. Note When a file is uploaded, the type of encoding used should be "Multipart/form-data", which can either send text data or support binary data uploads.

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "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" "Http://www.w3.org/TR/html4/loose.dtd" >

2.pictureservlet.java

The servlet accepts the data stream uploaded by the JSP, stores it to the appropriate path, and resolves the file name.

Package Com.servlet;import Java.io.bufferedoutputstream;import Java.io.dataoutputstream;import Java.io.fileoutputstream;import Java.io.ioexception;import Java.io.printwriter;import Javax.servlet.servletexception;import Javax.servlet.servletinputstream;import Javax.servlet.annotation.webservlet;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import com.realty.base.action.buildingaction;/** * Servlet Implementation class Pictureservlet */@WebServlet ("/                                                                                                           Pictureservlet ") public class Pictureservlet extends HttpServlet {private static final long serialversionuid = 1L; /** * @see Http        Servlet#httpservlet () */public Pictureservlet () {super (); TODO auto-generated Constructor stub}/** * @see httpservlet#doget (httpservletrequest request, Httpservletre   Sponse response)  */protected void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, Ioexceptio    n {//TODO auto-generated Method Stub doPost (request,response); }/** * @see httpservlet#dopost (httpservletrequest request, httpservletresponse response) */protected void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {//TODO Aut o-generated method Stub Response.setcontenttype ("Text/xml");//is an XML Response.setheader ("Cache-control",            "No-cache");            Response.setcharacterencoding ("UTF-8");            string filepath = "e:/pic/";//File upload path, actual development in general with relative path String filename = "";            String name= "";            ServletInputStream in = Request.getinputstream ();            byte[] buf = new byte[4048];            int len = in.readline (buf, 0, buf.length);            String f = new string (buf, 0, len-1); while (len = In.readline (buF, 0, buf.length))! =-1) {filename = new String (buf, 0, Len, "Utf-8");//solve Chinese character garbled problem int J = fi                Lename.lastindexof ("\" ");                int s = filename.indexof ("filename");                Name=filename.substring (S+10,J);                                                                                                                                   filename = name;//The uploaded file name System.out.println ("Filename=" +filename) can be obtained by the above processing; DataOutputStream file                Stream = new DataOutputStream (new Bufferedoutputstream (New FileOutputStream (filepath+ filename));                Len = in.readline (buf, 0, buf.length);                Len = in.readline (buf, 0, buf.length);                    while (len = in.readline (buf, 0, buf.length))! =-1) {String Tempf = new String (buf, 0, len-1);                    if (Tempf.equals (f) | | Tempf.equals (f + "--")) {break;         } else{                FileStream.Write (buf, 0, Len);            Write}} filestream.close ();            } printwriter Out=response.getwriter ();            String result = filename;            Out.print (result);            Out.close ();                                                  In.close ();                                                                              }}

Note the introduction of the relevant jar package, see attachment.

This article is from the Java My Favorites blog, so be sure to keep this source http://lindianli.blog.51cto.com/7129432/1409559

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.