To view and select the file to upload, you need the <input type="file"> field on the form. As stated in the HTML specification , you need to use the POST method, and the form's enctype attribute must be set to multipart/form-data .
<form action="uploadServlet" method="post" enctype="multipart/form-data"> <input type="file" name="file" /> <input type="submit" /> </form>
After submitting such a form, the form data is available in multipart format in HttpServletRequest#getInputStream() . For testing purposes (!) You can read the stream using the following snippet:
BufferedReader reader = new BufferedReader(new InputStreamReader(request.getInputStream())); for (String line; (line = reader.readLine()) != null;) { System.out.println(line); }
However, you need to parse the stream byte by byte (instead of char to char). Prior to the new new Servlet 3.0 API, the standard servlet API did not provide any built-in tools for their analysis. Normal form fields are also not available in the usual way request.getParameter() , they are included in the data stream of a multi-page form.
If you are not already on Servlet 3.0 (which is only a bit less than 2 months old), you need to analyze the stream yourself. Analyzing such a stream requires accurate background knowledge of how multiple form data queries are indicated and structured . To create the perfect multi-paragraph parser, you need to write a lot of code. But, fortunately, Apache Commons FileUpload , which has proven its reliability over the years. Read the User Guide and Frequently Asked Questions carefully to find code examples and to learn how to use it to the best extent (note MSIE!).
Balusc
source share