comsat icon indicating copy to clipboard operation
comsat copied to clipboard

Read File from HttpRequest

Open safar opened this issue 9 years ago • 2 comments

I have mixed form with text and file input fields which is being processed by a WebActor. I have to be able to read text input fields by their key and File contents from the same message.

I have used code below to read sent request using WebActor using Netty:

if (message instanceof HttpRequest) {
      final HttpRequest msg = (HttpRequest) message;
      logger.info("msg: {}", new String(msg.getByteBufferBody().array(),
            Charset.forName("UTF-8")));
}

this is the output

------WebKitFormBoundaryRJbBHRjR4HKByqsl
Content-Disposition: form-data; name="test_key"

test_value
------WebKitFormBoundaryRJbBHRjR4HKByqsl
Content-Disposition: form-data; name="file"; filename="accounts.png"
Content-Type: image/png

�PNG

\��q$Z5��X� ߀DDD$;��L�ܝm8|�J
... omitted binary gibberish 
��    IEND�B`�
------WebKitFormBoundaryRJbBHRjR4HKByqsl--

Are we intended to parse these messages manually? Is there a method which can read File part of HttpRequest directly which I'm unaware of? What is the easiest way to read a file from a multipart HttpRequest received by a WebActor?

safar avatar Nov 15 '16 10:11 safar

Multipart parsing is currently not natively supported by Web Actors. You have a few options: you can either parse the buffer using a library such as Apache Commons FileUpload, or you can handle the upload in a different servlet resource and use Servlet 3's native support for multipart.

Should Web Actors support multipart natively? Is it a right fit for the actor paradigm? Is using a separate servlet for uploads not a good solution?

pron avatar Nov 15 '16 17:11 pron

I don't see big advantages offered by specifically supporting multipart in Web Actors. @safar Why would you like to use Web Actors specifically in this case and won't some other method do? For example Netty seems to have a dedicated handler.

circlespainter avatar Dec 12 '16 17:12 circlespainter