Organizational Research By

Surprising Reserch Topic

convenient way to parse incoming multipart form data parameters in a servlet


convenient way to parse incoming multipart form data parameters in a servlet  using -'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);

,,}

asked Sep 16, 2015 by yashwantpinge
edited Sep 16, 2015 by rajesh
0 votes
6 views



Related Hot Questions

3 Answers

0 votes

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

answered Sep 16, 2015 by virendra.bajaj
0 votes

Solutions:

Solution A:

  1. Download http://www.servlets.com/cos/index.html
  2. Invoke getParameters() on com.oreilly.servlet.MultipartRequest

Solution B:

  1. Download http://jakarta.Apache.org/commons/fileupload/
  2. Invoke readHeaders() in org.apache.commons.fileupload.MultipartStream

Solution C:

  1. Download http://users.boone.net/wbrameld/multipartformdata/
  2. Invoke getParameter on com.bigfoot.bugar.servlet.http.MultipartFormData

Solution D:

Use Struts. Struts 1.1 handles this automatically.

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

answered Sep 16, 2015 by android_master
0 votes

Not always there's a servlet before of an upload (I could use a filter for example). Or could be that the same controller ( again a filter or also a servelt ) can serve many actions, so I think that rely on that servlet configuration to use the getPart method (only for Servlet API >= 3.0), I don't know, I don't like.

In general, I prefer indipendent solutions, able to live alone, and in this case http://commons.apache.org/proper/commons-fileupload/ is one of that.

List 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
        }
}
answered Sep 16, 2015 by girisha

...