Java 使用socket实现向服务器端上传图片[TCP]

  • 整体思路其实和使用IO流进行文件拷贝类似,只不过这次涉及的流更多。
  • 1、图片属于二进制文件,所以采用字节流处理
  • 2、源图片文件位于客户端主机的磁盘,所以我们需要通过本地IO,将其输入到内存(本地IO)
  • 3、然后将读入内存的文件通过socket的输出流输出到管道(网络IO)
  • 4、在服务器端通过socket的输入流读取管道内的文件到内存
  • 5、把读入内存中的文件写入本地src目录下(本地IO)
  • 6、和文件拷贝类似,整个过程都是“一边读一边写”的,而不是整个读完才写

服务器端代码: Server.java

package socket.tcp.uploadpic;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 任务:客户端向服务器上传一张图片,图片在D:/TestFile/mypic.jpg,要求上传到服务的src/下
 * 服务器收到后向客户端回复
 * 客户端收到回复后退出
 */
public class Server {
   public static void main(String[] args) throws IOException {
      ServerSocket serverSocket = new ServerSocket(9999);
      System.out.println("服务器已启动,等待连接中...");
      Socket socket = serverSocket.accept();
      System.out.println("客户端连接成功!");
      //开始接收数据
      InputStream inputStream = socket.getInputStream();
      BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("src/backa2.jpg"));
      byte[] buf = new byte[1024];
      int readLen = 0;
      while ( (readLen = inputStream.read(buf)) != -1 ) {
         bos.write(buf,0,readLen);
      }
      System.out.println("文件写入完毕");

      //给客户端回复一下
      OutputStream outputStream = socket.getOutputStream();
      outputStream.write("图片已收到!辛苦了".getBytes());
      socket.shutdownOutput();

      inputStream.close();
      bos.close();
      socket.close();
      serverSocket.close();
   }
}

客户端代码:Client.java

package socket.tcp.uploadpic;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;

public class Client {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket(InetAddress.getLocalHost(), 9999);
        System.out.println("服务器连接成功!");

        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:/TestFile/mypic.jpg"));
        OutputStream outputStream = socket.getOutputStream(); //获取socket输出流
        int readLen = 0;
        byte[] buf = new byte[1024];
        while( (readLen = bis.read(buf)) != -1 ) {
            outputStream.write(buf,0,readLen);
        }
        socket.shutdownOutput();
        System.out.println("图片已上传。。。");

        //发送完接收来自服务端的回复
        InputStream inputStream = socket.getInputStream();
        byte[] b = new byte[512];
        int len = 0;
        len = inputStream.read(b);
        System.out.println(new String(b,0,len));
        inputStream.close();

        bis.close();
        outputStream.close();
        socket.close();
    }
}

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以通过以下步骤接收客户端发送的TCP报文: 1. 创建一个ServerSocket对象并绑定端口号。 2. 调用ServerSocket的accept()方法,等待客户端连接请求。 3. 当有客户端连接请求时,accept()方法会返回一个Socket对象,该对象表示与客户端的通信连接。 4. 通过Socket对象的getInputStream()方法获取输入流,可以读取客户端发送的TCP报文。 5. 通过Socket对象的getOutputStream()方法获取输出流,可以向客户端发送TCP报文。 下面是一个简单的Java TCP服务器端的示例代码: ```java import java.io.*; import java.net.*; public class Server { public static void main(String[] args) throws IOException { int port = 8888; ServerSocket serverSocket = new ServerSocket(port); System.out.println("Waiting for client connection..."); Socket clientSocket = serverSocket.accept(); System.out.println("Client connected: " + clientSocket); BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true); String inputLine; while ((inputLine = in.readLine()) != null) { System.out.println("Received message from client: " + inputLine); out.println("Server received message: " + inputLine); } in.close(); out.close(); clientSocket.close(); serverSocket.close(); } } ``` 在上面的示例代码中,我们创建了一个ServerSocket对象,并绑定了端口号8888。然后调用accept()方法等待客户端连接请求。当有客户端连接时,我们获取客户端Socket对象,并通过Socket对象获取输入流和输出流。在while循环中,我们不断读取客户端发送的消息,并向客户端发送回复。最后,我们关闭输入流、输出流和Socket对象,并关闭ServerSocket对象。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值