(TCP) 从服务器下载数据

客户端:

package com.qf.demo6;

import java.io.File;
import java.io.FileOutputStream;
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;

/**
 * 客户端
 * 要下载数据
 * 
 * 
 * 1 先给服务器发送一句话 说  我要下载
 * 
 * 2 再去接收服务器发送的数据
 * @author Administrator
 *
 */
public class Client {

    public static void main(String[] args) {
        Socket socket = null;
        OutputStream os = null;
        InputStream is = null;
        FileOutputStream fos =null;
        //1 
        try {
             socket = new Socket("localhost", 4444);
            // 
             os = socket.getOutputStream();
            os.write("我要下载".getBytes());
            os.flush();


            // 2 接收服务器发送的文件了
             is = socket.getInputStream();
             fos = new FileOutputStream(new File("c.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 (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);
            if(fos!=null){
                try {
                    fos.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
}

服务器端:

package com.qf.demo6;

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

import com.qf.demo3.Util;

public class Server {

    public static void main(String[] args) {
        ServerSocket serverSocket = null;
        Socket socket = null;
        InputStream is = null;
        OutputStream os = null;
        InputStream fis = null;
        // 1
        try {
            serverSocket = new ServerSocket(4444);
            socket = serverSocket.accept();// 我要下载
            // 读取客户端发送过来的内容
            is = socket.getInputStream();
            byte[] bs = new byte[1024];
            int num = is.read(bs);
            String string = new String(bs, 0, num);
            if ("我要下载".equals(string)) {

                // 读取本地文件 发送给 客户端
                os = socket.getOutputStream();
                fis = new FileInputStream(new File("b.java"));
                byte[] bs2 = new byte[1024];
                int num2 = 0;
                while ((num2 = fis.read(bs2)) != -1) {
                    os.write(bs2, 0, num2);
                    os.flush();
                }

            }

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            Util.closed(serverSocket, socket, is, os);
            if(fis!=null){
                try {
                    fis.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值