javaformshttprequestnettyform-processing

Processing Multi-form data in Netty


With a client like this

StringBody body = new StringBody("form_username", Charset.forName("UTF-8"));
multipart.addPart("username", body);
ByteArrayBody bBody = new ByteArrayBody(bs, "form_command.dat");
multipart.addPart("data", bBody);
httppost.setEntity(multipart);

How are the values supposed to be retrieved in the netty server. I already have a HttpRequestDecoder added to the pipeline. And the messageReceived handled thus

HttpRequest request = (HttpRequest) e.getMessage();
    this.mRequest = request;


    if (is100ContinueExpected(request)) {
        send100Continue(e);
    }

    ChannelBuffer content = request.getContent();
    if (content.readable()) {

        System.out.println("Content()\n" + content.toString(CharsetUtil.UTF_8) + "\r\n");


    }

Print outputs .

Content()
--Xdq2t6unVsUp191MKhpR6BXz5P7Eoo
Content-Disposition: form-data; name="username"
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

form_username
--Xdq2t6unVsUp191MKhpR6BXz5P7Eoo
Content-Disposition: form-data; name="data"; filename="form_command.dat"
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary



--Xdq2t6unVsUp191MKhpR6BXz5P7Eoo--


End of contents

Solution

  • You need to use the new HttpPostRequestDecoder. This is only available in the upcoming Netty 3.5 (3 branch) and Netty 4 (master branch).

    Here an example usage.

    If you need to use it now, you can just copy the files mentioned in this pull request into your project namespace and use it.

    Hope this helps.