当前位置:首页 > Android > 正文内容

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

jsc9年前 (2016-04-05)Android3462

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

服务端的代码:

import java.io.DataOutputStream;  
import java.io.FileInputStream;  
import java.io.IOException;    
import java.net.ServerSocket;  
import java.net.Socket;  

public class Server01 {  
    public static void main(String[] args) {  
        try {  
            ServerSocket server = new ServerSocket(30000);  
            Socket socket = server.accept();  
            DataOutputStream dos = new DataOutputStream(socket.getOutputStream());  
            FileInputStream fis = new FileInputStream("C:/sunnyTest/picture/cat01.jpg");  
            int size = fis.available();
            
            System.out.println("size = "+size);
            byte[] data = new byte[size];  
            fis.read(data);  
            dos.writeInt(size);  
            dos.write(data);  
            
            dos.flush();  
            dos.close();  
            fis.close();  
            socket.close();  
            server.close();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  
}

客户端的代码:

import com.login.R;
import android.app.Activity;  
import android.content.Intent;
import android.graphics.Bitmap;  
import android.graphics.Bitmap.CompressFormat;  
import android.graphics.BitmapFactory;  
import android.os.Bundle;  
import android.os.Handler;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.widget.Button;  
import android.widget.ImageView; 

public class TestActivity extends Activity {  
      
    private ImageView imageView = null;  
    private Bitmap bmp = null;
    
    private ImageView imageView02;
    private Bitmap bmp02;
    private Button button02;
    private String uploadFile="/mnt/sdcard/qt.png";  
    
    /** Called when the activity is first created. */  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.image);  
  
        imageView = (ImageView) findViewById(R.id.image01);  
        Button btn = (Button) findViewById(R.id.Button01);  
        btn.setOnClickListener(new OnClickListener() {  
            public void onClick(View v) {  
                
                Socket socket = null;  
                try {  
                    socket = new Socket("192.168.1.203", 30000);  
                    DataInputStream dataInput = new DataInputStream(socket.getInputStream());  
                    int size = dataInput.readInt();  
                    byte[] data = new byte[size];  
                    int len = 0;  
                    while (len < size) {  
                        len += dataInput.read(data, len, size - len);  
                    }  
                    ByteArrayOutputStream outPut = new ByteArrayOutputStream();  
                    bmp = BitmapFactory.decodeByteArray(data, 0, data.length);  
                    bmp.compress(CompressFormat.PNG, 100, outPut);  
                    imageView.setImageBitmap(bmp);  
                   
                    //Bitmap bitmap = BitmapFactory.decodeStream(dataInput);
                    //myHandler.obtainMessage().sendToTarget();  
                } catch (IOException e) {  
                    e.printStackTrace();  
                } 
                finally {  
                    try {  
                        socket.close();  
                    } catch (IOException e) {  
                        e.printStackTrace();  
                    }  
                }  
                    }  
        });
 }  
 }

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

服务端:

import java.io.DataInputStream;
import java.io.DataOutputStream;  
import java.io.FileInputStream;  
import java.io.IOException;  
import java.net.InetSocketAddress;  
import java.net.ServerSocket;  
import java.net.Socket;  

public class Server02 {  
    public static void main(String[] args) {  
        try {  
            ServerSocket server = new ServerSocket(40000);  
            Socket socket = server.accept();  
            DataInputStream dos = new DataInputStream(socket.getInputStream());  
            int len = dos.available(); 
            System.out.println("len = "+len);
            byte[] data = new byte[len];  
            dos.read(data);
            
            System.out.println("data = "+data);
            dos.close();  
            socket.close();  
            server.close();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  
}

客户端:

 imageView02 = (ImageView)findViewById(R.id.image02);
        button02 = (Button)findViewById(R.id.Button02);
        button02.setOnClickListener(new OnClickListener(){
            public void onClick(View arg0) {
                Socket socket;
                try {
                    socket = new Socket("192.168.1.203",40000);
                    DataOutputStream out = new DataOutputStream(socket.getOutputStream()); 
                    
                    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.qt);
                    imageView02.setImageBitmap(bitmap);
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    //读取图片到ByteArrayOutputStream                    
                    bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
                    byte[] bytes = baos.toByteArray();
                    out.write(bytes);
                    
                    System.out.println("bytes--->"+bytes);
                    out.close();
                    socket.close();
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }            
        });
        
    }

转自:http://blog.csdn.net/qingzi635533/article/details/8961180

扫描二维码推送至手机访问。

版权声明:本文由微小站发布,如需转载请注明出处。

本文链接:https://jsc0.com/post/65.html

分享给朋友:

“android客户端和java服务端之间用socket来传输图片 ” 的相关文章

Android Location在GPS中的应用(二)

这一篇其实跟GPS 毫无关系。 继续上一篇的内容,讲GPS以外的东西,比如说Service的使用。比如说gps监控,它并不需要任何UI,在后台默默地运行就行。为什么不做成 Service呢?悄悄地向服务器发送用户的位置坐标是一个不错的想法,因为它完全不需要用户的干预。当然为了保留用户权利,我们应当留...

TypedArray和obtainStyledAttributes使用

TypedArray和obtainStyledAttributes使用

在编写Android自定义按钮示例基础上,如果要指定字体大小产生这样的效果: 其实是不需要自定义变量的,可以直接使用TextView的配置属性: <com.easymorse.textbutton.TextButto...

android json解析及简单例子

JSON的定义:       一种轻量级的数据交换格式,具有良好的可读和便于快速编写的特性。业内主流技术为其提供了完整的解决方案(有点类似于正则表达式 ,获得了当今大部分语言的支持),从而可以在不同平台间进行数据交换。JSON采用兼容性很高的文本格式...

实现应用程序只有在第一次启动时显示引导界面

第一次安装启动:启动页--->导航页-->主页面之后启动:启动页-->主页面实现的原理就是:在启动页面用做一个文件保存的状态,保存程序是不是第一次启动的状态。因为只是要保存一个状态,我们将这个程序是第一次打开就将他设为true,当他进入 主页面之后将他的状态未为false,因为都...

创建Popwindow弹出菜单的两种方式

创建Popwindow弹出菜单的两种方式

方法一的Activity package com.app.test02; import android.app.Activity; import android.os.Bundle; import android.view.Gravity; import android.v...

Android 学习之 开源项目PullToRefresh的使用

Android 学习之 开源项目PullToRefresh的使用

首先 下载 Android-PullToRefresh-master 下载地址  https://github.com/chrisbanes/Android-PullToRefresh 下载之...