Socket网络编程练习题四:客户端上传文件(多线程版)

题目

想要服务器不停止,能接收很多客户上传的图片?

解决方案

可以使用循环或者多线程
但是循环不合理,最优解法是(循环+多线程)改写

代码实战

客户端代码
package com.heima;

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

public class Client {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket("127.0.0.1",10000);

        //读取本地文件中的数据,并写到服务器中
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("G:\\wjkwk\\clientdir\\123.jpg"));
        BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());
        byte[] bytes = new byte[1024];
        int len;
        while ((len = bis.read(bytes)) != -1){
            bos.write(bytes,0,len);
        }

        //往服务器写结束标记
        socket.shutdownOutput();

        //接收服务器返回的数据
        BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        String line = br.readLine();
        System.out.println(line);
        socket.close();
    }
}

服务端代码
package com.heima;

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

public class Server {

    public static void main(String[] args) throws IOException {

        ServerSocket serverSocket = new ServerSocket(10000);

        while (true){
            //等待客户端来连接
            Socket socket = serverSocket.accept();
            //开启一个线程
            //一个用户就对应服务端的一条线程
            new Thread(new MyRunnable(socket)).start();
        }
    }
}

package com.heima;

import java.io.*;
import java.net.Socket;
import java.util.UUID;

public class MyRunnable implements Runnable {

    Socket socket;
    public MyRunnable(Socket socket){
        this.socket = socket;
    }
    @Override
    public void run() {

        try {
            //读取客户端上传的文件并保存到本地
            BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
            String name = UUID.randomUUID().toString().replace("-", "");
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("G:\\wjkwk\\serverdir\\"+name+".jpg"));
            int len;
            byte[] bytes = new byte[1024];
            while ((len = bis.read(bytes)) != -1){
                bos.write(bytes,0,len);
            }
            //回写数据
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            bw.write("上传成功");
            bw.newLine();
            bw.flush();

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //释放资源
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

运行结果

Socket网络编程练习题四:客户端上传文件(多线程版)_第1张图片

你可能感兴趣的:(网络编程,网络,TCP,io,网络编程,socket,多线程,客户端)