An example of Ajax multi-file uploading based on JSP _jsp programming

Source: Internet
Author: User
Tags file upload readline stub

Recent project development, encountered a problem of multiple file uploads, that is, without refreshing the page, upload multiple files to the server. The following are summarized and shared as follows:

This paper mainly adopts the Ajax,jquery,servlet technology based on JSP.

1.upload.jsp

When you click Upload, call the corresponding FileUpload function and asynchronously transfer the file to the servlet via Ajax. Note that when a file is uploaded, the encoding type used should be "Multipart/form-data", which can both send text data and 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 JSP uploaded data stream, stores it to the appropriate path, and resolves the filename.

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 httpservlet#httpservlet () * * * Public pictureservlet () {super (); TODO auto-generated Constructor stub}/** * @see httpservlet#doget (httpservletrequest request, HTTPSERVLETRESPO NSE response) */protected void doget (HttpServletRequestRequest, HttpServletResponse response) throws Servletexception, IOException {//TODO auto-generated method stub D
  Opost (Request,response); /** * @see Httpservlet#dopost (httpservletrequest request, httpservletresponse response) * * protected void Dopo St (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {//TODO Auto-gener
      Ated method Stub Response.setcontenttype ("Text/xml");//Is XML Response.setheader ("Cache-control", "No-cache");
      Response.setcharacterencoding ("UTF-8");
      string filepath = "e:/pic/";//File upload path, in actual development generally use 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 the problem of Chinese character garbled Int j = filename.LastIndexOf ("\");
        int s = filename.indexof ("filename");
        Name=filename.substring (S+10,J);
                                                          
        filename = name;//through the above processing can get uploaded filename System.out.println ("filename=" +filename); DataOutputStream FileStream = new DataOutputStream (new Bufferedoutputstream (New fileoutputs
        Tream (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 (); }
}

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

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.