一、目的

  服务器端:读取客户上传的文件,保存到本地的硬盘D,并且返回“上传”成功字样。

  客户端:从本地E读取文件,上传到服务器,读取服务器回写的数据。

二,服务器端实现步骤

  1.首先得创建ServerSocket对象,绑定用户的端口号,

   ServerSocket   server = new ServerSocket(8888);

  2.使用ServerSoket中的accept方法来不停止的接受检测客户端请求

   while(true){

      Socket socket=server.accept();

   使用多线程技术,提高程序的效率

    new Thread(new runnable){

    public void  run(){

           3.使用Socket对象中的方法getInputStream,获取网络字节输入流InputStream对象

       InputStream is=so·ckect.getInputStream();

        4.判断要存的D盘下路径是否存在,不存在创建

        File file=new File(“D:\\upload”);

        if(!file.exist()){

          file.mkdirs();

        }

        自定义一个文件的命名规则:防止同名的文件被覆盖

        规则:域名+毫秒值+随机数

         String fileName=”itcast”+System.currentTimeMillis()+new Random().nextInt(9999)+”.jpg”;

        5.创建一个本地字节输出流FileOutputStream对象,构造方法中要绑定目的地

        FileOutputStream fos=new FileOutputStream(file+”\\”+fileName);

        6.使用网络字节输入流InputStream对象中的方法read ,读取客户上传的文件

        int len=0;

        byte [] bytes =new byte[1024];

        while(len=is.read(bytes)!=-1){

        7.使用本地字节输出流FileOutputStream对象中的方法write,吧读取到的文件保存到服务器硬盘上

        fos.write(bytes,0,len);

        }

        8.使用Socket对象那个方法中的getOutputStream,获取到网络字节输出流OutputStream对象

        9.使用网络字节输出流OutputStream对象中的方法writer,给客户端回写“上传成功”

        socket.getOutputStream().write(“上传成功”);

        10.释放资源(FileoutputStream,Socket,ServerSocket)

         fos.close();

         socket.close();

         //服务器不用关闭

         //server.close();

           }

        }

    }

三、客户端实现

    1.创建一个本地字节输入流FileInputStream对象,构造方法中绑定要读取的数据源
    FileInputStream fis = new FileInputStream(“E:\\1.jpg”);
    2.创建一个客户端Socket对象,构造方法中绑定服务器的IP地址和端口号
    Socket socket = new Socket(“127.0.0.1”,8888);
    3.使用Socket中的方法getOutputStream,获取网络字节输出流OutputStream对象
    OutputStream os = socket.getOutputStream();
    4.使用本地字节输入流FileInputStream对象中的方法read,读取本地文件
    int len = 0;
    byte[] bytes = new byte[1024];
    while((len = fis.read(bytes))!=-1){
    5.使用网络字节输出流OutputStream对象中的方法write,把读取到的文件上传到服务器
    os.write(bytes,0,len);
    }
    /*
    解决:上传完文件,给服务器写一个结束标记
    void shutdownOutput() 禁用此套接字的输出流。
    对于 TCP 套接字,任何以前写入的数据都将被发送,并且后跟 TCP 的正常连接终止序列。
    */
    socket.shutdownOutput();
    6.使用Socket中的方法getInputStream,获取网络字节输入流InputStream对象
    InputStream is = socket.getInputStream();
    7.使用网络字节输入流InputStream对象中的方法read读取服务回写的数据
    while((len = is.read(bytes))!=-1){
    System.out.println(new String(bytes,0,len));
    }
    8.释放资源(FileInputStream,Socket)
    fis.close();
    socket.close();
    }

   

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

 

  

版权声明:本文为ironman-yan原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/ironman-yan/p/11809195.html