Java TCP Concurrent implementation file upload---reprint (PS: for java1.6)

Source: Internet
Author: User

/** 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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.