---Reprint of TCP file transfer

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{Socket s = new socket ("127.0.0.1", 4434);FileInputStream fis = new FileInputStream ("D://2.png");OutputStream out = S.getoutputstream ();byte[] buf = new byte[1024];int len = 0;while (len = Fis.read (buf))!=-1){Out.write (Buf,0,len);}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 ();}}/** server-side */class uploadserver{public static void Main (string[] args) throws Exception{ServerSocket ss = new ServerSocket (4434);Socket s = ss.accept ();InputStream is = S.getinputstream ();FileOutputStream fos = new FileOutputStream ("c://test.jpg");byte[] buf = new byte[1024];int len = 0;while (len = Is.read (buf))!=-1){Fos.write (buf,0,buf.length);}OutputStream out = S.getoutputstream ();Out.write ("Upload succeeded". GetBytes ());Fos.close ();S.close ();Ss.close ();}}

---Reprint of TCP file transfer

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.