Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Receive a multipart HTTP response

I'm writing a Java client application to receive live M-JPEG video from an IP camera. The video is sent by the camera as an endless multipart HTTP message where each part is a single JPEG frame. I need to process each of these frames as they arrive, so I'm hoping there's a way to make an HTTP request that asynchronously triggers an event as each message part/video frame is received.

Is anyone aware of any libraries that can do this? All the examples I can find on Google won't work because they use blocking calls that only parse the response and break it up into parts after the entire response has finished being received (which obviously won't work for an endless response).

I realise I could manually break up the data into parts as it arrives by searching for the message boundary but it just feels like I would be reinventing the wheel.

like image 886
user52386 Avatar asked Jan 07 '09 10:01

user52386


People also ask

How do you handle a multipart request in Java?

Class MultipartRequest. A utility class to handle multipart/form-data requests, the kind of requests that support file uploads. This class emulates the interface of HttpServletRequest , making it familiar to use. It uses a "push" model where any incoming files are read and saved directly to disk in the constructor.

What is a multipart response?

a multipart request is a REST request containing several packed REST requests inside its entity. a multipart response is a REST response containing several packed REST responses inside its entity.

What is multipart in Java?

Multipart is a container that holds multiple body parts. Multipart provides methods to retrieve and set its subparts. Multipart also acts as the base class for the content object returned by most Multipart DataContentHandlers.

How do you send a multipart file in request body?

To pass the Json and Multipart in the POST method we need to mention our content type in the consume part. And we need to pass the given parameter as User and Multipart file. Here, make sure we can pass only String + file not POJO + file. Then convert the String to Json using ObjectMapper in Service layer.


2 Answers

This project: http://fmj-sf.net, does have a class to parse multipart/x-mixed-replace responses: http://fmj-sf.net/doc/fmj/net/sf/fmj/media/parser/MultipartMixedReplaceParser.html

Searching google code with: multipart/x-mixed-replace lang:java

I found some other examples like: http://www.google.com/codesearch?as_q=multipart%2Fx-mixed-replace&btnG=Search+Code&hl=en&as_lang=java&as_license_restrict=i&as_license=&as_package=&as_filename=&as_case=

http://www.google.com/codesearch/p?hl=en#FCmBlvKk1MA/cambozola-0.50/src/com/charliemouse/cambozola/shared/CamStream.java&q=multipart/x-mixed-replace%20lang:java

http://www.google.com/codesearch/p?hl=en#Xnnd-VJLMBY/src/Grabber.java&q=multipart/x-mixed-replace%20lang:java

like image 58
Loki Avatar answered Sep 25 '22 23:09

Loki


Try HttpClient from Apache Commons. The source code has a couple classes that show how to read in a multipart in a stream fashion.

like image 23
Josh Avatar answered Sep 26 '22 23:09

Josh