【问题标题】:Android socket send big image and lose dataAndroid套接字发送大图像并丢失数据
【发布时间】:2012-12-10 17:26:40
【问题描述】:

现在它在终端之间发送图像。客户端将图片翻译成字节流,然后分发给服务器。服务器找到发送人并将数据发送到终端。现在在局域网的两站模拟器中发送大图。 发送和接收没有问题。部署到网络服务器,Server接收代码。

int length = 0;
int totalNum = 0;
byte[] buffer = new byte[1024];
while ((length = dis.readInt()) != 0) {
    length = dis.read(buffer, 0, length);
    System.out.println("length :-------->" + length);

    totalNum += length;
    out.writeInt(length);
    out.write(buffer, 0, length);
    out.flush();
}
System.out.println("totalNum:-------->" + totalNum);
out.writeInt(0);
out.flush();
Debug.info("totalNum::::" + totalNum);
initService.getEnterpriseMsgService().save(msg);

它每次接收 1024 个字节。有时对System.out.println("length :-------->" + length); 它是空的。模拟器每次发送和接收数据都是一致的。当我发送大图时,它是没有问题的。不知道是代码问题还是服务器问题。

寻求解决方案。提前致谢。

【问题讨论】:

    标签: android send image


    【解决方案1】:

    在发送和接收图像的情况下。您必须增加缓冲区大小

    byte[] buffer = new byte[4096];
    

    【讨论】:

      【解决方案2】:

      为什么不试试这种方式:

      InputStream is //your InputStream
      OutputStream out //your OutputStream
      byte[] buffer = new byte[1024];
      int length = 0;
      try {
          while ((length = is.read(buffer)) > 0) {
              out.write(buffer, 0, length);
          }
      
      } catch (Exception e) {
          // TODO: handle exception
      }
      

      【讨论】:

        猜你喜欢
        • 2015-05-18
        • 2014-12-21
        • 1970-01-01
        • 2021-09-01
        • 2017-09-14
        • 1970-01-01
        • 2021-06-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多