android客户端利用sokcet通信和向Java服务端发请求,Java服务端把文件发给android客户端
2015-02-13 15:48
417 查看
Java服务端
android客户端:
下载地址:http://download.csdn.net/detail/u014600432/8445151
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(); } } /** * 读取客户端指令 * @author yqq_coder * */ static class ReadThread implements Runnable{ private Socket mSocket; public ReadThread(Socket socket){ mSocket=socket; } @Override public void run() { System.out.println("读线程开启"); 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(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } buffer=new byte[count]; in.read(buffer); mByteArrayOutputStream.write(buffer); // askForFile System.out.println("读线程开启"+mByteArrayOutputStream.toString()); if(mByteArrayOutputStream.toString().equals("askForFile")){ File file=new File("F://测试.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("写线程开启socket测试"); } os.write(arrayOutputStream.toByteArray()); os.flush(); socket.shutdownOutput(); System.out.println("读线程开启socket"+socket.isConnected()); /* //启动写线程 (new Thread(new WriteThread(socket))).start();*/ } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(in!=null){ try { in.close(); in=null; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(os!=null){ try { os.close(); os=null; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } /** * 向客户端写数据(文件) * @author yqq_coder * *//* static class WriteThread implements Runnable{ private Socket mSocket; public WriteThread(Socket socket){ mSocket=socket; } @Override public void run() { System.out.println("写线程开启"); File file=new File("F://测试.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 block e.printStackTrace(); }finally{ if(os!=null){ try { os.close(); os=null; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } */ } } }
android客户端:
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; OutputStream outputStream; Handler mHandler=new Handler(){ public void handleMessage(Message msg) { if(msg.what==0x123){ Toast.makeText(SocketClient.this,"文件下载完毕", 0).show(); } } }; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(com.example.socketclient.R.layout.main); } public void downFile(View v){ new Thread(new Runnable() { @Override public void run() { try { socket=new Socket(InetAddress.getByName("172.20.113.1"), 20000); inputStream=socket.getInputStream(); outputStream=socket.getOutputStream(); outputStream.write("askForFile".getBytes()); outputStream.flush(); //socket.shutdownOutput(); Thread.sleep(3000); byte[] buffer=new byte[1024]; File file=new File(getCacheDir(),"测试"); 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 block e.printStackTrace(); } } }).start(); } }
下载地址:http://download.csdn.net/detail/u014600432/8445151
相关文章推荐
- 多文件的上传 Android客户端与java服务端
- 【javaweb:servlet】客户端请求服务端进行文件下载的demonstration
- Android---网络交互之客户端请求服务端资源
- 利用JAVA写一段上传文件程序(模拟Ftp客户端)
- Android文件上传至tomcat,服务端+客户端示例代码
- android 客户端与服务端的通信 发送get和post请求并获取数据
- 【TCP协议练习】服务端利用多线程TCP同时接受多个客户端发送文件
- Android-网络交互之客户端请求服务端资源-stoneson的专栏-  -  棒槌网@Android开发论坛 - Powered by phpwind
- Android客户端采用Http 协议Post方式请求与服务端进行数据交互
- java实现的客户端向服务端上传文件的程序
- 根据wsdl 利用axis1.4的WSDL2Java工具生成服务端和客户端代码
- java客户端向服务端发post请求
- Android客户端采用Http 协议Post方式请求与服务端进行数据交互
- android客户端和java服务端之间用socket来传输图片
- Android---网络交互之客户端请求服务端资源后续
- 用Java模拟表单(或iPhone、Android客户端)上传文件
- Java利用HttpURLConnection发送post请求上传文件
- 用.Net打造一个移动客户端(Android/IOS)的服务端框架NHM(二)——“请求”“交互”与传输数据(服务器端)
- Android aidl项目中服务端与客户端aidl文件不一致引起的问题
- JAVA服务端多个文件合并传到客户端分解