如何使用字节流发送视频文件?
How to Send a Video file using byte stream?
所以,我有这个 .mp4 格式的视频,我已将其转换为字节并发送到我的服务器并将字节写入文件。
当我尝试打开新文件时,它显示 'No Proper Codec found' 或类似的内容。
那么,如何使用编解码器将视频传输到客户端,以便它可以在我的服务器端播放。
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
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();
谢谢
您发送的是 ASCII 格式的长度,但您从未单独读取它。相反,您假设硬连线长度为 1415874。因此该长度仍存在于输入中并写入目标文件。
在没有定界符的情况下发送 ASCII 格式的长度无论如何都行不通,因为您在接收端不知道长度有多长。您应该将长度作为 long
发送,使用 DataOutputStream.writeLong()
,并通过 DataInputStream.readLong()
读取它。事实上,您应该按照 this answer.
中的说明进行操作
所以,我有这个 .mp4 格式的视频,我已将其转换为字节并发送到我的服务器并将字节写入文件。
当我尝试打开新文件时,它显示 'No Proper Codec found' 或类似的内容。
那么,如何使用编解码器将视频传输到客户端,以便它可以在我的服务器端播放。
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
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();
谢谢
您发送的是 ASCII 格式的长度,但您从未单独读取它。相反,您假设硬连线长度为 1415874。因此该长度仍存在于输入中并写入目标文件。
在没有定界符的情况下发送 ASCII 格式的长度无论如何都行不通,因为您在接收端不知道长度有多长。您应该将长度作为 long
发送,使用 DataOutputStream.writeLong()
,并通过 DataInputStream.readLong()
读取它。事实上,您应该按照 this answer.