Enctype= "Multipart/form-data", Servlet gets parameters

Source: Internet
Author: User
Tags html form
The HTML Form Enctype property is a MIME encoding that sets the transfer of a form, with a total of three values to choose from:


①application/x-www-form-urlencoded (default value, cannot be used for file uploads)

②multipart/form-data (Specifies that the transmitted data is a binary type, such as a picture, MP3, file)

③text/plain (pure text transmission, send mail, receive will not appear garbled)


uploaddemo.jsp

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%> <!
DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >


Servletuploaddemo.java


Package com.text;
Import java.io.IOException;
Import javax.servlet.ServletException;
Import Javax.servlet.annotation.MultipartConfig;
Import Javax.servlet.annotation.WebServlet;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;

Import Javax.servlet.http.Part;
Servlet3.0+ can be mapped using annotation mode instead of web.xml files.
File upload is servlet3.0+ must have. Part: interface. Represents each item in the form. Equivalent to the Fileitem @WebServlet ("/servlet/servletuploaddemo") @MultipartConfig//** plus this annotation, the reflection of the servlet to know that the process is file upload public Class Servletuploaddemo extends HttpServlet {public void doget (HttpServletRequest request, HttpServletResponse Respon SE) throws Servletexception, IOException {//Normal parameters can also be removed as usual (@MultipartConfig not forgotten) String name = RE
        Quest.getparameter ("name");
        String age = request.getparameter (' age ');
Special parameters with part fetch part part = Request.getpart ("file");
System.out.println (name+ "--" +age); SYSTEM.OUT.PRINTLN (parT);
        Get filename String contentdisposition = Part.getheader ("content-disposition"); System.out.println (contentdisposition);//Form-data; Name= "File";
        Filename= "User.sql" int filenameindex = Contentdisposition.indexof ("filename=");

        String filename = contentdisposition.substring (filenameindex+10, Contentdisposition.length ()-1);
    Part.write (Getservletcontext (). Getrealpath ("/files") + "/" +filename); public void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, I
    oexception {doget (request, response);
 }

}


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.