Java重修笔记 TCP 网络通信编程 - 传输文件

 1. 服务器端

public class TCPFileCopyServer {
    public static void main(String[] args) throws Exception {
        // 服务器端在 8888 端口监听
        ServerSocket serverSocket = new ServerSocket(8888);
        // 等待连接
        System.out.println("服务端在端口 8888 监听....");
        Socket socket = serverSocket.accept();
        // 接收客户端传过来的图片
        InputStream inputStream = socket.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(inputStream);
        byte[] bytes = StreamUtils.streamToByteArray(bis);
        // 设置图片存放路径
        File file = new File("src\\imageCopy.png");
        // 设置为追加方式写入
        FileOutputStream fos = new FileOutputStream(file, true);
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        bos.write(bytes);
        bos.close();
        socket.shutdownInput();
        // 向客户端发送信息发送
        OutputStream outputStream = socket.getOutputStream();
        OutputStreamWriter osw = new OutputStreamWriter(outputStream);
        BufferedWriter writer = new BufferedWriter(osw);
        writer.write("收到图片");
        writer.flush();
        // 关闭其他资源
        writer.close();
        bis.close();
        socket.close();
        serverSocket.close();


    }
}

2. 客户端

public class TCPFileCopyClient {
    public static void main(String[] args) throws Exception {
        // 客户端往端口 8888 发一张图片
        Socket socket = new Socket(InetAddress.getLocalHost(), 8888);
        // 设置图片路径
        String filePath = "d:\\hnt.png";
        File file = new File(filePath);
        // 读出图片内容
        FileInputStream fileInputStream = new FileInputStream(file);
        BufferedInputStream bis = new BufferedInputStream(fileInputStream);
        // 获取文件内容存放到 bytes 数组
        byte[] bytes = StreamUtils.streamToByteArray(bis);
        // 发送数据
        OutputStream outputStream = socket.getOutputStream();
        BufferedOutputStream bos = new BufferedOutputStream(outputStream);
        bos.write(bytes); // 写入
        bis.close(); // 关闭输入流
        socket.shutdownOutput(); // 设置结束标记
        // 接收从服务端传来的消息
        InputStream inputStream = socket.getInputStream();
        String s = StreamUtils.streamToString(inputStream);
        System.out.println(s);
        // 关闭其他资源
        inputStream.close();
        bos.close();
        socket.close();
    }
}

3. 工具类

public class StreamUtils {
    public static byte[] streamToByteArray(InputStream is) throws Exception {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();//创建输出流对象
        byte[] b = new byte[1024];//字节数组
        int len;
        while ((len = is.read(b)) != -1) {//循环读取
            bos.write(b, 0, len);//把读取到的数据,写入bos
        }
        byte[] array = bos.toByteArray();//然后将bos 转成字节数组
        bos.close();
        return array;
    }

    public static String streamToString(InputStream is) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder builder = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            builder.append(line + "\r\n");
        }
        return builder.toString();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值