Example of jsp-based AJAX multi-File Upload, jspajax File Upload
In recent project development, I encountered a problem of uploading multiple files, that is, uploading multiple files to the server without refreshing the page. The following is a summary:
This article mainly uses jsp-based ajax, jquery, servlet and other technologies.
1. upload. jsp
When you click upload, call the corresponding fileupload function and send the file to servlet asynchronously through ajax for processing. Note that during file upload, the encoding type used should be "multipart/form-data", which can both send text data and upload binary data.
<% @ 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 "> <Html>
2. PictureServlet. java
Use this servlet to accept the data stream uploaded by jsp, store it in the corresponding path, and parse 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. ht Tp. 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 (HttpS ErvletRequest request, response) */protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stub doPost (request, response );} /*** @ see HttpServlet # doPost (HttpServletRequest request, HttpServletResponse response) */protected void doPost (HttpServletRequest request, HttpServletResponse re Response SE) throws ServletException, IOException {// TODO Auto-generated method stub response. setContentType ("text/xml"); // xml response. setHeader ("Cache-Control", "no-cache"); response. setCharacterEncoding ("UTF-8"); String filepath = "E:/pic/"; // File Upload path. In actual development, the relative path String filename = "" is generally used ""; string name = ""; ServletInputStream in = request. getInputStream (); byte [] buf = new byte [4048]; int len = in. readLi Ne (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 Chinese character garbled problem int j = filename. lastIndexOf ("\" "); int s = filename. indexOf ("filename"); name = filename. substring (s + 10, j); filename = name; // you can obtain the file name System. out. println ("filename =" + filename); DataOutputStream fileStream = new DataOutputStream (new BufferedOutputStream (new FileOutputStream (filepath + filename); len = in. readLine (buf, 0, buf. lengt H); 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 all the content of this article. I hope it will be helpful for your learning and support for helping customers.