[转载]android客户端和java服务端之间用socket来传输图片

为研究android客户端和java服务端之间用socket来传输图片的问题,困扰我很久,网上的参考内容不是完整,就是不详细,所以鉴于自己的痛苦,测试成功后,我决定把代码共享,希望遇到这个问题的同胞,不再痛苦。

一、从服务端向客户端发送图片:

服务端的代码:

  1. import java.io.DataOutputStream;    
  2. import java.io.FileInputStream;    
  3. import java.io.IOException;      
  4. import java.net.ServerSocket;    
  5. import java.net.Socket;    
  6.   
  7. public class Server01 {    
  8.     public static void main(String[] args) {    
  9.         try {    
  10.             ServerSocket server = new ServerSocket(30000);    
  11.             Socket socket = server.accept();    
  12.             DataOutputStream dos = new DataOutputStream(socket.getOutputStream());    
  13.             FileInputStream fis = new FileInputStream("C:/sunnyTest/picture/cat01.jpg");    
  14.             int size = fis.available();  
  15.               
  16.             System.out.println("size = "+size);  
  17.             byte[] data = new byte[size];    
  18.             fis.read(data);    
  19.             dos.writeInt(size);    
  20.             dos.write(data);    
  21.               
  22.             dos.flush();    
  23.             dos.close();    
  24.             fis.close();    
  25.             socket.close();    
  26.             server.close();    
  27.         } catch (IOException e) {    
  28.             e.printStackTrace();    
  29.         }    
  30.     }    
  31. }    

客户端的代码:

  1. import com.login.R;  
  2. import android.app.Activity;    
  3. import android.content.Intent;  
  4. import android.graphics.Bitmap;    
  5. import android.graphics.Bitmap.CompressFormat;    
  6. import android.graphics.BitmapFactory;    
  7. import android.os.Bundle;    
  8. import android.os.Handler;    
  9. import android.view.View;    
  10. import android.view.View.OnClickListener;    
  11. import android.widget.Button;    
  12. import android.widget.ImageView;   
  13.   
  14. public class TestActivity extends Activity {    
  15.         
  16.     private ImageView imageView = null;    
  17.     private Bitmap bmp = null;  
  18.       
  19.     private ImageView imageView02;  
  20.     private Bitmap bmp02;  
  21.     private Button button02;  
  22.     private String uploadFile="/mnt/sdcard/qt.png";    
  23.       
  24.     /** Called when the activity is first created. */    
  25.     @Override    
  26.     public void onCreate(Bundle savedInstanceState) {    
  27.         super.onCreate(savedInstanceState);    
  28.         setContentView(R.layout.image);    
  29.     
  30.         imageView = (ImageView) findViewById(R.id.image01);    
  31.         Button btn = (Button) findViewById(R.id.Button01);    
  32.         btn.setOnClickListener(new OnClickListener() {    
  33.             public void onClick(View v) {    
  34.                   
  35.                 Socket socket = null;    
  36.                 try {    
  37.                     socket = new Socket("192.168.1.203", 30000);    
  38.                     DataInputStream dataInput = new DataInputStream(socket.getInputStream());    
  39.                     int size = dataInput.readInt();    
  40.                     byte[] data = new byte[size];    
  41.                     int len = 0;    
  42.                     while (len < size) {    
  43.                         len += dataInput.read(data, len, size - len);    
  44.                     }    
  45.                     ByteArrayOutputStream outPut = new ByteArrayOutputStream();    
  46.                     bmp = BitmapFactory.decodeByteArray(data, 0, data.length);    
  47.                     bmp.compress(CompressFormat.PNG, 100, outPut);    
  48.                     imageView.setImageBitmap(bmp);    
  49.                      
  50.                     //Bitmap bitmap = BitmapFactory.decodeStream(dataInput);  
  51.                     //myHandler.obtainMessage().sendToTarget();    
  52.                 } catch (IOException e) {    
  53.                     e.printStackTrace();    
  54.                 }   
  55.                 finally {    
  56.                     try {    
  57.                         socket.close();    
  58.                     } catch (IOException e) {    
  59.                         e.printStackTrace();    
  60.                     }    
  61.                 }    
  62.                     }    
  63.         });  
  64.  }    
  65.  }    


二、客户端向服务端发送图片的代码:

服务端:

  1. import java.io.DataInputStream;  
  2. import java.io.DataOutputStream;    
  3. import java.io.FileInputStream;    
  4. import java.io.IOException;    
  5. import java.net.InetSocketAddress;    
  6. import java.net.ServerSocket;    
  7. import java.net.Socket;    
  8.   
  9. public class Server02 {    
  10.     public static void main(String[] args) {    
  11.         try {    
  12.             ServerSocket server = new ServerSocket(40000);    
  13.             Socket socket = server.accept();    
  14.             DataInputStream dos = new DataInputStream(socket.getInputStream());    
  15.             int len = dos.available();   
  16.             System.out.println("len = "+len);  
  17.             byte[] data = new byte[len];    
  18.             dos.read(data);  
  19.               
  20.             System.out.println("data = "+data);  
  21.             dos.close();    
  22.             socket.close();    
  23.             server.close();    
  24.         } catch (IOException e) {    
  25.             e.printStackTrace();    
  26.         }    
  27.     }    
  28. }    


客户端:

    1. imageView02 = (ImageView)findViewById(R.id.image02);  
    2.        button02 = (Button)findViewById(R.id.Button02);  
    3.        button02.setOnClickListener(new OnClickListener(){  
    4.         public void onClick(View arg0) {  
    5.             Socket socket;  
    6.             try {  
    7.                 socket = new Socket("192.168.1.203",40000);  
    8.                 DataOutputStream out = new DataOutputStream(socket.getOutputStream());   
    9.                   
    10.                 Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.qt);  
    11.                 imageView02.setImageBitmap(bitmap);  
    12.                 ByteArrayOutputStream baos = new ByteArrayOutputStream();  
    13.                 //读取图片到ByteArrayOutputStream                      
    14.                 bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);  
    15.                 byte[] bytes = baos.toByteArray();  
    16.                 out.write(bytes);  
    17.                   
    18.                 System.out.println("bytes--->"+bytes);  
    19.                 out.close();  
    20.                 socket.close();  
    21.             } catch (UnknownHostException e) {  
    22.                 e.printStackTrace();  
    23.             } catch (IOException e) {  
    24.                 e.printStackTrace();  
    25.             }  
    26.         }             
    27.     });  
    28.          
    29.    }  
posted @ 2015-01-01 22:07  chenlinyunyi  阅读(155)  评论(0编辑  收藏  举报