<! DOCTYPE html>
@MultipartConfig (location = "E:/workspace") @WebServlet (name = "Uploadservlet", Urlpatterns = {"/uploadservlet"}) public class Uploadservlet extends HttpServlet {/** * Processes requests for both HTTP * <code>get</ Code> and * <code>POST</code> methods. * * @param request servlet Request * @param response servlet response * @throws servletexception if a servlet- Specific error occurs * @throws IOException if an I/O error occurs */protected void ProcessRequest (HttpServlet Request request, HttpServletResponse response) throws Servletexception, IOException {request.setcharact Erencoding ("Utf-8"); Iterates all part objects in collection for (section part:request.getParts ()) {//handles only the upload file segment if (Part.getname () . StartsWith ("file")) {String fileName = GetFileName (part); Part.write (FileName); }}} private String GetFileName (part part) { String Header = Part.getheader ("content-disposition"); String fileName = header.substring (Header.indexof ("filename=\") + Ten, header.lastindexof ("\" ")); Header.lastindexof ("\" "); return fileName; }//<editor-fold defaultstate= "collapsed" desc= "httpservlet methods. Click on the + sign on the left to edit the code. >/** * Handles the HTTP * <code>GET</code> method. * * @param request servlet Request * @param response servlet response * @throws servletexception if a servlet- Specific error occurs * @throws IOException if an I/O error occurs */@Override protected void doget (Httpser Vletrequest request, HttpServletResponse response) throws Servletexception, IOException {ProcessRequest (Request, response); }/** * Handles the HTTP * <code>POST</code> method. * * @param request servlet Request * @param response servlet response * @throws ServleTexception If a servlet-specific error occurs * @throws IOException if an I/O error occurs */@Override Prot ected void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, Ioexceptio n {processrequest (request, response); }/** * Returns A short description of the servlet. * * @return A String containing servlet description */@Override public String Getservletinfo () {RET Urn "short description"; }//</editor-fold>}
Upload Multiple files using getparts ()