zoukankan      html  css  js  c++  java
  • Java中网络编程实现局域网图片上传

    Java中网络编程实现局域网图片上传

    实现步骤

    这其中省略了许多的细节过程:创建套接字,绑定端口和地址,流的封装/拆封装。

    • 找到文件
    • 提取文件数据
    • 设置接收方文件收取容器
    • 传送文件数据
    • 关闭相关的流
    实现细节(以实现服务器/客户端收发数据为例简单介绍)
    服务端
    • 初始化

      ServerSocket serverSocket = null; // 创建套接字
      InputStream inputStream = null; // 创建接收数据的流
      serverSocket = new ServerSocket(8800);
      
    • 等待连接

      Socket socket = serverSocket.accept(); // 监听客户端
      
    • 处理数据

      inputStream = socket.getInputStream(); // 获取客户端发来的数据
      byte[] bytes = new byte[1024]; // 由于储存客户端发来的数据
      
    • 收尾

    try {
        serverSocket.close();
        inputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    
    
    客户端
    • 创建连接

      Socket socket = new Socket("localhost", 8800);
      
    • 获取/发送数据

      String message = "Java网络编程";
      OutputStream outputStream = socket.getOutputStream();
      outputStream.write(message.getBytes(StandardCharsets.UTF_8));
      socket.shutdownOutput();
      
    • 关闭连接

      socket.close();
      outputStream.close();
      
    高级应用
    • 服务器
    package Network;
    
      import java.io.*;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    public class PictureServer {
        public static void main(String[] args) {
            ServerSocket serverSocket= null;
            Socket socket = null;
            InputStream inputStream = null;
            OutputStream outputStream = null;
            BufferedInputStream bufferedInputStream = null;
            BufferedOutputStream bufferedOutputStream = null;
            try {
                serverSocket = new ServerSocket(9999);
                socket = serverSocket.accept();
                inputStream = socket.getInputStream();
                bufferedInputStream = new BufferedInputStream(inputStream);
                outputStream = new FileOutputStream("3.png");
                bufferedOutputStream = new BufferedOutputStream(outputStream);
                bufferedOutputStream.write(bufferedInputStream.readAllBytes());
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                try {
                    serverSocket.close();
                    socket.close();
                    outputStream.close();
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    
    • 客户端
    package Network;
      
      import java.io.*;
      import java.net.Socket;
      
      public class PictureClient {
          public static void main(String[] args) {
              Socket socket = null;
              InputStream inputStream = null;
              OutputStream outputStream = null;
              BufferedOutputStream bufferedOutputStream = null;
              BufferedInputStream bufferedInputStream = null;
              int read = 0;
              try {
                  socket = new Socket("localhost", 9999);
                  inputStream = new FileInputStream("img.png");
                  bufferedInputStream = new BufferedInputStream(inputStream);
                  outputStream = socket.getOutputStream();
                  bufferedOutputStream = new BufferedOutputStream(outputStream);
                  bufferedOutputStream.write(bufferedInputStream.readAllBytes());
                  socket.shutdownOutput();
              } catch (IOException e) {
                  e.printStackTrace();
              }finally {
                  try {
                      socket.close();
                      inputStream.close();
                      outputStream.close();
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
              }
          }
      }
      
    
  • 相关阅读:
    关键词user附近有语法错误
    Java期末考试冲刺总结
    getElementsByName&getElementById
    window.location.herf传值问题
    三种提示框
    statement没有返回结果集问题
    ajax从jsp向servlet传值
    java.sql.SQLException: Access denied for user ''@'localhost' (using password: NO)报错问题解决
    Unregistering application product with eureka with status DOWN
    输出废弃
  • 原文地址:https://www.cnblogs.com/JonnyJiang-zh/p/14408543.html
Copyright © 2011-2022 走看看