在套接字上发送和接收文件

2022-01-19 00:00:00 sockets network-programming android java

我正在从 java 服务器向远程 Android 客户端发送文件.我使用输出流写入字节.在读取这些字节时,read() 方法会在流结束后继续尝试读取字节.如果我在服务器端关闭 outputstream,读取操作工作正常.但是我必须再次在同一个套接字上写入文件所以无法关闭输出流任何解决方案?

I am sending files to remote Android client from java server. I write the bytes using outputstream. On reading these bytes read() method keep trying to read bytes after the stream is ended. if I close the outputstream on server-side, read operation work fines. But I have to write file on the same socket again so can't close output stream any solution?

注意:我的代码适用于共享单个文件

编写文件的代码

public static void writefile(String IP, String filepath, int port, OutputStream out) throws IOException {
    ByteFileConversion bfc = new ByteFileConversion();
    byte[] file = bfc.FileToByteConversion(filepath);

    out.write(file, 0, file.length);
    out.close(); // i donot want to close this and how can I tell reading side that stream is ended.
    System.out.println("WRITTEN");
}

这是我在 Android 上阅读的文件:

Here Am I reading the file on Android :

public Bitmap fileReceived(InputStream is) {

    Bitmap bitmap = null;
    String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
    String fileName = "a.png";
    String imageInSD = baseDir + File.separator + fileName;
    //  System.out.println(imageInSD);
    if (is != null) {
        FileOutputStream fos = null;
        OutputStream bos = null;
        try {

            bos = new FileOutputStream(imageInSD);

            byte[] aByte = new byte[1024];
            int bytesRead;
            int index = 0;
            DataInputStream dis = new DataInputStream(is);


            while ((bytesRead = is.read(aByte)) > 0) {
                index = bytesRead + index;
                bos.write(aByte, 0, bytesRead);

                //  index = index+ bytesRead;

                System.out.println("Loop" + aByte + "    byte read are " + bytesRead + "whree  index =" + index);

            }
            bos.flush();
            bos.close();

            Log.i("IMSERVICE", "out of loop");
            java.io.FileInputStream in = new FileInputStream(imageInSD);
            bitmap = BitmapFactory.decodeStream(in);
            bitmap = BitmapFactory.decodeFile(imageInSD);

            Log.i("IMSERVICE", "saved");
            //  if (bitmap != null) 
            //       System.out.println("bitmap is    "+ bitmap.toString());

        } catch (IOException ex) {
            // Do exception handling      
            //      Log.i("IMSERVICE", "exception ");
            System.out.println("ex");
        }
    }

    return bitmap;
}

其实我想重置socket连接

提前致谢

推荐答案

你需要:

  1. 在文件之前发送文件的长度.您可以为此使用 DataOutputStream.writeLong(),在接收方使用 DataInputStream.readLong().
  2. 在接收端从流中准确读取那么多字节:

  1. Send the length of the file ahead of the file. You can use DataOutputStream.writeLong() for that, and DataInputStream.readLong() at the receiver.
  2. Read exactly that many bytes from the stream at the receiver:

while (total < length && (count = in.read(buffer, 0, length-total > buffer.length ? buffer.length : (int)(length-total))) > 0)
{
    out.write(buffer, 0, count);
    total += count;
}

E&OE

其实我是想重置socket连接

Actually I want to reset socket connection

其实你不想做任何这样的事情.

Actually you don't want to do any such thing.

相关文章