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

原理

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();
    }
}

服务器代码

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

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();
    }
}
上一篇:显示器已入手,猜猜我选了哪个显示器?


下一篇:Rails向数据库添加新字段