Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convenient way to parse incoming multipart/form-data parameters in a Servlet [duplicate]

Is there any convenient way to read and parse data from incoming request.

E.g client initiate post request

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’m not able to get param using request.getParameter("paramName"). The 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 the content for me

-----------------------------29772313742745 Content-Disposition: form-data; name="name" J.Doe -----------------------------29772313742745 Content-Disposition: form-data; name="email" [email protected] -----------------------------29772313742745 

What is the best way to parse incoming request? I don’t want to write my own parser, probably there is a ready solution.

like image 908
atuser Avatar asked Jul 26 '10 16:07

atuser


People also ask

What is a multipart parser?

The Multipart parser is used for working with the request body in the multipart format. This parser creates a hash table where the names of the request body parameters are the keys and the values of the corresponding parameters are the hash table values.

What is a multipart request in Servlet?

public interface MultipartHttpServletRequest extends HttpServletRequest, MultipartRequest. Provides additional methods for dealing with multipart content within a servlet request, allowing to access uploaded files.

How do you use multipart form data?

Multipart form data: The ENCTYPE attribute of <form> tag specifies the method of encoding for the form data. It is one of the two ways of encoding the HTML form. It is specifically used when file uploading is required in HTML form. It sends the form data to server in multiple parts because of large size of file.

What is multipart request in Java?

Multipart requests combine one or more sets of data into a single body, separated by boundaries. You typically use these requests for file uploads and for transferring data of several types in a single request (for example, a file along with a JSON object).


1 Answers

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

Prior to Servlet 3.0, a de facto standard to parse multipart/form-data requests would be using Apache Commons FileUpload. Just carefully read its User Guide and Frequently Asked Questions sections to learn how to use it. I've posted an answer with a code example before here (it also contains an example targeting Servlet 3.0).

like image 159
BalusC Avatar answered Oct 22 '22 23:10

BalusC