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