[英]refresh servlet jetty java
I have a video stream from an ip-camera and I want to handle this stream via a Server so i can display it on as many devices (like iPads / Browsers) as I need (camera only has 100Mbit/s so a lot of the devices don't show anything). 我有一个来自IP摄像机的视频流,我想通过服务器处理该流,因此我可以根据需要在任意数量的设备(例如iPad /浏览器)上显示它(摄像机只有100Mbit / s,所以很多设备不显示任何内容)。 I have a jetty http-Server running. 我有一个运行中的码头HTTP服务器。 I wrote a class which gets the stream and converts it to a MjpegFrame: 我写了一个获取流并将其转换为MjpegFrame的类:
MjpegFrame = frame;
try {
MjpegInputStream m = new MjpegInputStream(url.openStream());
MjpegFrame f;
while ((f = m.readMjpegFrame()) != null) {
if(!running) break;
frame = f;
}
m.close();
} catch (IOException e) {
//some error outputs
}
get the current frame 获取当前帧
public MjpegFrame getCurrentFrame() {
return frame;
}
This works fine. 这很好。 Now I am trying to display it with my Servlet, but here I only get a single photo instead of a stream: 现在,我试图用Servlet显示它,但是在这里,我只得到一张照片而不是流:
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//String auth = request.getAuthType();
//System.out.println("auth:"+auth);
if(vm != null) {
MjpegFrame frame = vm.getCurrentFrame();
if(frame != null) {
BufferedOutputStream output = null;
try{
output = new BufferedOutputStream(response.getOutputStream(), 1024);
response.reset();
response.setBufferSize(1024);
response.setContentType("image/webp");
response.setHeader("Cache-Control", "max-age=0") ;
response.setHeader("Accept-Encoding", "gzip, deflate, sdch");
while(frame != null){
response.setContentLength(frame.getContentLength());
output.write(frame.getJpegBytes(), 0, frame.getContentLength());
frame = vm.getCurrentFrame();
}
}catch(Exception e){
e.printStackTrace();
}finally {
}
} else {
System.out.println("No image available...");
}
} else {
System.out.println("Error: VideoMultiplier is not set");
}
}
Does anyone know what is wrong with my code? 有人知道我的代码有什么问题吗?
Solved it by myself: 由我自己解决:
the problem was the Conent-Type 问题是同意类型
response.setContentType("image/webp");
I used wireshark to analyse it and realised the response should look different. 我使用wireshark对其进行了分析,并意识到响应看起来应该有所不同。 Anyway, thats my response: 无论如何,这就是我的回应:
String contentType = "multipart/x-mixed-replace; boundary=--yourboundary";
response.setContentType(contentType);
and instead of --yourboundary you use the boundary from the camera, or, to make it more flexible, build your own header: 而不是--yourboundary,而是使用摄像机的边界,或者为了使其更加灵活,请构建自己的标头:
public StringBuffer createHeader(int contentLength) {
StringBuffer header = new StringBuffer(100);
header.append("--yourboundary\r\nContent-Type: image/jpeg\r\nContent-Length: ");
header.append(contentLength);
header.append("\r\n\r\n");
return header;
}
and then write it like this: 然后这样写:
frame = vm.getCurrentFrame();//here I get my frame of the current image I wanna send
StringBuffer header = createHeader(frame.getJpegBytes().length);
byte[] headerBytes = header.toString().getBytes();
byte[] imageBytes = frame.getJpegBytes();
// create a newImage array that is the size of the two arrays
byte[] newImage = new byte[headerBytes.length + imageBytes.length];
// copy headerBytes into start of newImage (from pos 0, copy headerBytes.length bytes)
System.arraycopy(headerBytes, 0, newImage, 0, headerBytes.length);
// copy imageBytes into end of newImage (from pos headerBytes.length, copy imageBytes.length bytes)
System.arraycopy(imageBytes, 0, newImage, headerBytes.length, imageBytes.length);
output.write(newImage,0,newImage.length);
output.flush();
hope it helps someone. 希望它可以帮助某人。 cheers 干杯
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.