Java:接收多部分 HTTP 响应

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/419850/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 12:18:44  来源:igfitidea点击:

Java: Receive a multipart HTTP response

javahttpmultipart

提问by user52386

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.

我正在编写一个 Java 客户端应用程序来接收来自 IP 摄像机的实时 M-JPEG 视频。视频由摄像机作为无休止的多部分 HTTP 消息发送,其中每个部分都是单个 JPEG 帧。我需要在这些帧到达时对其进行处理,因此我希望有一种方法可以发出 HTTP 请求,在接收到每个消息部分/视频帧时异步触发事件。

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

有没有人知道任何可以做到这一点的图书馆?我能在 Google 上找到的所有示例都不起作用,因为它们使用阻塞调用,这些调用只解析响应,并在接收到整个响应后将其分解为多个部分(这显然不适用于无休止的响应)。

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.

我意识到我可以通过搜索消息边界在数据到达时手动将其分解成部分,但感觉就像我要重新发明轮子。

回答by Josh

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

尝试来自 Apache Commons 的 HttpClient。源代码有几个类展示了如何以流方式读取多部分。

回答by Maurice Perry

I have written classes that process multipart requests for my project Sceye-Fi, an HTTP server that uses the com.sun.net.httpserverclasses that come with java 6, to receive photo uploads from an Eye-Ficard. In theory, nothing should prevent it to work on an endless multipart request. Hope it will help.

我编写了一些类来处理我的项目Sceye-Fi 的多部分请求,Sceye-Fi是一个 HTTP 服务器,它使用com.sun.net.httpserverJava 6 附带的类来接收来自Eye-Fi卡的照片上传。理论上,没有什么可以阻止它处理无休止的多部分请求。希望它会有所帮助。