JAVA——多客户端上传文件,服务端接收

162 阅读2分钟

需求

多客户端上传文件,服务端接收

Server

public class Server01{
    public static void main(String[] args) throws {
        ServerSocket server = new ServerSocket(1025);
        System.out.println("server running……"); 
        while(true){
        Socket socket = server.accept();
        System.out.println("a client connected……");
        ReceivedFileTask task = new ReceivedFileTask(socket);//接收文件的任务 
        Thread t = new Thread(task);//线程
        t.start();
        }
    }
} 
 //解决安全问题currentTimeMillis()
    class FileNameUtil{ 
        private static int number=1;
      
        public  synchronizad static String getName(){
            return System.currentTimeMillis()+".mp3";//根据当前时间来获取名称。
            return "music"+number++ +"mp3";
        }
    }
    class ReceivedFileTask implenments Runnable{
        private Socket socket;
        //构造函数
        public ReceivedFileTask(Socket socket){
            this.socket=socket;
        }
        @Override
        public void run(){//写接收的代码
            //获取服务端的输入流,字节
            BufferedInputStream in =null;
            //获取服务端的输出流(向客户端反馈消息),字符
             BufferedWrier out =null;
            //获取文件的字节输出流
             BufferedOutputStream bos =null;
             
             try{
                 in = new BufferedInputStream(socket.getInputStream());
                 out=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                 //创建文件对象
                // String fileName=System.currentTimeMillis()+"";//文件名客户端没有上传,服务端自定义,根据时间来定。
                 String fileName=FileNameUtil.getName();
                 bos=new bufferedOutputStream(new FileOutputStream(fileName));
                 //从服务端的输入流获取数据写入文件中
                 byte[] buf=new byte[1024];
                 int len = 0;
                 while((len=in.read(buf))!=-1){
                     bos.write(buf,0,len);
                     bos.flush();
                     
                 }
                 //向服务端反馈
                 out.write("over");
                 out.flush();
                 System.out.println("a file has been received……");
             }catch(IOException e){
                 e.printStackTrace();
             }finally{
                 try{
                     socket.close();
                 }catch(IOException e){
                     e.printStackTrace();
                 }
                 if(bos!=null){
                     try{
                         bos.close();
                     }catch(IOException e){
                         e.printStackTrace();
                     }
                 }
             }         
        }
    }

Client

public class Client01{
    public static void main(String[] args){
        String ip ="172.16.80.51";
        //上传文件
        UpdateFileTask task1=new UpdateFileTask(ip,"任务1","a.mp3");
        UpdateFileTask task2=new UpdateFileTask(ip,"任务1","b.mp3");
        UpdateFileTask task3=new UpdateFileTask(ip,"任务1","c.mp3");
        
        Thread t1=new Thread(task1);
        Thread t2=new Thread(task2);
        Thread t3=new Thread(task3);
        t1.start();
        t2.start();
        t3.start();
    }
}
class UpdateFileTask implement Runnable{
    private String ip;
    private String taskName;
    private String fileName;
    //创建构造函数
    public UpdateFileTask(String ip, String taskName,String fileName){
        this.ip = ip;
        this.taskName = taskName;
        this.fileName = fileName;
    }
    //重写run函数
    public void run(){
       System.out.println(taskName+"start……"); 
       Socket socket=null;
       //获取文件的字节输入流
       BufferedInputStream bis=null;//要捉异常
       
       //获取客户端向客户端写出数据的输出流
       BufferedOutputStream out = null;
       //获取客户端读取服务端传来的输入流(字符数据)
       BufferedReader in;
       
       try{
           Socket socket=new Socket(ip,1025);//关联端口
           bis= new BufferedInputStream(new FileInputStream(fileName));
           out = new BufferedOutputStream(socket.getOutputStream());
           in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
           
           byte[] buf=new byte[1024];
           int len=0;
           while((len = bis.read(buf))!=-1){
               out.write(buf,0,len);
               out.flush();
           }
           //获取服务端的响应
           socket.shutdownOutput();//关闭输出流
           String resp =in.readLine();//从服务器读取一段话
           System.out.println(taskName+resp);//打印+响应 
       }catch(UnknowHostException e){
           e.printStackTrace();
       }catch(IOException e){
           e.printStackTrace();
       }finally{
           try{
               socket.close();
            }catch(IOException e){
               e.printStackTrace();
           }
           if(bis!=null){
              try{
                   bis.close();
               }catch(IOException e){
                   e.printStackTrace();
               }  
           }
       }
    }
}