简体   繁体   English

如何使用字节流发送视频文件?

[英]How to Send a Video file using byte stream?

So, I have this video in .mp4 format and I've converted it into bytes and sent to my server and written the bytes to a file. 因此,我将视频录制为.mp4格式,并将其转换为字节并发送到服务器,然后将字节写入文件中。

When I try to open the new file, it says, 'No Proper Codec found' or something like that. 当我尝试打开新文件时,它说“找不到正确的编解码器”或类似的内容。

So, How do I transfer the video to client with the codec so it can play at my server end. 因此,如何使用编解码器将视频传输到客户端,以便可以在服务器端播放。

Clinet.java Clinet.java

File file = new File("/Users/Batman/Documents/Eclipse/Record/outo.flv");
    InputStream is = new FileInputStream(file);
    OutputStream os = RTSPSocket.getOutputStream();
    long len = file.length();
    byte[] bytes = new byte[(int) len];
    int offset = 0;
    int numRead = 0;
    while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
        offset += numRead;
    }
    String s = String.valueOf(len);
    RTSPBufferedWriter.write(s);
    RTSPBufferedWriter.flush();
        os.write(bytes);
    os.close();
    is.close();

Server.java 服务器.java

inputStream = socket.getInputStream();
                byte[] bytes = new byte[1415874];
                for (int i = 0; i < bytes.length; i++) {
                    fileOutputStream.write(inputStream.read(bytes));
                }
                fileOutputStream.close();
                inputStream.close();

Thanks 谢谢

You're sending the length in ASCII but you're never reading it separately. 您以ASCII格式发送长度,但从未分开读取。 Instead you are assuming a hardwired length of 1415874. So the length is still there in the input and gets written to the target file. 取而代之的是,您假设硬接线的长度为1415874。因此,该长度仍在输入中,并被写入目标文件。

Sending the length in ASCII without a delimiter won't work anyway, as you don't know at the receiving end how long the length is. 没有定界符的ASCII格式的发送长度无论如何都将无法正常工作,因为在接收端您不知道该长度有多长。 You should send the length as a long , using DataOutputStream.writeLong() , and reading it via DataInputStream.readLong() . 您应该使用DataOutputStream.writeLong()将长度发送为long ,并通过DataInputStream.readLong()读取长度。 In fact you should proceed as given in this answer . 实际上,您应该按照此答案中的说明进行操作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM