我想将图像文件从 android 传输到 C 服务器。当我尝试发送文件大小时发送文件名-> 文件大小-> 图像文件,它与图像文件连接。我不确定为什么。我看不出发送文件名和文件大小之间的区别。当我发送文件名时,它不会与文件大小连接。
从java端(android)
DataOutputStream imgBodyOut = new DataOutputStream(client.getOutputStream());
                //sending name of the file
                buffer=fileName.getBytes();
                imgBodyOut.write(buffer);
                imgBodyOut.flush();
                //sending size of the file
                buffer=((Long.toString(filesize))+'\n').getBytes();
                imgBodyOut.write(buffer);
                imgBodyOut.flush();
                //sending body
                DataInputStream imgBodyIn = new DataInputStream(new FileInputStream(imgFile));
                int len;
                while(filesize >0&& (len=imgBodyIn.read(buffer))>0){
                    imgBodyOut.write(buffer,0,len);
                    imgBodyOut.flush();
                    filesize-=len;
                }
                imgBodyOut.flush();
                imgBodyIn.close(); 
                imgBodyOut.close();
C(server side) //接收文件名
read(connfd,filename_str,sizeof(filename_str));
printf("File name : %s.JPG\n",filename_str);
//receiving file size
read(connfd,filesize_str,sizeof(filesize_str));
printf("File size : %sbytes\n",filesize_str);
//Test filesize
        int i=0;
        while(i<1024){
            printf("%c",filesize_str[i]);
            i++;
        }
结果在 C
File name : P1011474.JPG
File size : 714438bytes
714438����8EExifII*
                   �����(1 �2i�&��2�OLYMPUS DIGITAL CAMERA         OLYMPUS CORPORATIONX100,D540Z,C310ZHHv775u-770000:00:00 00:00:00PrintIM0250�
�
����������� '
                                   '�'''^'�'�''!������"�'�d�0220��������
���� �
�|�H��}�0100��������������    �
�
�
由于连接,我无法发送图像。感谢您的阅读。