基于TCP的文件传输

服务器端处理客户请求:

public class ServerThread extends Thread {
    private Socket socket=null;
    public ServerThread(Socket socket){
        this.socket=socket;
    }
    @Override
    public void run() {
        //3、获取输入流,并读取客户端信息
        InputStream is= null;
        DataInputStream dis=null;
        FileOutputStream fos=null;
        OutputStream os= null;
        PrintWriter oos=null;
        try {
                is = socket.getInputStream();
                dis=new DataInputStream(is);
                //文件名和长度
                String fileName=dis.readUTF();
                long fileLength=dis.readLong();
                File directory=new File("G:\\hello");
                if(!directory.exists()){
                    directory.mkdir();
                }
                File file=new File(directory.getAbsolutePath()+File.separatorChar+
                fileName);
                fos=new FileOutputStream(file);
                //开始接受文件
                byte[] bytes=new byte[1024];
                int length=0;
                while((length=dis.read(bytes,0,bytes.length))!=-1){
                    fos.write(bytes,0,length);
                    fos.flush();
                }
                System.out.println("文件接受成功!文件长度为"+fileLength);
                //4、获取输出流,响应客户端的请求
                os = socket.getOutputStream();
                oos=new PrintWriter(os);
                oos.write("服务器已经接受到文件!");
                oos.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //5、关闭资源
            try {
                if(fos!=null)
                    fos.close();
                if(dis!=null)
                    dis.close();
                if(oos!=null)
                    oos.close();
                if(is!=null) {
                    is.close();
                }
                if(os!=null)
                    os.close();
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
服务器端;

public class Server {
    public static void main(String[] args){
        try {
            //1、创建一个服务器端Socket,即ServerSocket,指定绑定的端口
            ServerSocket serverSocket=new ServerSocket(9999);
            int num=0;//统计客户端的数量
            //2、调用accept()方法开始坚挺,等待客户端的连接
            System.out.println("***服务器即将启动,等待客户端的连接***");
            Socket socket=null;
            while (true){
                socket=serverSocket.accept();
                ServerThread serverThread=new ServerThread(socket);
                serverThread.start();
                num++;
                System.out.println("当前客户端的数量 :"+num);
                InetAddress inetAddress=socket.getInetAddress();
                System.out.println("客户端的IP地址为 :"+inetAddress.getHostAddress());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
客户端:

public class Client {
    public static void main(String[] args){
        try {
                //1.创建客户端Socket,指定服务器地址和端口
                Socket socket=new Socket("localhost",9999);
                //2.获取输入流,像服务器发送信息
                OutputStream os=socket.getOutputStream();//字节输入流
                File file=new File("G:\\a.jpg");
                FileInputStream fis=null;
                DataOutputStream dos=null;
                if(file.exists()) {
                    fis=new FileInputStream(file);
                    dos=new DataOutputStream(os);
                    dos.writeUTF(file.getName());
                    dos.flush();
                    dos.writeLong(file.length());
                    dos.flush();
                    System.out.println("***开始传输文件***");
                    byte[] bytes=new byte[1024];
                    int length=0;
                    long progress=0;
                    while ((length=fis.read(bytes,0,bytes.length))!=-1){
                        dos.write(bytes,0,length);
                        dos.flush();
                        progress+=length;
                        System.out.print("  "+(100*progress/file.length())+"%"+"  ");
                    }
                    System.out.println();
                    System.out.println("***文件传输成功***");
                    //3.获取输入流,并读取服务器端的响应信息
                    InputStream is = socket.getInputStream();
                    BufferedReader ois=new BufferedReader(
                            new InputStreamReader(is)
                    );
                    String s=null;
                    while((s=ois.readLine())!=null){
                        System.out.println(s);
                    }
                    //4/关闭资源
                    fis.close();
                    dos.close();
                    is.close();
                    os.close();
                    socket.close();
                }
            }catch (IOException e) {
            e.printStackTrace();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值