(TCP模式)客户端与服务器之间的文件传输

1 .先看需求:
上传文件
将 客户端的文件上传到 服务器
2 . 步骤:
2-1.创建socket输出流,为了方便往socket写数据:
os = socket.getOutputStream();
2-2 . 读取文件读到数组中(fileinputstream)
将数组中的数据通过os写入socket
3 .
3-1.服务器端,先接收一个socket
3-2.创建socket输入流(读取socket中的数据)和普通的文件输出流(方便把从socket读取的数据写入文件)

明白操作之后就很简单了

客户端:

package com.qf.demo5;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

import com.qf.demo3.Util;

/**
 * 上传文件
 * 将 客户端的文件上传到 服务器
 * @author Administrator
 *
 */
public class Client {

    public static void main(String[] args) {
        // 1  创建快递点
        Socket socket =  null;
        OutputStream os =null;
        InputStream is =null;
        try {
             socket = new Socket("127.0.0.1", 9999);
            // 2 先得到快递员
             os = socket.getOutputStream();
            // 3 准备数据  从本地文件中读取数据
             is =new  FileInputStream(new File("D:\\A.java"));
            byte[] bs = new byte[1024];
            int num = 0;
            while((num = is.read(bs))!=-1){
                // 将读取出来的数据 写到 快递点中
                os.write(bs,0,num);
                os.flush();
            }
            System.out.println("客户端已经发送完了数据了");
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            Util.closed(null, socket, is, os);
        }


    }
}

服务器端:

package com.qf.demo5;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

import com.qf.demo3.Util;

public class Server {

    public static void main(String[] args) {
        // 1 
        ServerSocket serverSocket =null;
        Socket socket = null;
        InputStream is  = null;
        FileOutputStream fos = null;
        try {
             serverSocket = new ServerSocket(9999);
            // 2 
             socket = serverSocket.accept();
            // 3 从socket中获取数据
             is  = socket.getInputStream();
             fos = new FileOutputStream(new File("b.java"));
            byte[] bs = new byte[1024];
            int num = 0;
            while((num = is.read(bs))!=-1){
                fos.write(bs, 0, num);
                fos.flush();
            }
            System.out.println("服务端接收完毕");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            Util.closed(serverSocket, socket, is, fos);
        }
    }
}
  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值