14-5 网络编程 ---- TCP网络编程(2)例子2

该博客介绍了如何使用TCP进行网络编程,通过Java实现客户端将文件`漂移火辣辣.jpg`发送到监听9000端口的服务端,服务端接收到文件内容后将其保存为`漂移火辣辣1.jpg`。示例展示了Socket和ServerSocket的使用,包括输入输出流的读写操作,以及资源的关闭流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

14-5 网络编程 ---- TCP网络编程(2)例子2

例题2:客户端发送文件给服务端,服务端将文件保存在本地

运行步骤:
(1)启动server
(2)再启动client
(3)之后在server会显示发送来的信息

(这里的127.0.0.1为本机,即本机给本机发信息,由本机接收)

代码:

package java1;

import org.junit.Test;

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

//实现TCP的网络编程
//例题2:客户端发送文件给服务端,服务端将文件保存在本地。
public class TCPTest2 {

    @Test
    public void client(){

        OutputStream os = null;
        FileInputStream fis = null;
        Socket socket = null;
        try {
            //1.造Socket
            socket = new Socket(InetAddress.getByName("127.0.0.1"),9000);
            //2.获取输出流
            os = socket.getOutputStream();
            //3.获取输入流
            fis = new FileInputStream(new File("漂移火辣辣.jpg"));
            //4.读取
            byte[] buffer = new byte[1024];
            int len;
            while((len = fis.read(buffer)) != -1){
                os.write(buffer,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //5.关闭
            if(fis != null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
            if(os != null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
            if(socket != null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }



    }



    @Test
    public void server(){
        ServerSocket ss = null;
        Socket socket = null;
        InputStream is = null;
        FileOutputStream fos = null;
        try {
            //1.造一个ServerSocket
            ss = new ServerSocket(9000);
            //2.获取客户端的socket
            socket = ss.accept();
            //3.获取客户端的输入流
            is = socket.getInputStream();
            //4.保存数据到本地
            fos = new FileOutputStream(new File("漂移火辣辣1.jpg"));
            //5.读写过程
            byte[] buffer = new byte[1024];
            int len;
            while((len = is.read(buffer)) != -1){
                fos.write(buffer,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //6.关闭
            if(fos != null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
            if(is != null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
            if(socket != null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
            if(ss != null){
                try {
                    ss.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }


    }


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

YY鸟

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

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

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

打赏作者

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

抵扣说明:

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

余额充值