JAVA网络编程中客户端给服务器端发送文件

该代码示例展示了如何使用TCP协议在客户端和服务器端之间传输文件。客户端首先连接到服务器,然后通过输出流将图片数据发送到服务器。服务器接收连接,从输入流读取文件数据并将其保存到本地文件。确保先启动服务器,再启动客户端以完成文件传输过程。
摘要由CSDN通过智能技术生成

eg:客户端给服务器端传送一张图片

/*
客户端发送文件给服务器端
 */


package com.jcwzc01;

import com.sun.security.ntlm.Server;
import org.junit.Test;

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

public class TestTcp01 {
    @Test
            public void client() {//创建客户端测试程序
        InetAddress inetAddress;
        OutputStream outputStream = null;
        FileInputStream fileInputStream = null;
        Socket socket = null;

        {
            try {
                inetAddress = InetAddress.getByName("127.0.0.1");
                socket = new Socket(inetAddress, 8899);
                outputStream = socket.getOutputStream();//输出流用于写入文件
                fileInputStream = new FileInputStream(new File("G:\\JAVASystem\\xx\\1.jpg"));//文件输入流用于读取文件
                byte[] arr = new byte[1024];
                int temp;
                while ((temp = fileInputStream.read(arr)) != -1) {
                    outputStream.write(arr, 0, temp);
                }
            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (fileInputStream != null) {
                    try {
                        fileInputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (outputStream != null) {
                    try {
                        outputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (socket != null) {
                    try {
                        socket.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }


            }
        }
    }
@Test
    public void server(){//创建客户端
    ServerSocket serverSocket=null;
    Socket socket=null;
    InputStream inputStream=null;
    FileOutputStream fileOutputStream=null;
    try {
         serverSocket=new ServerSocket(8899);//指定端口
         socket=serverSocket.accept();
         inputStream=socket.getInputStream();//输入流用于读取文件
         int temp;
         fileOutputStream=new FileOutputStream(new File("1.jpg"));//文件输出流把客户端发过来的图片保存在文件中
         byte[] arr=new byte[1024];
         while((temp=inputStream.read(arr))!=-1){
             fileOutputStream.write(arr,0,temp);
         }

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


}
}

注意:先启动服务器程序,在启动客户端程序

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值