One of the new features of Servle 3.0t support for uploading a form

Source: Internet
Author: User

1. Upload

* Upload the requirements for the form:

> method= "POST"

> enctype= "Multipart/form-data", its default value is: application/x-www-form-urlencoded

> <input type= "file" name= "must be given"/>

* Upload servlet for use:

> Request.getparameter () can no longer be used

> Request.getinputstream () use it to get data for the entire form!

To upload a file using commons-fileupload:

* Commons-fileupload

> Create a factory

> Parser

> Use the parser to parse the request object to get list<fileitem>

SERVLET3.0 provides support for uploads:

* The form does not change

* Instead of using commons-fileupload in the servlet, use the Upload component interface provided by Servlet3.0!

Steps to upload:

* Use Request.getpart ("field name") to get the part instance,

* Part:

> String getcontenttype (): Gets the MIME type of the uploaded file

> String getName (): Gets the name of the form item, not the file name

> String GetHeader (String header): Gets the value of the specified header

> Long GetSize (): Gets the size of the uploaded file

> InputStream getinputstream (): Get the contents of the uploaded file

> void Write (String fileName): Save the uploaded file to the specified path

* The default servlet is not supported using the Upload component: You need to add an annotation to the servlet: @MultipartConfig

It does not provide a way to get the name of the uploaded file:

* This requires our own interception from the Content-disposition head!

For example:

Front-end Form Upload Form page: form.jsp

<body>

     <h1> register </h1>

<< Span style= "color: #3f7f7f;" >form action= " <c:url value< Span style= "color:black;" >= '/aservlet ' /> " method= "POST" enctype= " Multipart/form-data " >

user name:

<input type="text" name="username" />

<br />

Brief calendar:

<input type="file" name="Resume" />

<br />

<input type="Submit" value=" Register " />

</form>

</body>

Server side: Aservlet.java

Import Javax.servlet.http.Part;

/*

* default Servlet is not supported using the Upload component: need to give Servlet Add an annotation : @MultipartConfig

*/

@WebServlet(urlpatterns="/aservlet")

@MultipartConfig

Public class aservlet extends httpservlet {

@Override

Public void doPost (httpservletrequest req, HttpServletResponse resp )

throws servletexception, IOException {

req. setcharacterencoding ("UTF-8");

/*

* 1. GetParameter () method can be used!!!

         */

String username = req. GetParameter ("username"); can be used!!!

/*

* 2. Gets the file form fields, corresponding to the Part Object

         */

Part Part = req. GetPart ("Resume");

        

/*

* 3. from Part get the data you need in the

         */

        // get the upload file MIME type

System. out. println (part. getContentType ());

        // Gets the number of bytes uploaded to the file

System. out. println (part. GetSize ());

        // get file Field name

System. out. println (part. GetName ());

        // gets the header that contains the name of the uploaded file

System. Out . println (part. GetHeader ("content-disposition"));

        // Save upload file

part. Write ("c:/xxx.jpg");

        

        // intercept upload file name

String filename = part. GetHeader ("content-disposition");

int start = filename. lastIndexOf ("filename=\" ") + ten;

int End = filename. Length ()-1;

filename = filename. substring (start, end);

System. out. println (filename);

    }

}

One of the new features of Servle 3.0t support for uploading a form

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.