Based on the operation simulation download the file IO streams

First, create a server-side

public  class DownloadServer {
     public  static  void main (String [] args) throws Exception { 
        the ServerSocket Server = new new the ServerSocket (55021 );
         the while ( to true ) { 
            the Socket Socket = server.accept (); 
            System.out.println ( "the connection is established ! " );
             // create a character sent by the client to accept the input stream object 
            BufferedReader br = new new BufferedReader ( new new InputStreamReader (Socket.getInputStream ()));
             // create a character output stream object
            PrintWriter pw=new PrintWriter(socket.getOutputStream());
            StringBuilder sbu=new StringBuilder();
            String s=br.readLine();
            if(s.equals("image")){
                //创建文件对象
                File file=new File("service");
                File[] files = file.listFiles();
                for(File f:files){
                    System.out.print(f.getName()+"\t");
                    sbu.append(f.getName()+",");
                }
                pw.println (SBU); 
                pw.flush (); 
                br.close (); 
                pw.close (); 
            } the else { 
                File File = new new File ( "-Service /" + S);
                 // create a binary input stream objects, read the files in the directory 
                DataInputStream DIS = new new DataInputStream ( new new the FileInputStream (file));
                 // create a binary output stream object, the client sends data to 
                the DataOutputStream DOS = new new the DataOutputStream (Socket.getOutputStream ());
                 byte [] bytes = new new byte [1024];
                 Int Data;
                 // read the directory of the file to the client outputs a binary stream of 
                the while ((= dis.read Data (bytes)) = -. 1! ) { 
                    Dos.write (bytes, 0 , Data); 
                } 
                dos.close (); 
                dis.close (); 
                br.close (); 
                the Socket.close (); 
            } 
        } 

    } 
}

Second, create a client

public  class DownloadClient {
    static the Socket Socket;
     public  static  void main (String [] args) throws Exception { 
        Scanner INPUT = new new Scanner (the System.in); 
        System.out.println ( .. "1. Image \ T2 videos \ Music T3 " ); 
        of System.out.print ( " Please select number: " );
         int NUM = input.nextInt ();
         Switch (NUM) {
             Case . 1 : 
                String [] STR = getName ();
                 for ( int. 1 = I; I <= str.length; I ++ ) { 
                    of System.out.print (I + "," + STR [-I. 1] + "\ T" ); 
                } 
                of System.out.print ( "\ n-please select the number you want to download the image: " );
                 int num2 = input.nextInt (); 
                downloads (STR [num2 -1 ]);
                 BREAK ;
             Case 2 :
                 BREAK ;
             Case . 3 :
                 BREAK ; 
        } 
    } 
    public  static String [] getName () throws Exception {
        // Create a socket object 
        socket = new new the Socket ( "127.0.0.1", 55021 ); 
        PrintWriter pw = new new PrintWriter (Socket.getOutputStream ()); 
        pw.println ( "Image" ); 
        pw.flush (); 
        // Gets server data transmission over 
        the BufferedReader br = new new the BufferedReader ( new new the InputStreamReader (Socket.getInputStream ())); 
        String S = br.readLine (); 
        br.close (); 
        pw.close (); 
        the Socket.close (); 
        return s.split ( "," ); 
    } 
    public  static  voiddownloads (String name) throws Exception { 
        Socket = new new the Socket ( "127.0.0.1", 55021 ); 
        the PrintWriter PW = null ; 
        DataInputStream DIS = null ; 
        the DataOutputStream DOS = null ;
         the try {
             // create an output stream, and transmitted to download Image name 
            pw = new new PrintWriter (Socket.getOutputStream ()); 
            pw.println (name); 
            pw.flush (); 
            // accept client sent the binary stream 
            DIS = new newDataInputStream (Socket.getInputStream ());
             // the file path and the write mode to write 
            DOS = new new the DataOutputStream ( new new a FileOutputStream ( "downloads /" + name, to true ));
             byte [] bytes = new new  byte [1024 ] ;
             int Data;
             // read binary stream and copied 
            the while - ((= dis.read Data (bytes)) =. 1! {) 
                dos.write (bytes, 0 , Data); 
            } 

        } the catch (Exception E) { 
            E .printStackTrace (); 
        } the finally {
            try {
                dos.close();
                dis.close();
                pw.close();
                socket.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

 

Guess you like

Origin www.cnblogs.com/TFE-HardView/p/11098424.html