当前位置: 首页 > news >正文

JAVA客户端发送图片给服务端案例

文章目录

      • 1.客户端发送图片给服务端案例(TCP为例)

1.客户端发送图片给服务端案例(TCP为例)

客户端

public void client() throws IOException {// 1.创建Socket// 指明服务器端的IP地址和端口号InetAddress inetAddress = InetAddress.getByName("192.168.199.191"); // inetAddress变量将存储这个IP地址的封装对象。你可以使用这个InetAddress对象执行网络相关的操作,比如获取IP地址、主机名等信息。int port = 8989;Socket socket = new Socket(inetAddress, port);//2.创建File实例,FileInputStreamFile file = new File("image.png");FileInputStream fis = new FileInputStream(file);//3.通过Socket,获取输出流OutputStream os = socket.getOutputStream();// 读写数据byte[] buffer = new byte[1024];int len;while((len = fis.read(buffer)) != -1){os.write(buffer, 0, len);}System.out.println("数据发送完毕");//4.关闭Socket和相关的流os.close();fis.close();socket.close();}

服务端

@Testpublic void server() throws IOException {// 1.创建ServerSocketint port = 8989;ServerSocket serverSocket = new ServerSocket(port);// 2.接受来自于客户端的socketSocket socket = serverSocket.accept();// 3.通过Socket获取输入流InputStream is = socket.getInputStream();// 4.创建File类的实例,FileOutputStream实例File file = new File("image_111.png");FileOutputStream fos = new FileOutputStream(file);// 5.读写过程byte[] buffer = new byte[1024];int len;while((len = is.read(buffer)) != -1){fos.write(buffer, 0, len);}System.out.println("数据接收完毕");// 6.关闭相关的socket和流fos.close();is.close();socket.close();}

http://www.mrgr.cn/news/30227.html

相关文章:

  • JavaWeb笔记整理——Redis
  • 信息收集常用指令
  • 『功能项目』QFrameWorkBug修改器界面【65】
  • TimeSpan(一个简单的计时器)
  • 佰朔资本:pb和pe是什么?股票pb和pe怎么看?
  • apt-get install 安装的tomcat配置
  • 硬件资源从硬编码到设备树
  • 深入解析NVIDIA GH200:Grace Hopper的多样性与性能挑战
  • 基于SpringBoot+Vue的篮球馆会员信息管理系统
  • nodejs child_process 操作git 提交记录 提取git commit信息
  • Vue:加载本地视频
  • C++和OpenGL实现3D游戏编程【连载9】——纹理的镂空显示
  • 电子书号和纸质书号的ISBN 号有什么不同?
  • 《高等代数》行列式转置(应用)
  • 如何让零售巨头在营销拓客中旗开得胜?新增企业API自动输出客户名单!
  • git 压栈存储当前分支修改,出栈使用保存
  • Fiddler 工具使用教程
  • platform
  • Linux系统查看硬件配置教程
  • Android U 多任务启动分屏——Launcher流程(下分屏 更新中)