Tcp文件下载

实现音乐的下载

服务器

package Net;

import java.io.*;
import java.net.*;

/**
 * @author 小小小白白白白
 */
public class TcpServer01 {
    public static void main(String[] args) throws Exception {
        //监听9999端口
        ServerSocket serverSocket = new ServerSocket(9999);
        //等待客户端连接
        System.out.println("服务端在9999端口监听...");
        Socket socket = serverSocket.accept();
        //读取客户端发送要下载的文件名
        InputStream inputStream = socket.getInputStream();
        byte[] bytes = new byte[1024];
        int len=0;
        String downloaded="";
        //使用while读取是为了防止文件名过长,数据量过大
        while ((len = inputStream.read(bytes)) != -1){
            downloaded += new String(bytes, 0, len);
        }
        System.out.println("客户端希望下载的文件名是:"+downloaded);
        //服务器上准备两个文件:等风归和清枫徐月
        String filename ="";
        if ("等风归".equals(downloaded)){
            filename="F:\\音乐\\等风归.mp3";
        }else {
            filename="F:\\音乐\\清枫徐月.mp3";
        }

        //创建输入流用于读取文件
        BufferedInputStream bis =
                new BufferedInputStream(new FileInputStream(filename));
        //使用工具类读取文件到一个字节数组
        byte[] bytes1 = StreamUtils.streamToByteArray(bis);
        //得到输出流
        BufferedOutputStream bos =
                new BufferedOutputStream(socket.getOutputStream());
        //写入数据通道,返回给客户端
        bos.write(bytes1);
        socket.shutdownOutput();
        //关闭相关的流
        bis.close();
        inputStream.close();
        socket.close();
        serverSocket.close();
        System.out.println("服务器退出");
    }
}

客户端

package Net;

import java.io.*;
import java.net.*;
import java.util.Scanner;

/**
 * @author 小小小白白白白
 */
public class TcpClient01 {
    public static void main(String[] args) throws Exception {
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入下载文件名");
        String filename = sc.next();
        //客户端连接服务器准备发送
        Socket socket = new Socket(InetAddress.getLocalHost(), 9999);
        //获取和socket关联的输出流
        OutputStream outputStream = socket.getOutputStream();
        outputStream.write(filename.getBytes());
        socket.shutdownOutput();
        //读取服务器返回的文件
        BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
        byte[] bytes = StreamUtils.streamToByteArray(bis);
        //得到一个输出流将文件写入磁盘
        String filepath ="F:\\"+filename+".mp3";
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filepath));
        bos.write(bytes);
        //关闭相关的流
        bos.close();
        bis.close();
        outputStream.close();
        socket.close();
        System.out.println("客户端下载完毕");
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

1while(true){learn}

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值