0

我创建了一个应用程序,它通过套接字编程将图像从服务器(桌面)发送到客户端(android)............问题是我在客户端(android)获取文件,但没有内容。

谁能告诉我有什么问题

客户端(安卓)

    DataInputStream dis=new DataInputStream(socket.getInputStream());
    receiveFile(dis); // call method receiveFile()

public Bitmap receiveFile(InputStream is) throws Exception{
                 String baseDir =     Environment.getExternalStorageDirectory().getAbsolutePath();
                    String fileName = "myFile.png";
                    String imageInSD = baseDir + File.separator + fileName;
                    System.out.println("FILE----------------->"+imageInSD);
                  int filesize=6022386;
                  int bytesRead;
                  int current = 0;
                  byte [] data  = new byte [filesize];

                    FileOutputStream fos = new FileOutputStream(imageInSD);
                    BufferedOutputStream bos = new BufferedOutputStream(fos);
                    bytesRead = is.read(data,0,data.length);
                    current = bytesRead;
                    int index = 0;
                    while (index < filesize)
                    {
                        bytesRead = is.read(data, index, filesize - index);
                        if (bytesRead < 0)
                        {
                            throw new IOException("Insufficient data in stream");
                        }
                        index += filesize;
                    }

                    bos.write(data, 0 , current);  
                    bos.flush();
                    bos.close();
                    return null;
              }

服务器(桌面)

send(socket.getOutputStream()); // call method send()


    public void send(OutputStream os) throws Exception{
      // sendfile
      File myFile = new File ("C:/div.png");
      System.out.println("the file is read");
      byte [] mybytearray  = new byte [(int)myFile.length()+1];
      FileInputStream fis = new FileInputStream(myFile);
      BufferedInputStream bis = new BufferedInputStream(fis);
      bis.read(mybytearray,0,mybytearray.length);
      System.out.println("Sending...");
      os.write(mybytearray,0,mybytearray.length);
      os.flush();
      }
4

2 回答 2

0

Java中复制流的正确方法如下:

while ((count = in.read(buffer)) > 0)
{
   out.write(buffer, 0, count);
}

目前您的代码:

  1. 假设read()填充缓冲区。Javadoc 中没有这样说。
  2. 忽略 的返回结果read(),它除了是无价的计数之外,还可以是 -1 表示 EOS。
  3. 浪费地分配整个文件大小的缓冲区。
  4. 假设文件的大小适合int.
  5. 依靠接收器神奇地知道传入文件的大小。

上面的代码没有做任何这些假设,并且适用于从 1 开始的任何缓冲区大小。

于 2013-09-03T00:36:05.930 回答
-1

查看您的代码,我看到您希望接收一个文件,将其保存到外部存储并返回该文件的位图。这就是我猜你想要做的,但你的代码并没有这样做。如果您愿意,可以使用以下代码来完成该任务。首先,服务器发送 4 个字节指示文件的大小,然后是文件的内容;客户端读取这 4 个字节,然后读取整个文件,并将其保存到磁盘它读取的每个块。最后,它将接收到的文件转换为位图并返回。

客户端代码:

public Bitmap receiveFile(InputStream is) throws Exception
    {
        String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
        String fileName = "myFile.png";
        String imageInSD = baseDir + File.separator + fileName;
        System.out.println("FILE----------------->" + imageInSD);

        // read first 4 bytes containing the file size
        byte[] bSize = new byte[4];
        is.read(bSize, 0, 4);

        int filesize;
        filesize = (int) (bSize[0] & 0xff) << 24 | 
                   (int) (bSize[1] & 0xff) << 16 | 
                   (int) (bSize[2] & 0xff) << 8 | 
                   (int) (bSize[3] & 0xff);

        int bytesRead;
        // You may but don't have to read the whole file in memory
        // 8k buffer is good enough
        byte[] data = new byte[8 * 1024];
        int bToRead;
        FileOutputStream fos = new FileOutputStream(imageInSD);
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        while (filesize > 0)
        {
            // EDIT: just in case there is more data in the stream. 
            if (filesize > data.length) bToRead=data.length;
            else bToRead=filesize;
            bytesRead = is.read(data, 0, bToRead);
            if (bytesRead > 0)
            {
                bos.write(data, 0, bytesRead);
                filesize -= bytesRead;
            }
        }
        bos.close();
        // I guess you want to return the received image as a Bitmap
        Bitmap bmp = null;
        FileInputStream fis = new FileInputStream(imageInSD);
        try
        {
            bmp = BitmapFactory.decodeStream(fis);
        }
        catch (Exception e)
        {
            // in case of an error set it to null
            bmp = null;
        }
        finally
        {
            fis.close();
        }
        return bmp;
    }

服务器代码:

public void send(OutputStream os) throws Exception
{
    // sendfile
    File myFile = new File("C:/div.png");
    System.out.println("the file is read");
    int fSize = (int) myFile.length();
    byte[] bSize = new byte[4];
    bSize[0] = (byte) ((fSize & 0xff000000) >> 24);
    bSize[1] = (byte) ((fSize & 0x00ff0000) >> 16);
    bSize[2] = (byte) ((fSize & 0x0000ff00) >> 8);
    bSize[3] = (byte) (fSize & 0x000000ff);

    // send 4 bytes containing the filesize
    os.write(bSize, 0, 4);

    byte[] mybytearray = new byte[(int) fSize];
    FileInputStream fis = new FileInputStream(myFile);
    BufferedInputStream bis = new BufferedInputStream(fis);
    int bRead = bis.read(mybytearray, 0, mybytearray.length);
    System.out.println("Sending...");
    os.write(mybytearray, 0, bRead);
    os.flush();
    bis.close();
}
于 2013-09-02T19:17:48.027 回答