最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

网络编程Socket一之上传文件视频

互联网 admin 0浏览 0评论

网络编程Socket一之上传文件视频

通过网络编程,实现从客户端上传视频到服务器端

具体实现:

UploadClient类模拟上传视频端
package com.gxa.upload;import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;public class UploadClient {public static void main(String[] args) throws Exception {//1、创建Socket对象Socket socket = new Socket("127.0.0.1", 8080);//2、读取客户端电脑的视频FileInputStream in = new FileInputStream("F:\\国信安\\资料\\视频\\Video_2020-11-18_102710.wmv");//3、通过socket写出数据到服务器端OutputStream out = socket.getOutputStream();//4、写出数据到服务器端   流的模板代码byte[] buffer = new byte[1024];int len = 0;while ((len = in.read(buffer)) > 0) {out.write(buffer, 0, len);}//关闭socket输出流   确保数据传输完socket.shutdownOutput();//获取服务器端的反馈信息InputStream socketIn = socket.getInputStream();len = socketIn.read(buffer);System.out.println(new String(buffer, 0, len));//关闭资源in.close();out.close();}
}
UploadServer类模拟视频接收端并反馈信息
package com.gxa.upload;import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;public class UploadServer {public static void main(String[] args) throws Exception {//1、创建ServerSocket对象ServerSocket ss = new ServerSocket(8080);//2、获取客户端的socket对象Socket accept = ss.accept();//3、获取客户端的数据InputStream in = accept.getInputStream();//4、往服务器的磁盘上写出数据,即返回数据FileOutputStream out = new FileOutputStream("hello.wmv");//5、流的模板代码byte[] bytes = new byte[1024];int len = 0;while ((len = in.read(bytes)) != -1) {out.write(bytes, 0, len);}//6、反馈上传成功OutputStream socketOut = accept.getOutputStream();socketOut.write("视频上传成功".getBytes());//关闭资源accept.close();ss.close();}
}

结果:

 

 

 

网络编程Socket一之上传文件视频

通过网络编程,实现从客户端上传视频到服务器端

具体实现:

UploadClient类模拟上传视频端
package com.gxa.upload;import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;public class UploadClient {public static void main(String[] args) throws Exception {//1、创建Socket对象Socket socket = new Socket("127.0.0.1", 8080);//2、读取客户端电脑的视频FileInputStream in = new FileInputStream("F:\\国信安\\资料\\视频\\Video_2020-11-18_102710.wmv");//3、通过socket写出数据到服务器端OutputStream out = socket.getOutputStream();//4、写出数据到服务器端   流的模板代码byte[] buffer = new byte[1024];int len = 0;while ((len = in.read(buffer)) > 0) {out.write(buffer, 0, len);}//关闭socket输出流   确保数据传输完socket.shutdownOutput();//获取服务器端的反馈信息InputStream socketIn = socket.getInputStream();len = socketIn.read(buffer);System.out.println(new String(buffer, 0, len));//关闭资源in.close();out.close();}
}
UploadServer类模拟视频接收端并反馈信息
package com.gxa.upload;import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;public class UploadServer {public static void main(String[] args) throws Exception {//1、创建ServerSocket对象ServerSocket ss = new ServerSocket(8080);//2、获取客户端的socket对象Socket accept = ss.accept();//3、获取客户端的数据InputStream in = accept.getInputStream();//4、往服务器的磁盘上写出数据,即返回数据FileOutputStream out = new FileOutputStream("hello.wmv");//5、流的模板代码byte[] bytes = new byte[1024];int len = 0;while ((len = in.read(bytes)) != -1) {out.write(bytes, 0, len);}//6、反馈上传成功OutputStream socketOut = accept.getOutputStream();socketOut.write("视频上传成功".getBytes());//关闭资源accept.close();ss.close();}
}

结果:

 

 

 

发布评论

评论列表 (0)

  1. 暂无评论