/** Client 1, service Endpoint 2, read the client's existing file data 3, through the socket output stream to the server 4, read the service side Feedback 5, close **/import java.io.*;import java.net.*;class uploadclient{public static void Main (string[] args) throws Exception{if (args.length! = 1){System.out.println ("No Files selected!!! ");Return}File File = new file (Args[0]);if (! ( File.exists () && file.isfile ())){System.out.println ("The file does not exist or the file is not a file!!!" ");Return}if (! ( File.getname (). EndsWith (". jpg"))){SYSTEM.OUT.PRINTLN ("File format Error!!! ");Return}if (File.length () > 1024*1024*5){System.out.println ("The file is too big!!! ");Return}Socket s = new socket ("202.194.240.74", 4444);FileInputStream fis = new FileInputStream (file);OutputStream out = S.getoutputstream ();byte[] buf = new byte[1024];int len = 0;while (len = Fis.read (buf))!=-1){
Out.write (Buf,0,len);//This step is a problem, how to solve? }Execution of Shutdownoutput when data is completeS.shutdownoutput ();InputStream is = S.getinputstream ();byte[] Bufin = new byte[1024];int lenin = Is.read (Bufin);System.out.println (New String (Bufin,0,lenin));Fis.close ();S.close ();}}/** thread */class Uploadthread implements runnable{Private Socket S;Uploadthread (Socket S){THIS.S = s;}public void Run (){int count = 1;String IP = "111";Try{System.out.println (ip+ "Connection");InputStream is = S.getinputstream ();File File = new file (ip+ "(" + (count++) + ")" + ". jpg");while (File.exists ())File = new File (ip+ "(" + (count++) + ")" + ". jpg");FileOutputStream fos = new FileOutputStream (file);byte[] buf = new byte[1024];int len = 0;while (len = Is.read (buf))!=-1){Fos.write (Buf,0,len);}OutputStream out = S.getoutputstream ();Out.write ("Upload succeeded". GetBytes ());Fos.close ();S.close ();}catch (Exception e){
throw new RuntimeException (ip+ "upload failed" +e); }}}/** server-side */class uploadserver{public static void Main (string[] args) throws Exception{ServerSocket ss = new ServerSocket (4444);while (true){Socket s = ss.accept ();New Thread (new Uploadthread (s)). Start ();}}}
Java TCP Concurrent implementation file upload---reprint (PS: for java1.6)