(TCP) 从服务器下载数据

来源:互联网 发布:一直播如何挂淘宝链接 编辑:程序博客网 时间:2024/06/05 16:56

客户端:

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();                }            }        }    }}
原创粉丝点击