Servlet for loading multiple parts

How can I upload files and get other form parameters? I want to handle requests for several parts in a Java servlet.

+7
java servlets file-upload
source share
5 answers

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!).

+11
source share

Step 1

Read the adatapost post.

Step 2

Check out the Apache Commons FileUpload project .

There, O'Reily’s workable solution is similar, but its license to use requires you to buy a book, and even this requirement is so poorly worded that I will not use it with another link.

+5
source share

Step 1

set the tag attribute of the enctype attribute form tag.

 <form enctype="multipart/form-data" ....> <input type="file" id="file1" name="file"/> .... other stuff </form> 

Step 2

Read the Justin post.

+2
source share

To handle enctype = "multipart / form-data" , we cannot directly use request.getParameter ()

Now to solve the problem

Now there can be different ways to upload a file to the server. But I'm going to use the MultipartRequest class provided by oreilly. To use this class, you must have the cos.jar file.

 public class UploadServlet extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { MultipartRequest m=new MultipartRequest(request,"d:/new"); out.print("successfully uploaded"); } } 

this will load your file in d: / new

Now, to get the multi-page request parameter, you must use the FilenameUtils class and getOriginalFileName() of the MultipartRequest class method.

 String file = FilenameUtils.getName(req.getOriginalFileName("myfile"))+"\\"; String message = req.getParameter("message"); 
+1
source share

This does not work for IE7 and below. Apparently you need to add another attribute to the form encoding = "multipart / form-data"

0
source share

All Articles