Socket-based upload/download (Java) Lite version

Source: Internet
Author: User

In a simple example, there is no interface but code.

Server code

Package server; import Java. io. bufferedinputstream; import Java. io. bufferedoutputstream; import Java. io. datainputstream; import Java. io. dataoutputstream; import Java. io. fileinputstream; import java.net. serversocket; import java.net. socket; public class androidserver implements runnable {private serversocket = NULL; private datainputstream is; Public void run () {If (serversocket = NULL) {try {se Rversocket = new serversocket (5926);} catch (exception e) {e. printstacktrace () ;}try {Socket socket = serversocket. accept (); is = new datainputstream (New bufferedinputstream (New fileinputstream ("D: \ test.mp4"); // upload the file to be uploaded, put inputstreamdataoutputstream OS = new dataoutputstream (New bufferedoutputstream (socket. getoutputstream (); // initialize the socket outputstreamint count; byte [] buffer = new byte [1024]; W Hile (COUNT = is. Read (buffer, 0, buffer. Length ))! =-1) {// put the file into the socket outputstream OS. write (buffer, 0, count);} OS. flush (); serversocket. close (); socket. close ();} catch (exception e) {e. printstacktrace () ;}} public static void main (string [] ARGs) {thread publish topserverthread = new thread (New androidserver (); // start the Service publish topserverthread. start ();}}

Client code

Import Java. io. bufferedoutputstream; import Java. io. datainputstream; import Java. io. dataoutputstream; import Java. io. file; import Java. io. fileoutputstream; import java.net. socket; public class test {public static void main (string [] ARGs) {try {Socket socket = new socket ("192.168.0.228", 5926); datainputstream Dis = new datainputstream (socket. getinputstream (); // obtain inputstreamdataoutputstream OS = New dataoutputstream (New bufferedoutputstream (New fileoutputstream (new file ("D: \ fuck"); // initialize outputstreamint COUNT = 0; byte [] buffer = new byte [1024]; while (COUNT = dis. read (buffer ))! =-1) {// put inputstream in socket into outputstreamos. write (buffer, 0, count);} OS. close (); socket. close (); // the upload is complete. Close socket} catch (exception e) {e. printstacktrace () ;}finally {}}}

  

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.