HttpServletRequest Support for file uploads

Source: Internet
Author: User

Previously, the process of uploading files was always a headache for developers, because the Servlet itself did not provide direct support, needed to be implemented using a third-party framework, and was not easy to use. Servlet 3.0 already provides this functionality and is very simple to use. To do this, HttpServletRequest provides two methods to parse the uploaded file from the request:

? Part GetPart (String name)

? collection<part> getparts ()

The former is used to get the file given name in the request, which is used to get all the files. Each file is represented by a Javax.servlet.http.Part object. This interface provides an easy way to process files, such as write (), delete (), and so on. At this point, combining httpservletrequest and part to save the uploaded file becomes very simple, as follows:

Part photo = Request. GetPart ("photo"), photo. Write ("/tmp/photo.jpg");//You can simplify two lines of code to request . GetPart ("photo"). Write ("/tmp/photo.jpg") line.

In addition, you can customize the upload operation with the @MultipartConfig annotations mentioned earlier, such as restricting the size of the uploaded file, and the path of saving the file. The usage is very simple.

It is important to note that if the requested MIME type is not multipart/form-data, you cannot use the two methods above, or you will throw an exception.

HttpServletRequest Support for file uploads

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.