编程语言
首页 > 编程语言> > Java>网络编程、Socket、ServerSocket 实现文件上传FileUpLoad

Java>网络编程、Socket、ServerSocket 实现文件上传FileUpLoad

作者:互联网

客户端代码

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

public class FileUpload_Client {

    public static void main(String[] args) throws IOException {
        // create FileInputStream
        // 创建一个字节流
        FileInputStream fileInputStream = new FileInputStream("E:\\folder_io_dome\\Client\\dome.txt");

        // create socket object and set port
        // 创建一个socket对象 并设置端口号
        Socket socket = new Socket("localhost", 1111);
        // 通过socket对象获取字节输出流
        OutputStream outputStream = socket.getOutputStream();


        byte[] bytes = new byte[1024];
        int len = 0;
        // print get data
        while ((len = fileInputStream.read(bytes)) != -1) {
            System.out.println(new String(bytes, 0, len));
            // write data on the server
            // 将本地数据写入服务端
            outputStream.write(bytes, 0, len);
        }

        // because '-1' not write server , so use 'shutdownOutput' method
        // '-1' 读取到了,但是没有上传到服务端,因此  需要手动添加服务端的停止条件
        socket.shutdownOutput();

        // accept server send message
        // 接收服务端发送过来的消息
        InputStream inputStream = socket.getInputStream();
        while ((len = inputStream.read(bytes)) != -1) {
            // print server send message
            System.out.println(new String(bytes, 0, len));
        }


        // close stream
        fileInputStream.close();
        socket.close();
    }
}

服务端代码

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class FileUpLoad_Server {
    public static void main(String[] args) throws IOException {
        file_upload();
    }

    public static void file_upload() {
        try {
            // create serverSocket object
            // 创建serverSocket对象
            ServerSocket serverSocket = new ServerSocket(1111);
            while (true) {
                // accept client send data
                // 接收客户端发送的数据
                // get socket object
                Socket socket = serverSocket.accept();
                // 获取inputStream对象
                InputStream inputStream = socket.getInputStream();

                // random name 随机生成名字
                String file_name = System.currentTimeMillis() + ".txt";

                // write disk 写入硬盘
                FileOutputStream fileOutputStream = new FileOutputStream("E:\\folder_io_dome\\Server\\" + file_name);

                // set every time read size
                byte[] bytes = new byte[1024];
                int len = 0;
                // ergodic write disk 遍历写入硬盘
                while ((len = inputStream.read(bytes)) != -1) {
                    System.out.println("Accept data success !! Now write data on the disk !!");
                    // print client message/data 打印客户端发送过来的消息或者是数据
                    // System.out.println(new String(bytes, 0, len));

                    // 写入数据(缓存区)
                    fileOutputStream.write(bytes);
                }
                // flush data  刷新数据,写入硬盘
                fileOutputStream.flush();


                // return message
                // 返回消息
                OutputStream outputStream = socket.getOutputStream();
                outputStream.write("Data write disk success !!".getBytes());

                // 关流 close Stream
                fileOutputStream.close();
                socket.close();
                // 因可能出现重复上传,这里,不关闭流
                // serverSocket.close();
            }
        } catch (IOException e) {
            System.out.println(e);
        }
    }

}

标签:FileUpLoad,java,Socket,bytes,Java,io,new,import,socket
来源: https://blog.csdn.net/weixin_43309893/article/details/116239323