zoukankan      html  css  js  c++  java
  • java之Socket传递图片

    客户端:

    package client;
    
    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.net.Socket;
    import java.net.UnknownHostException;
    
    public class MyClient {
    
        private static final String ADDRESS = "127.0.0.1";
        private static final int PORT = 7869;
    
        private Socket CreateSocket() {
            try {
                Socket server = new Socket(ADDRESS, PORT);
                return server;
            } catch (UnknownHostException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }
    
        public boolean FileTransfer(String fileName) {
            Socket server = this.CreateSocket();
            try {
                BufferedOutputStream bos = new BufferedOutputStream(
                        server.getOutputStream());
                BufferedInputStream bis = new BufferedInputStream(
                        new FileInputStream(fileName));
                byte[] bytes = new byte[1024];
                int len = 0;
                while ((len = bis.read(bytes)) != -1) {
                    bos.write(bytes, 0, len);
                }
                bis.close();
                bos.close();
                return true;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return false;
        }
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            MyClient client = new MyClient();
            if (client.FileTransfer("wario.png")) {
                System.out.println("Trans file sucessfully!");
            }
        }
    
    }

    服务器端:

    package server;
    
    import java.io.IOException;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    public class FileServer implements Runnable {
    
        private ServerSocket server = null;
        private int port = -1;
        private String fileName;
    
        public FileServer(int port, String fileName) {
            this.port = port;
            this.fileName = fileName;
            try {
                this.server = new ServerSocket(this.port);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                System.out.println("Please check this port!");
                e.printStackTrace();
            }
        }
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Thread thread=new Thread(new FileServer(7869,"nannan.png"));
            thread.start();
            
        };
    
        @Override
        public void run() {
            // TODO Auto-generated method stub
            while (true) {
                try {
                    Socket client;
                    client = this.server.accept();
                    AcceptFile accept=new AcceptFile(client,fileName);
                    Thread thread=new Thread(accept);
                    thread.start();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    
    }
    
    
    package server;
    
    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.net.Socket;
    
    public class AcceptFile extends Thread {
    
        private Socket client;
        private String fileName;
        
        public AcceptFile(Socket client,String fileName) {
            this.client = client;
            this.fileName=fileName;
        }
    
        @Override
        public void run() {
            // TODO Auto-generated method stub
            try {
                BufferedInputStream bis=new BufferedInputStream(client.getInputStream());
                BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(fileName));
                System.out.println("Server begin receive file!");
                int len=0;
                byte[] bytes=new byte[1024];
                while((len=bis.read(bytes))!=-1){
                    bos.write(bytes, 0, len);
                }
                bos.close();
                bis.close();
                System.out.println("Accept file sucessfully!");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
        }
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
    
        }
    
    }
  • 相关阅读:
    【系列】CentOS 7.3 离线安装(无网络环境)CI CD环境之sonarqube配置
    Abp vnext 配置Swagger增加token认证
    sonarqube+gitlab runner +docker 代码质量检查问题汇总
    【EF Core】EF core中使用FluentAPI对外键进行指定配置
    【系列】CentOS 7.3 离线安装(无网络环境)CI CD环境之gitlab runner 关于私有docker仓库配置
    【系列】CentOS 7.3 离线安装(无网络环境)CI CD环境之harbor
    【系列】CentOS 7.3 离线安装(无网络环境)CI CD环境之gitlab + gitlab runner(docker in docker)
    【杂记】关于在实际项目中使用TDD的方法
    【系列】CentOS 7.3 离线安装(无网络环境)CI CD环境之docker+docker compose
    【TeamCity】使用TeamCity搭建ASP.NET Core + SVN 的 CICD环境
  • 原文地址:https://www.cnblogs.com/nannanITeye/p/3461710.html
Copyright © 2011-2022 走看看