"Java EE Enterprise Application Combat Learning Record" servlet3.0 upload file

Source: Internet
Author: User

1<%--2 Created by IntelliJ.3 User:administrator4Date:2016/10/65Time:14:206To change ThisTemplate Use File | Settings |File Templates.7--%>8<%@ page contenttype= "Text/html;charset=utf-8" language= "java"%>9Ten One<title>upload</title> A -<body> -<form method= "POST" action= "/upload" enctype= "Multipart/form-data" > theFile name: <input type= "text" id= "name" name= "name" ><br/> -Select File: <input type= "file" id= "file" name= "file" ><br/> -<input type= "Submit" value= "Upload" > -</form> +</body> -
1  PackageSanglp.servlet;2 3 Importjavax.servlet.ServletException;4 ImportJavax.servlet.annotation.MultipartConfig;5 ImportJavax.servlet.annotation.WebServlet;6 ImportJavax.servlet.http.HttpServlet;7 Importjavax.servlet.http.HttpServletRequest;8 ImportJavax.servlet.http.HttpServletResponse;9 ImportJavax.servlet.http.Part;Ten Importjava.io.IOException; One ImportJava.io.PrintWriter; A Importjava.util.Collection; -  - /** the * Created by Administrator on 2016/10/6. -  */ -@WebServlet (name = "Upload", Urlpatterns = {"/upload")}) - @MultipartConfig +  Public classUploadservletextendsHttpServlet { - @Override +     protected voidService (HttpServletRequest req, HttpServletResponse resp)throwsservletexception, IOException { A         //Super.service (req, resp); atResp.setcontenttype ("TEXT/HTML;CHARSET=GBK"); -PrintWriter out=Resp.getwriter (); -         //get normal request parameters -String filename=req.getparameter ("name"); -         //get upload domain for uploaded files -Part Part=req.getpart ("File"); inOut.println ("The type of upload file is" +part.getcontenttype () + "<br/>"); -Out.println ("The size of the uploaded file is" +part.getsize () + "<br/>"); to         //gets the file upload header Name +Collection<string> headernames=part.getheadernames (); -          for(String headername:headernames) { theOut.println (headername+ "--" +part.getheader (headername) + "<br/>"); *         } $         //write uploaded files to the serverPanax NotoginsengPart.write (Getservletcontext (). Getrealpath ("/uploadfiles") + "/" +fileName); -     } the}

Java EE Enterprise Application Combat learning record servlet3.0 upload file

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.