java的TCP服务器接受来自Android客户端图像以及字符串 [英] java TCP server accepting images as well as strings from android client

查看:139
本文介绍了java的TCP服务器接受来自Android客户端图像以及字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要发送的字符串消息以及images.i正在设计一个允许用户共享图片和聊天(串)的Andr​​oid聊天应用,以及
Java服务器是监听和广播的后端。
我经历了几篇文章,但仍是一​​头雾水。
1)我怎样才能发送数据,而在receiveing​​服务器可以知道它的字符串或图像?
2)我应该有听,然后分别广播图像和字符串分开的插座
3)我可以有一个对象,它可以抓住Java服务器字符串以及在字节数组,passit图像?
请帮忙
还我很困惑应该我使用的ObjectOutputStream / DataOutputStream类来实现它。


解决方案

 保持独立的插座,使得它更简单,或者你需要syncronisation1),使用ObjectOutputStream图像这就是图像传输速度最快的方法2)使用DataOutputStream类字符串图像发送code      myfile文件=新的文件(D:\\\\ ab.jpg);
      字节[] = mybytearray新的字节[(INT)myFile.length()];
      FIS的FileInputStream =新的FileInputStream(MYFILE);
      二BufferedInputStream为=新的BufferedInputStream(FIS);
      bis.read(mybytearray,0,mybytearray.length);
      OutputStream的OS = sock.getOutputStream();
      的System.out.println(发送...);
      os.write(mybytearray,0,mybytearray.length);
      os.flush();
      os.close();图像接收code    INT文件大小= 6022386; //文件大小暂难codeD
    长启动= System.currentTimeMillis的();
    INT读取动作;
    INT电流= 0;    文件f =新的文件(D:\\\\ ab.jpg);
    f.createNewFile();
    //接收文件
    字节[] = mybytearray新的字节[文件大小]
    InputStream为= socket.getInputStream();
    FOS的FileOutputStream =新的FileOutputStream(F);
    BOS的BufferedOutputStream =新的BufferedOutputStream(FOS);
    读取动作= is.​​read(mybytearray,0,mybytearray.length);
    电流=读取动作;
   做{
       读取动作=
       is.read(mybytearray,电流,(mybytearray.length电流));
       如果(读取动作> = 0)电流+ =读取动作;
    }而(读取动作-1个);
    数=电流;
    复制= mybytearray.clone();
    bos.write(mybytearray,0,电流);
    bos.flush();
    长端= System.currentTimeMillis的();
    的System.out.println(年底启动);
    bos.close();
    fos.close();
    状态=真;

i want to send string messages as well as images.i am designing android chatting application which user is allowed to share images and chat(string) as well Java Server is at the backend which listens and broadcasts. I am going through several articles but still confused. 1)how can i send data and while at receiveing server can know its string or image? 2)should i have separate sockets for listening and then broadcasting for images and strings respectively 3)can i have an object which can hold strings as well as images in byte array and passit on to java server? Please help also i am confused that should i use objectoutputstream/dataoutputstream to achieve it.

解决方案

Keep Seperate Sockets that makes it simpler,or you need syncronisation

1)use objectoutputstream for image thats fastest method of image transfer

2)use dataoutputstream for string

Image Sending code

      File myFile = new File ("d:\\ab.jpg");
      byte [] mybytearray  = new byte [(int)myFile.length()];
      FileInputStream fis = new FileInputStream(myFile);
      BufferedInputStream bis = new BufferedInputStream(fis);
      bis.read(mybytearray,0,mybytearray.length);
      OutputStream os = sock.getOutputStream();
      System.out.println("Sending...");
      os.write(mybytearray,0,mybytearray.length);
      os.flush();
      os.close();

Image receiving code

    int filesize=6022386; // filesize temporary hardcoded
    long start = System.currentTimeMillis();
    int bytesRead;
    int current = 0;

    File f=new File("d:\\ab.jpg");
    f.createNewFile();
    // receive file
    byte [] mybytearray  = new byte [filesize];
    InputStream is = socket.getInputStream();
    FileOutputStream fos = new FileOutputStream(f);
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    bytesRead = is.read(mybytearray,0,mybytearray.length);
    current = bytesRead;
   do {
       bytesRead =
       is.read(mybytearray, current, (mybytearray.length-current));
       if(bytesRead >= 0) current += bytesRead;
    } while(bytesRead > -1);
    count=current;
    Copy=mybytearray.clone();
    bos.write(mybytearray, 0 , current);
    bos.flush();
    long end = System.currentTimeMillis();
    System.out.println(end-start);
    bos.close();
    fos.close();
    Status=true;

这篇关于java的TCP服务器接受来自Android客户端图像以及字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆