Software structure 软件结构

 
 
 
 
 
 
 
 
 

 
 
 
 

 
 
 







 
1 public class ServerDemo { 2 public static void main(String[] args) throws IOException { 3 ServerSocket server = new ServerSocket(8888); 4 while (true) { 5 Socket client = server.accept(); 6 7 new Thread(new Runnable() { 8 @Override 9 public void run() { 10 try { 11 InputStream is = client.getInputStream(); 12 File file = new File("d:\\upload"); 13 if (!file.exists()) { 14 file.mkdirs(); 15 } 16 String fileName = "test" + System.currentTimeMillis() + new Random().nextInt(999999) + ".mp4"; 17 FileOutputStream fos = new FileOutputStream(file + "\\" + fileName); 18 byte[] bytes = new byte[2048]; 19 int len = 0; 20 while ((len = is.read(bytes)) != -1) { 21 fos.write(bytes, 0 , len); 22 } 23 client.getOutputStream().write("upload success !".getBytes()); 24 fos.close(); 25 client.close(); 26 } catch (IOException e) { 27 e.printStackTrace(); 28 } 29 30 } 31 }).start(); 32 33 } 34 // server.close(); 35 36 } 37 } 38 39 40 41 public class ClientDemo { 42 public static void main(String[] args) throws IOException { 43 Socket client = new Socket("127.0.0.1", 8888); 44 OutputStream ops = client.getOutputStream(); 45 FileInputStream fis = new FileInputStream("d:\\1.m4a"); 46 byte[] bytes = new byte[2048]; 47 int len = 0; 48 while ((len = fis.read(bytes)) !=-1) { 49 ops.write(bytes,0, len); 50 } 51 client.shutdownOutput(); 52 53 InputStream is = client.getInputStream(); 54 while ((len = is.read(bytes)) != -1) { 55 System.out.println(new java.lang.String(bytes,0,len)); 56 } 57 client.close(); 58 fis.close(); 59 } 60 }


 
 
 
 
 
 
                    
                     
                    
                 
                    
                
 
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号