Java Service side
Package Com.yqq.sockettest;import Java.io.bytearrayoutputstream;import Java.io.file;import Java.io.FileInputStream ; Import Java.io.filenotfoundexception;import Java.io.ioexception;import Java.io.inputstream;import Java.io.outputstream;import Java.net.serversocket;import Java.net.socket;public class SocketTest {static ServerSocket mserversocket;static Socket socket;public static void Main (string[] args) throws IOException {Mserversocket =new ServerSocket (20000); while (true) {socket=mserversocket.accept ();(new Thread (new Readthread (socket)). Start ();}} /** * Read Client instruction * @author Yqq_coder * */static class Readthread implements Runnable{private Socket msocket;public Readthread (Socket socket) {msocket=socket;} @Overridepublic void Run () {System.out.println ("read thread Open"); InputStream In=null;outputstream Os=null; Bytearrayoutputstream mbytearrayoutputstream=new Bytearrayoutputstream (); try {in=msocket.getinputstream (); int count=0; Byte[] Buffer=null; while ((Count=in.available ()) ==0) {count=in.available (); try {thread.sleep;} catch (Interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();} } buffer=new Byte[count]; In.read (buffer); Mbytearrayoutputstream.write (buffer); Askforfile System.out.println ("Read thread Open" +mbytearrayoutputstream.tostring ()); if (mbytearrayoutputstream.tostring (). Equals ("Askforfile")) {file File=new file ("f://test. xls"); FileInputStream Is=null; Is=new fileinputstream (file); Os=msocket.getoutputstream (); Bytearrayoutputstream arrayoutputstream=new Bytearrayoutputstream (); Byte[] Buffer2=new byte[1024]; while (is.read (buffer)!=-1) {arrayoutputstream.write (buffer2); System.out.println ("Write Thread open Socket test"); } os.write (Arrayoutputstream.tobytearray ()); Os.flush (); Socket.shutdownoutput (); SYSTEM.OUT.PRINTLN ("Read thread open Socket" +socket.isconnected ());/*//Start the Write thread (new thread (new Writethread (socket)). Start (); */ }} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();} Finally{if (in!=null) {try {in.close (); in=null;} catch (IOException e) {TODO auto-generated catch Blocke.printstacktrace ();}} if (os!=null) {try {os.close (); os=null;} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}}} /** * Write data to client (file) * @author Yqq_coder * *//*static class Writethread implements Runnable{private Socket msocket;public Wri Tethread (socket socket) {Msocket=socket;} @Overridepublic void Run () {System.out.println ("Write thread Open"); File File=new file ("f://test. xls"); FileInputStream is=null;outputstream os=null;try {is=new fileinputstream (file); Os=msocket.getoutputstream (); Bytearrayoutputstream arrayoutputstream=new Bytearrayoutputstream (); Byte[] Buffer=new byte[1024]; while (is.read (buffer)!=-1) {arrayoutputstream.write (buffer);} os.write (Arrayoutputstream.tobytearray ()); Os.flush (); Msocket.shutdownoutput ();} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();} Finally{if (os!=null) {try {os.close (); os=null;} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}}} */}}}
Android Client:
Package Com.yqq.socketclient;import Java.io.file;import Java.io.fileoutputstream;import java.io.IOException;import Java.io.inputstream;import Java.io.outputstream;import Java.net.inetaddress;import Java.net.Socket;import Java.net.unknownhostexception;import Android.app.activity;import Android.os.bundle;import android.os.Environment; Import Android.os.handler;import android.os.looper;import Android.os.message;import Android.view.view;import Android.widget.toast;public class Socketclient extends Activity {private Socket socket;inputstream Inputstream;o Utputstream OutputStream; Handler mhandler=new Handler () {public void Handlemessage (Message msg) {if (msg.what==0x123) {Toast.maketext ( Socketclient.this, "File download Complete", 0). Show ();}}; @Overrideprotected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate ( Savedinstancestate); Setcontentview (Com.example.socketclient.r.layout.main);} public void Downfile (View v) {New Thread (new Runnable () {@Overridepublic void run () {TRy {socket=new socket (inetaddress.getbyname ("172.20.113.1"), 20000); Inputstream=socket.getinputstream (); O Utputstream=socket.getoutputstream (); Outputstream.write ("Askforfile". GetBytes ()); Outputstream.flush ();// Socket.shutdownoutput (); Thread.Sleep (+); byte[] Buffer=new byte[1024]; File File=new file (Getcachedir (), "test"); FileOutputStream fileoutputstream=new fileoutputstream (file), while (Inputstream.read (buffer)!=-1) { Fileoutputstream.write (buffer);} Looper.prepare (); Message Message=message.obtain (); message.what=0x123;mhandler.sendmessage (message); Looper.loop ();} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}). Start ();}}
: http://download.csdn.net/detail/u014600432/8445151
The Android client uses SOKCET communication and sends a request to the Java service side to send the file to the Android client