从Android客户端发送文件到c#服务器,在读取整个文件之前中断

本文关键字:文件 读取 中断 服务器 客户端 Android | 更新日期: 2023-09-27 18:10:14

c# code

 private MemoryStream Read() {
        NetworkStream clientStream = _client.GetStream();
        MemoryStream messageStream = new MemoryStream();
    var inbuffer = new byte[65535];
    if(clientStream.CanRead) {
        do {
            int bytesRead = clientStream.Read(inbuffer,0,inbuffer.Length);
            messageStream.Write(inbuffer, 0, bytesRead);
            messageStream.Flush();
            Debug.Print(messageStream.Length + " added " + bytesRead);
        } while(clientStream.DataAvailable); // GOES BEYOND HERE, EVEN THOUGH THERE'S MORE DATA
    }
    messageStream.Position = 0;
    return messageStream;
}
android代码

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                if(resultCode == RESULT_OK){                   
                        if(requestCode == ATTACH_FILE_REQUEST){
                                 writeToSocket("<FILE>generic.jpeg");
                                 InputStream input = null;
                                 String filePath = data.getData().getPath().toString();
                                 try {
                                        input = getContentResolver().openInputStream(data.getData());                  
                                        BufferedInputStream bufferedInputStream = new BufferedInputStream(input);
                                        byte[] bytes = new byte[65535];
                                        ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
                                                while((bufferedInputStream.read(bytes)) != -1){
                                                         byteArray.write(bytes, 0, bytes.length);                                
                                                 }
                                                socketNetworkHelper.writeFile(byteArray.toByteArray());
                                } catch (FileNotFoundException e1) {
                                        // TODO Auto-generated catch block
                                        e1.printStackTrace();
                                        Toast.makeText(this, "Error in file selection", Toast.LENGTH_LONG).show();
                                } catch (IOException e2){
                                        e2.printStackTrace();
                                        Toast.makeText(this, "Input/Output Error", Toast.LENGTH_LONG).show();
                                }        
                        }
                }
}

在SocketNetworkHelper.java中有一个套接字连接:

public void writeFile(byte[] buffer) {
                try {
                        socket.getOutputStream().write(buffer);
                } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
        }

问题是:我怎样才能得到整个文件?

从Android客户端发送文件到c#服务器,在读取整个文件之前中断

您也可以将文件大小作为参数发送

private MemoryStream Read(int filesize) {
            NetworkStream clientStream = _client.GetStream();
            MemoryStream messageStream = new MemoryStream();
            var inbuffer = new byte[filesize];
            if (clientStream.CanRead) {
                int totalBytesRead = 0;
                do {
                    int bytesRead = clientStream.Read(inbuffer, 0, inbuffer.Length);
                    totalBytesRead += bytesRead;
                    messageStream.Write(inbuffer, 0, bytesRead);
                    messageStream.Flush();
                } while (totalBytesRead < filesize);
            }
            messageStream.Position = 0;
            return messageStream;