• Java SE 基础复习-IO与序列化(2)-网络操作 HTTP与SOCKET


    一、Socket  文件传输

    1、基本文件传输

    客户端

     public static void main(String[] args)
         {       
             Socket socket=null;
             FileInputStream fis=null;
             DataOutputStream dos=null;
             int sum=0;
             int length=0;
             File file=new File("H:\TEST\test.jpg");
             long l=file.length();
             try {
                 fis=new FileInputStream(file);
                 socket=new Socket("127.0.0.1", 23456);
                 dos=new DataOutputStream(socket.getOutputStream());
                 byte[] b=new byte[8192];
                 while((length=fis.read(b))>0)
                 {
                     dos.write(b, 0, length);
                     sum+=length;
                     System.out.println("已发送"+(sum*1.0/l)+"%");
                 }
                 if(sum==l)
                 {
                     System.out.println("传送完成!");
                 }
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (UnknownHostException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally{
                try {
                    if(fis!=null)
                        fis.close();
                    if(dos!=null)
                        dos.close();
                    if(socket!=null)
                        socket.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
             
         }  

    服务器端

        public static void main(String[] args)
        {
            ServerSocket serverSocket=null;
            try {
                serverSocket=new ServerSocket(23456);
                while(true)
                {
                    Socket socket=serverSocket.accept();
                    if(socket!=null)
                        handleFile(socket);
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally
            {
                try {
                    if(serverSocket!=null)
                        serverSocket.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
     private static void handleFile(Socket socket)
        {
            int length=0;
            FileOutputStream fos=null;
            DataInputStream dis=null;
            File file=new File("H:\TEST\test2.jpg");
            try {
                dis=new DataInputStream(socket.getInputStream());
                fos=new FileOutputStream(file);
                byte[] buf=new byte[8192];
                while((length=dis.read(buf))>0)
                {
                    fos.write(buf, 0, length);
                }
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            finally{
                try {
                    if(fos!=null)
                    {
                        fos.close();
                    }
                    if(dis!=null)
                    {
                        dis.close();
                    }
                    if(socket!=null)
                    {
                        socket.close();
                    }
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

    2、文件断点续传

    二、http

  • 相关阅读:
    polya定理
    树状数组
    离散数学通路数的矩阵计算法
    高次同余方程求解
    Uva1378
    poj2888
    poj2409&&poj1286
    poj2182
    poj2154
    判断一个字符是否为汉字
  • 原文地址:https://www.cnblogs.com/maydow/p/4596412.html
Copyright © 2020-2023  润新知