A convenient way to analyze input parameters of multipart / form-data in a servlet

Is there a convenient way to read and analyze data from an incoming request.

For example, a client request to send messages

URLConnection connection = new URL(url).openConnection(); connection.setDoOutput(true); connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); PrintWriter writer = null; try { OutputStream output = connection.getOutputStream(); writer = new PrintWriter(new OutputStreamWriter(output, charset), true); // true = autoFlush, important! // Send normal param. writer.println("--" + boundary); writer.println("Content-Disposition: form-data; name=\"param\""); writer.println("Content-Type: text/plain; charset=" + charset); writer.println(); writer.println(param); 

I was unable to get the parameter using request.getParameter("paramName") . Following code

 BufferedReader reader = new BufferedReader(new InputStreamReader( request.getInputStream())); StringBuilder sb = new StringBuilder(); for (String line; (line = reader.readLine()) != null;) { System.out.println(line); } 

however displays content for me

 -----------------------------29772313742745 Content-Disposition: form-data; name="name" J.Doe -----------------------------29772313742745 Content-Disposition: form-data; name="email" abuse@spamcop.com -----------------------------29772313742745 

What is the best way to parse an incoming request? I do not want to write my own parser, maybe there is a ready-made solution.

+51
java urlconnection servlets
Jul 26 '10 at 16:57
source share
3 answers

multipart/form-data encoded requests by default are not supported by Servlet API prior to version 3.0. The Servlet API parses the default parameters using application/x-www-form-urlencoded encoding. When using a different encoding, the calls to request.getParameter() all return null . When you're already on Servlet 3.0 ( Glassfish 3 , Tomcat 7 , etc.), you can use HttpServletRequest#getParts() instead. Also see this blog for advanced examples.

Prior to the 3.0 de facto servlet, Apache Commons FileUpload will be used to parse multipart/form-data requests . Just read the User Guide and Frequently Asked Questions sections carefully to learn how to use it. I posted a response with sample code before here (it also contains an example of targeting for Servlet 3.0).

+70
Jul 26 '10 at 17:03
source share

Solutions:

Solution A:

Solution B:

Solution C:

Solution D:

Use Struts. Struts 1.1 handles this automatically.

Link: http://www.jguru.com/faq/view.jsp?EID=1045507

+16
Mar 20 2018-12-12T00:
source share

There is not always a servlet before loading (for example, I could use a filter). Or it may be that the same controller (again a filter or servlet) can perform many actions, so I think that relying on this configuration of the servlet use the getPart method (only for Servlet API> = 3.0), I don’t know I do not like.

In general, I prefer independent solutions that can live alone, in which case http://commons.apache.org/proper/commons-fileupload/ is one of them.

 List<FileItem> multiparts = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request); for (FileItem item : multiparts) { if (!item.isFormField()) { //your operations on file } else { String name = item.getFieldName(); String value = item.getString(); //you operations on paramters } } 
+2
Jun 09 '15 at 12:02
source share



All Articles