我基本上想要的是简单的视频文件传输:当我按下按钮时,客户端会将文件发送到服务器,然后服务器再次将文件发送回客户端。
我现在得到的只是从客户端发送到服务器,服务器接收它。
以下是一些代码:
客户:
System.out.println("Connecting...");
sock = new Socket(IP, PORT);
InputStream is = new FileInputStream(new File("FILE PATH"));
byte[] bytes = new byte[1024];
OutputStream stream = sock.getOutputStream();
int count = is.read(bytes, 0, 1024);
while (count != -1) {
stream.write(bytes, 0, 1024);
count = is.read(bytes, 0, 1024);
}
is.close();
stream.close();
sock.close();
System.out.println("2");
服务器:
byte[] data = new byte[1024];
int count = fin.getInputStream().read(data, 0, 1024);
System.out.println("Receiving video...");
File video = new File("test.mp4");
FileOutputStream fos = new FileOutputStream(video);
while (count != -1) {
fos.write(data, 0, count);
count = fin.getInputStream().read(data, 0, 1024);
}
fos.close();
fin.close();
System.out.println("Done receiving");
感谢。