Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/extjs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
刷新servlet jetty java_Java_Servlets_Jetty - Fatal编程技术网

刷新servlet jetty java

刷新servlet jetty java,java,servlets,jetty,Java,Servlets,Jetty,我有一个来自ip摄像头的视频流,我想通过服务器处理这个流,这样我就可以在尽可能多的设备(如iPad/浏览器)上显示它,因为我需要摄像头只有100Mbit/s,所以很多设备什么都不显示。我有一个jetty http服务器正在运行。我编写了一个类,用于获取流并将其转换为MjpegFrame: MjpegFrame = frame; try { MjpegInputStream m = new MjpegInputStream(url.openStream

我有一个来自ip摄像头的视频流,我想通过服务器处理这个流,这样我就可以在尽可能多的设备(如iPad/浏览器)上显示它,因为我需要摄像头只有100Mbit/s,所以很多设备什么都不显示。我有一个jetty http服务器正在运行。我编写了一个类,用于获取流并将其转换为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
        }
获取当前帧

 public MjpegFrame getCurrentFrame() {
     return frame;
 }
这个很好用。现在我试图用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");
    }

}
有人知道我的代码有什么问题吗?

我自己解决了:

问题是康奈特式的

 response.setContentType("image/webp");
我用wireshark对其进行了分析,并意识到反应应该有所不同。不管怎样,这就是我的回答:

String contentType = "multipart/x-mixed-replace; boundary=--yourboundary";
response.setContentType(contentType); 
而不是-yourbound,您使用相机的边界,或者,为了使其更灵活,构建您自己的标题:

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;
}
然后这样写:

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();
希望它能帮助别人。 cheers

接受编码不是响应标题。此外,您可能还需要查看Servlet3.1的WriteListener以进行异步响应写入。