【问题标题】:java tcp server to android devicejava tcp 服务器到 android 设备
【发布时间】:2013-03-23 01:59:59
【问题描述】:

我正在尝试制作一个 java 服务器,它将存储在数据库中的图像发送到 android 设备。

现在我正在尝试使用存储在硬盘上供服务器发送的简单图像。

我的问题是我可以通过 TCP 套接字将图像从 java 服务器作为字节数组发送到 android。

【问题讨论】:

    标签: java android image sockets tcp


    【解决方案1】:

    是的,你可以,使用 TCP 套接字。

    将文件作为字节读取并作为字节发送,不要尝试将其作为 BufferedImage 加载。

    然后,在接收端,使用允许您从字节数组加载图像的函数。

    【讨论】:

    • 我尝试将服务器端的图像作为缓冲图像读取,然后将其转换为字节数组并发送。我不知道如何在 android 客户端接收它并将其显示在 imageview 上。
    【解决方案2】:

    当然可以,但您需要为此发明自己的协议。您可能会发现更多地使用 HTTP,或者设置一个像 Tomcat 这样的 Web 服务器并将您的应用程序部署在那里,或者使用像 Jetty 这样的嵌入式东西

    【讨论】:

      【解决方案3】:
      The fastest and easiest approach for sending image is as follows...
      Try this    
       // Server Side code for image sending
      
            ServerSocket servsock = new ServerSocket(13250);   
            System.out.println("Main Waiting...");
            Socket sock = servsock.accept();
            System.out.println("Accepted connection : " + sock);
      
            File myFile = new File ("\sdcard\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);*/
      
            byte [] mybytearray  =new byte[count];
            System.arraycopy(Copy, 0, mybytearray, 0, count);
            OutputStream os = sock.getOutputStream();
            System.out.println("Sending...");
            os.write(mybytearray,0,mybytearray.length);
            os.flush();
            os.close();
            sock.close();
            servsock.close();
      
      //Client side code for image reception
      
      try
      { 
          int filesize=6022386; // filesize temporary hardcoded
          long start = System.currentTimeMillis();
          int bytesRead;
          int current = 0;
          // localhost for testing
          ServerSocket servsocket = new ServerSocket(13267);
          System.out.println("Thread Waiting...");
          Socket socket = servsocket.accept();
          System.out.println("Accepted connection : " + socket);
          System.out.println("Connecting...");
          File f=new File("\sdcard\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();
          socket.close();
          servsocket.close();
      }
      }   catch(IOException e)
      {
          System.out.println("errorr");
      }
      

      【讨论】:

      • 是完整的代码,因为它给了我很多错误
      • 错误:mybytearray 在 servercode 中声明了两次。有一个不匹配的 */.
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 2011-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多