zoukankan      html  css  js  c++  java
  • [Socket]Socket文件传输

    1、Server

    import java.io.DataInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    public class Server
    {
        private ServerSocket serverSocket;
    
        private DataInputStream in;
    
        private FileOutputStream out;
    
        public Server(int port) throws IOException
        {
            serverSocket = new ServerSocket(8899);
        }
    
        public void receiveFile(String destFile) throws IOException
        {
            Socket socket = serverSocket.accept();
            DataInputStream in = new DataInputStream(socket.getInputStream());
            
            out = new FileOutputStream(destFile);
            int length = in.readInt();
            System.out.println("receiveLength:"+length);
    
            byte[] buffer = new byte[2048];
            int end = 0;
            while ((end = in.read(buffer)) > 0)
            {
                out.write(buffer, 0, end);
                out.flush();
            }
        }
        public void close()
        {
            try{
                try{
                    if (in != null) in.close();
                }
                finally{
                    try{
                        if (out != null) out.close();
                    }
                    finally{
                        serverSocket.close();
                    }
                }
            }catch (IOException e){
                // ignore
            }
    
        }
    
        public static void main(String[] args) throws IOException
        {
            Server server = new Server(8899);
            try{
                server.receiveFile("c:/dest.txt");
            }finally{
                server.close();
            }
        }
    }

    2、Client

    import java.io.DataOutputStream;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.net.Socket;
    import java.net.UnknownHostException;
    
    public class Client
    {
        private Socket socket;
    
        private FileInputStream input;
    
        private DataOutputStream output;
    
        public Client(String IP, int port) throws UnknownHostException, IOException
        {
            this.socket = new Socket(IP, port);
            output = new DataOutputStream(socket.getOutputStream());
        }
    
        public void sendFile(String fileName) throws IOException
        {
            input = new FileInputStream(fileName);
            // 获得文件长度
            int fileLength = input.available();
            System.out.println("file length:" + fileLength);
            output.writeInt(fileLength);
            // 读取文件,写入socket
            byte[] buffer = new byte[2048];
            int bufferLength = 0;
            while ((bufferLength = input.read(buffer)) > 0)
            {
                output.write(buffer, 0, bufferLength);
            }
        }
    
        public void close()
        {
            try{
                try{
                    if (input != null) input.close();
                }
                finally{
                    try{
                        if (output != null) output.close();
                    }
                    finally{
                        socket.close();
                    }
                }
            }catch (IOException e){
                // ignore
            }
    
        }
    
        public static void main(String[] args) throws UnknownHostException, IOException
        {
            Client client = new Client("127.0.0.1", 8899);
            try
            {
                client.sendFile("c:/src.txt");
            }
            finally
            {
                client.close();
            }
        }
    }
  • 相关阅读:
    谷歌浏览器插件开发Tutorial: Getting Started (Hello, World!) 教程:准备开始(你好,世界!)
    Android ViewPager多页面滑动切换以及动画效果
    4.4 我同意条款—CheckBox的isCheck属性
    4.2设计具有背景图的按钮—ImageButton的焦点及事件处理
    【文件打开】浏览打开窗口
    【原创】PE检测工具
    emu8086注册算法分析及KeyGen实现
    学破解 <一> PE格式之MSDOS MZ header
    学破解 <二> PE格式之IMAGE_NT_HEADERS
    反虚拟机程序测试
  • 原文地址:https://www.cnblogs.com/jfqiu/p/3242863.html
Copyright © 2011-2022 走看看