其他分享
首页 > 其他分享> > 2021.6.19笔记 综合案例:文件上传

2021.6.19笔记 综合案例:文件上传

作者:互联网

原理

在这里插入图片描述

客户端代码

在这里插入图片描述

public class PracticeClient {
    public static void main(String[] args) throws IOException {
        FileInputStream fips = new FileInputStream("H:\\test\\aa.txt");
        Socket so = new Socket("175.0.0.1",8888);
        OutputStream ops = so.getOutputStream();
        byte[] bytes = new byte[1024];
        int len = 0;
        while((len = fips.read(bytes)) != -1) {
            ops.write(bytes,0,len);
        }
        InputStream ips = so.getInputStream();
        while((len = ips.read(bytes)) != -1) {
            System.out.println(new String(bytes,0,len));
        }
        fips.close();
        so.close();
    }
}

服务器代码

在这里插入图片描述

public class PracticeServer {
    public static void main(String[] args) throws IOException {
        ServerSocket sso = new ServerSocket(8888);
        Socket socket = sso.accept();
        InputStream ips = socket.getInputStream();
        File file = new File("H:\\test\\bba");
        if(file.exists()) {
            file.mkdirs();
        }
        FileOutputStream fops = new FileOutputStream(file + "\\server.txt");
        byte[] bytes = new byte[1024];
        int len = 0;
        while((len = ips.read(bytes)) != -1) {
            fops.write(bytes,0,len);
        }
        OutputStream ops = socket.getOutputStream();
        ops.write("收到谢谢".getBytes());
        fops.close();
        socket.close();
        sso.close();
    }
}

标签:2021.6,19,bytes,len,ips,file,close,new,上传
来源: https://blog.csdn.net/q4451728/article/details/118051854