Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/252.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
Streaming 我已经为mjpeg创建了一个测试服务器,但它只显示第一帧_Streaming_Video Streaming_Jpeg_Mjpeg - Fatal编程技术网

Streaming 我已经为mjpeg创建了一个测试服务器,但它只显示第一帧

Streaming 我已经为mjpeg创建了一个测试服务器,但它只显示第一帧,streaming,video-streaming,jpeg,mjpeg,Streaming,Video Streaming,Jpeg,Mjpeg,我用java创建了一个服务器,用于通过IP传送MJPEG内容。 下面是代码 package mjpeg; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import java.nio.file.

我用java创建了一个服务器,用于通过IP传送MJPEG内容。 下面是代码

package mjpeg;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.Path;


public class MjpegStreamServer {

    private static BufferedReader in ;
    public static void main(String[] args) throws InterruptedException {
        try {
            ServerSocket sSock = new ServerSocket(8080);
            Socket s = sSock.accept();


            in = new BufferedReader(new InputStreamReader(s.getInputStream()));
            PrintWriter out = new PrintWriter(s.getOutputStream());

            // Reader thread
            Thread t = new Thread(new Runnable() {

                @Override
                public void run() {
                    String userInput;
                    try {
                        while ((userInput = in.readLine()) != null) {
                            System.out.println(userInput);
                        }
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                }
            });

            t.start();
            //

            //Writer main thread
            Path path = Paths.get("D:\\Image\\i.jpg");
            byte[] data = Files.readAllBytes(path);
            //data1  => "D:\\Image\\j.jpg



            String response;
            response = "HTTP/1.1 200 OK\r\nContent-Type: multipart/x-mixed-replace; boundary=--myboundary\r\n";
            out.print(response);
            int i = 0;
            int length = data.length;
            // length1 => data1.length

            while (i < 1000) {
                if (i % 2 == 0) {
                    String header = "--myboundary\r\n"
                            + "Content-Type:image/jpeg\r\n" + "Content-Length:"
                            + length + "\r\n\r\n";
                    out.print(header);
                    s.getOutputStream().write(data);
                } else {
                    String header = "--myboundary\r\n"
                            + "Content-Type:image/jpeg\r\n" + "Content-Length:"
                            + length1 + "\r\n\r\n";
                    out.print(header);
                    s.getOutputStream().write(data1);
                }
                out.print("\r\n");
                out.flush();
                i++;
                Thread.sleep(100);

            }
            sSock.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
package;
导入java.io.BufferedReader;
导入java.io.IOException;
导入java.io.InputStreamReader;
导入java.io.PrintWriter;
导入java.net.ServerSocket;
导入java.net.Socket;
导入java.nio.file.Files;
导入java.nio.file.path;
导入java.nio.file.Path;
公共类服务器{
中的专用静态缓冲读取器;
公共静态void main(字符串[]args)引发InterruptedException{
试一试{
ServerSocket sSock=新的ServerSocket(8080);
套接字s=sSock.accept();
in=新的BufferedReader(新的InputStreamReader(s.getInputStream());
PrintWriter out=新的PrintWriter(s.getOutputStream());
//读线程
线程t=新线程(新的可运行线程(){
@凌驾
公开募捐{
字符串用户输入;
试一试{
而((userInput=in.readLine())!=null){
System.out.println(用户输入);
}
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
});
t、 start();
//
//书写器主线程
Path Path=Path.get(“D:\\Image\\i.jpg”);
字节[]数据=文件。readAllBytes(路径);
//数据1=>“D:\\Image\\j.jpg
字符串响应;
response=“HTTP/1.1 200确定\r\n内容类型:multipart/x-mixed-replace;boundary=--myboundary\r\n”;
打印(响应);
int i=0;
int length=data.length;
//length1=>data1.length
而(i<1000){
如果(i%2==0){
字符串头=“--myboundary\r\n”
+“内容类型:图像/jpeg\r\n”+“内容长度:”
+长度+“\r\n\r\n”;
打印输出(页眉);
s、 getOutputStream().write(数据);
}否则{
字符串头=“--myboundary\r\n”
+“内容类型:图像/jpeg\r\n”+“内容长度:”
+长度1+“\r\n\r\n”;
打印输出(页眉);
s、 getOutputStream().write(数据1);
}
输出。打印(“\r\n”);
out.flush();
i++;
睡眠(100);
}
sSock.close();
}捕获(IOE异常){
e、 printStackTrace();
}
}
}
这将交替发送两个图像, 我用浏览器进行了测试,浏览器只显示第一帧

记录的输出为

HTTP/1.1 200 OK
Content-Type: multipart/x-mixed-replace; boundary=--myboundary

--myboundary
Content-Type:image/jpeg
Content-Length:71327

<JPEG Data>
--myboundary
Content-Type:image/jpeg
Content-Length:60808

<JPEG Data>
HTTP/1.1200正常
内容类型:multipart/x-mixed-replace;boundary=--myboundary
--我的边界
内容类型:图像/jpeg
内容长度:71327
--我的边界
内容类型:图像/jpeg
内容长度:60808

你知道这里有什么问题吗?

定义的边界应该以
--
作为前缀,所以如果你的边界是
--myboundary
,你应该写
--myboundary
。或者将其定义为
myboundary
啊!谢谢!这很有效!