Java使用Socket实现文件的下载(多线程版)

Java使用Socket实现文件的下载(多线程版)

客户端

import java.io.*;
import java.net.Socket;
import java.util.Scanner;
 
/**
 * @program: ImportantNotDelete
 * @Date: 2018-09-02 20:28
 * @Author: 夜~星空
 * @Description:客户端:使用套接字进行文件下载
 */
public class Client {
 
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        DataInputStream dateInputStream=null;
        DataOutputStream dataOutputStream=null;
        DataOutputStream localWrite=null;
        Socket socket=null;
        try {
            socket=new Socket("127.0.0.1",9992);
            //输入流
            dateInputStream = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
            //输出流
            dataOutputStream = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
            System.out.println("请输入文件路径:");
            String filePath = in.next();
            dataOutputStream.writeUTF(filePath);
            dataOutputStream.flush();
 
            String fileName = dateInputStream.readUTF();
            String length=dateInputStream.readUTF();
            System.out.println("文件名:"+fileName+"    文件大小:"+length);
            localWrite=new DataOutputStream(new BufferedOutputStream(new FileOutputStream("F:/"+fileName)));
            byte[] bytes=new byte[4096];
            while (true){
                int read=0;
                if(dataOutputStream!=null){
                    read = dateInputStream.read(bytes);
                }
                if(read==-1){
                    break;
                }
                localWrite.write(bytes,0,read);
                localWrite.flush();
            }
            System.out.println("文件传输完毕!");
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            in.close();
            try {
                localWrite.close();
                dataOutputStream.close();
                dateInputStream.close();
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
 
        }
 
    }
 
}

服务器端

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
 
/**
 * @program: ImportantNotDelete
 * @Date: 2018-09-02 20:47
 * @Author: 夜~星空
 * @Description:服务器端:使用套接字实现文件下载功能
 */
public class Server {
 
    public static void main(String[] args) {
 
        try {
            int i=0;
            ServerSocket serverSocket=new ServerSocket(9992);
            while(true){
                System.out.println("服务器已启动!");
                Socket socket =serverSocket.accept();
                Thread thread=new Thread(new ThreadHandler(socket),"Thread-"+i++);
                thread.start();
            }
 
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
 
class ThreadHandler implements Runnable {
    private Socket socket;
 
    public ThreadHandler(Socket socket) {
        this.socket = socket;
    }
 
    @Override
    public void run() {
        DataOutputStream dataOutputStream=null;
        DataInputStream dataInputStream=null;
        DataInputStream localRead =null;
        try {
            dataOutputStream=new DataOutputStream(socket.getOutputStream());
            dataInputStream=new DataInputStream(new BufferedInputStream(socket.getInputStream()));
 
            String filePath = dataInputStream.readUTF();
            File file=new File(filePath);
            //判断文件是否存在
            if(!file.exists()){
                return;
            }else{
                //文件名
                String fileName = file.getName();
                dataOutputStream.writeUTF(fileName);
                dataOutputStream.flush();
                //文件大小
                long length = file.length();
                dataOutputStream.writeUTF(String.valueOf(length));
                dataOutputStream.flush();
 
                System.out.println("开始向 "+Thread.currentThread().getName()+
                        " 发送文件,文件名:"+fileName+"  文件大小"+length);
                localRead =new DataInputStream(new BufferedInputStream(new FileInputStream(file)));
                byte[] bytes=new byte[4096];
                while (true){
                    int read=0;
                    if(localRead!=null){
                        read = localRead.read(bytes);
                    }
                    if(read==-1){
                        break;
                    }
                    dataOutputStream.write(bytes,0,read);
                    dataOutputStream.flush();
                }
                System.out.println("向 "+Thread.currentThread().getName()+" 发送文件完毕!");
            }
 
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                localRead.close();
                dataOutputStream.close();
                dataInputStream.close();
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值