11月25, 2020

Socket编程TCP协议实现多线程文件上传

通过Java Socket编程,实现多线程文件上传。主要涉及ServerScoket服务端、Socket客户端、IO流操作、多线线程。

客户端:

package learn.javase.Update;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

/**
 * 利用TCP的Socket\ServerSocket客户端向服务器上传文件
 * 客户端
 * @author Jole
 *
 */
public class UpClent {

    public static void main(String[] args) throws IOException{
        Socket socket =  new Socket("127.0.0.1",8888);
        OutputStream out = socket.getOutputStream();

        //读取本地文件,并通过out输出流,发送给服务端
        File file = new File("J:"+File.separator+"wxhandbook-master.zip");
        FileInputStream in = new FileInputStream(file);
        byte[] data = new byte[1024];
        int len =0;
        while((len=in.read(data))!=-1) {
            out.write(data, 0, len);
        }
        socket.shutdownOutput();

        //获取服务器返回信息
        InputStream info = socket.getInputStream();
        int infoSize=info.read(data);
        System.out.println(new String(data, 0, infoSize));

        info.close();
        socket.close();
    }

}

多线程实现类:

package learn.javase.Update;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Random;
/**
 * 改造为多线程,服务端
 * @author Jole
 *
 */
public class UpServerThread implements Runnable{

    private Socket socket;
    public UpServerThread(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        try {
            //获取客户端输入流
            InputStream in = socket.getInputStream();

            //判断文件夹是否存在,不存在则新建
            File file = new File("H:\\up");
            if(!file.exists()) {
                file.mkdir();
            }
            //设置文件名规则
            String fileName = System.currentTimeMillis()+"_"+new Random().nextInt(9999)+".zip";
            FileOutputStream out = new FileOutputStream(file+File.separator+fileName);
            //读取客户端发送数据,并写入到H:\\up文件夹下面
            byte[] data = new byte[1024];
            int len = 0;
            while((len=in.read(data))!=-1) {
                out.write(data, 0, len);
            }

            //返回消息给客户端,上传成功信息
            OutputStream outInfo = socket.getOutputStream();
            outInfo.write("上传成功".getBytes());
            outInfo.close();
            out.close();
            socket.close();
        }catch(Exception e) {
            e.printStackTrace();
        }
    }

}

服务端线程启动类:

package learn.javase.Update;

import java.net.ServerSocket;
import java.net.Socket;
/**
 * 启动服务器多线程
 * @author Jole
 *
 */
public class UpServerMainThread {
    public static void main(String[] args) throws Exception{
        ServerSocket server  = new ServerSocket(8888);
        while(true) {
            Socket socket = server.accept();
            new Thread(new UpServerThread(socket)).start();
        }
    }
}

本文链接:https://www.yanxizhu.com/post/socket.html

-- EOF --

Comments