编写客户端程序, 客户端上传.txt文件(需要对文件类型进行判断), 服务器端用于接收文件(采用多线程), 文件上传成功, 服务端给客户端一个反馈: 文件上传成功.

本文档详细介绍了如何编写客户端程序,通过判断文件类型上传.txt文件,并利用多线程技术在服务器端接收文件。当文件上传成功后,服务器会向客户端发送确认反馈。
摘要由CSDN通过智能技术生成

编写客户端程序, 客户端上传.txt文件(需要对文件类型进行判断), 服务器端用于接收文件(采用多线程), 文件上传成功, 服务端给客户端一个反馈: 文件上传成功.

在这里插入图片描述

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

public class UpServer {
    public static void main(String[] args) throws IOException {
        ServerSocket ss = new ServerSocket(13770);
        while (true) {
            Socket s = ss.accept();

            new Thread(() -> {
                try {
                    BufferedInputStream bis = new BufferedInputStream(s.getInputStream());
                    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(System.currentTimeMillis() + ".txt"));

                    byte[] bys = new byte[1024 * 8];
                    int len ;
                    while ((len= bis.read(bys)) != -1) {
                        bos.write(bys, 0, len);
                        bos.flush();
                    }
                    //给出反馈
                    BufferedWriter bwServer = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
                    bwServer.write("文件上传成功");
                    bwServer.flush();
                    bos.close();
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (s != null) {
                        try {
                            s.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }

            }).start();
        }

    }
}


public class UpClient {
    public static void main(String[] args) {
        Socket s = null;
        BufferedInputStream bis = null;
        try {
            s = new Socket("127.0.0.1",13770);
            File file = new File("jianda\\People.txt");
            if (!file.exists() || !file.isFile() || !file.getName().endsWith("txt")){
                System.out.println("文件路径不对");
                return;
            }
            bis = new BufferedInputStream(new FileInputStream(file));
            BufferedOutputStream bos = new BufferedOutputStream(s.getOutputStream());
            byte[] bys = new byte[1024 * 8];
            int len;
            while ((len = bis.read(bys)) != -1) {
                bos.write(bys,0,bys.length);
                bos.flush();
            }
            s.shutdownOutput();
            BufferedReader brClient = new BufferedReader(new InputStreamReader(s.getInputStream()));
            String data = brClient.readLine();    //等待读取数据
            System.out.println("服务器端反馈: " + data);
            brClient.close();
        }catch (IOException e1){
            e1.printStackTrace();
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值