The examples in this article describe how Android downloads files through a socket. Share to everyone for your reference, as follows:
Service-side code
Import Java.io.bufferedinputstream;import Java.io.datainputstream;import java.io.dataoutputstream;import Java.io.file;import Java.io.fileinputstream;import Java.io.ioexception;import Java.io.InputStream;import Java.io.outputstream;import java.net.serversocket;import java.net.socket;import Java.util.Scanner; Public classFunctionserver {Private Static intPORT = -; PrivateString Path ="the path to the file you want to download"; Public Static voidMain (string[] args) throws ioexception{Functionserver server=NewFunctionserver (); Server.start (); } Public voidstart () throws ioexception{ServerSocket SS=NewServerSocket (PORT); while(true) {Socket s=ss.accept (); NewService (s). Start ();//Creating Threads } } classService extends thread{Socket s; PublicService (Socket s) { This. S =s; } Public voidrun () {Try{InputStreaminch= S.getinputstream ();//Get input streamScanner sc =NewScanner (inch); OutputStream out=S.getoutputstream (); while(true) {String str= Sc.nextline ();//Read file name if(!str.equals (NULL) ) {System. out. println ("your file name is"+str); //get files based on path and file nameFile f =NewFile (path+str); FileInputStream FIS=NewFileInputStream (f); DataInputStream Dis=NewDataInputStream (NewBufferedinputstream (FIS)); byte[] buffer =New byte[8192]; DataOutputStream PS=NewDataOutputStream ( out); Ps.writelong ((Long) f.length ());//Send File SizePs.flush (); while(true) { intRead =0; if(dis!=NULL) {Read=fis.read (buffer); } if(Read = =-1){ Break; } ps.write (Buffer,0, read); } ps.flush (); Dis.close (); S.close (); out. Flush (); Break; } } }Catch(IOException e) {e.printstacktrace (); } } }}
Client code, download thread
classDownloadthread extends Thread {socket socket; InputStreaminch; OutputStream out; String Path="File Save path"; String functionname; String ServerIP="Server IP"; intSocketport ="Service port number"; intfilesize,downloadfilesize; PublicDownloadthread (String functionname) { This. functionname =functionname; } @Override Public voidrun () {looper.prepare (); while(!thread.interrupted ()) { Try{Socket=NewSocket (ServerIP, Socketport); InputStreaminch=Socket.getinputstream (); OutputStream out=Socket.getoutputstream (); out. Write ((functionname +"\ n"). GetBytes ("GBK")); out. Flush ();//clean up the buffer to ensure that it is sent to the serverFile f =NewFile (path +functionname); OutputStream Song=NewFileOutputStream (f); DataInputStream Dis=NewDataInputStream (NewBufferedinputstream (inch)); DataOutputStream dos=NewDataOutputStream (NewBufferedoutputstream (song)); FileSize= (int) Dis.readlong ()-1; System. out. println ("Start Download"); byte[] buffer =New byte[8192]; while(true) { intRead =0; if(Dis! =NULL) {Read=dis.read (buffer); Downloadfilesize+=Read; } if(Read = =-1) { Break; } dos.write (Buffer,0, read); } System. out. println ("File Download Complete"); Dos.close (); } Catch(unknownhostexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } finally { This. Interrupt (); }}}} Basic can be used directly, according to their own needs to change a little bit ok I hope this article on Android programming has
How Android downloads files via the socket