简体   繁体   English

无法从Android上的套接字打开接收到的amr / jgp文件

[英]Can't open received amr/jgp file from socket on android

To send and receive file via socket over Wifi I have used following code... 为了通过Wifi通过套接字发送和接收文件,我使用了以下代码...

Client side : 客户端 :

Socket socket = new Socket(dstAddress, dstPort);
            int bufferSize=socket.getReceiveBufferSize();
            InputStream in=socket.getInputStream();
            DataInputStream clientData = new DataInputStream(in);
            String fileName = clientData.readUTF();
            System.out.println(fileName);
            OutputStream output = new FileOutputStream("/sdcard/"+fileName);

            byte[] buffer = new byte[bufferSize];
            int read;
            while((read = clientData.read(buffer)) != -1){
                output.write(buffer, 0, read);
            }


            //close every thing
            output.flush();
            output.close();
            socket.close();

Server side: 服务器端:

File file = new File(Environment.getExternalStorageDirectory(), "/sdcard/test.amr");

            byte[] mybytearray = new byte[8092];
            FileInputStream fis = new FileInputStream(file);
            BufferedInputStream bis = new BufferedInputStream(fis);
            DataInputStream dis = new DataInputStream(bis);
            OutputStream os;
            try {
                os = socket.getOutputStream();
                DataOutputStream dos = new DataOutputStream(os);
                dos.writeUTF(file.getName());
                dos.writeLong(mybytearray.length);
                int read;
                while((read = dis.read(mybytearray)) != -1){
                    dos.write(mybytearray, 0, read);

                }
                os.flush();

                os.close();
                socket.close();

At this point I am receiving file 'test.amr' from server without change of its original size. 此时,我正在从服务器接收文件“ test.amr”,而没有更改其原始大小。 But when I try to play the file in client device it can't be played. 但是,当我尝试在客户端设备中播放文件时,无法播放。

Note : mp3, avi and txt file received using above code can be opened and played perfectly. 注意:使用上述代码接收的mp3,avi和txt文件可以打开并完美播放。

Please suggest how to solve this issue. 请建议如何解决此问题。

Finally I have solved the above problem by just removing dos.writeLong(mybytearray.length); 最后,我通过删除dos.writeLong(mybytearray.length);解决了上述问题。 line form server code... as per suggestion of greenapps. 线形服务器代码...根据greenapps的建议。

My revised server code is: 我修改后的服务器代码是:

File file = new File(Environment.getExternalStorageDirectory(), "/sdcard/test.amr");

        byte[] mybytearray = new byte[8092];
        FileInputStream fis = new FileInputStream(file);
        BufferedInputStream bis = new BufferedInputStream(fis);
        DataInputStream dis = new DataInputStream(bis);
        OutputStream os;
        try {
            os = socket.getOutputStream();
            DataOutputStream dos = new DataOutputStream(os);
            dos.writeUTF(file.getName());
            int read;
            while((read = dis.read(mybytearray)) != -1){
                dos.write(mybytearray, 0, read);

            }
            os.flush();

            os.close();
            socket.close();

You're writing the buffer size as a long but you're not reading it. 您正在写long的缓冲区大小,但没有读取它。 So the long is being read as part of the image. 因此, long作为图像的一部分被读取。 As writing your sending buffer size has no conceivable relevance to the receiver, you should remove the writeLong(mybytearray.length) call from the sender. 由于写入发送缓冲区的大小与接收者没有任何关联,因此应从发送者中删除writeLong(mybytearray.length)调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM