Search code examples
javaandroidsocketsfile-transfer

Can't open file received from socket android java


I am making an app with socket in which i want to share data from two devices in the same wifi. With my code i can share the file successfully between two devices with the exact size of the file and saves into the device storage with a specific name but when i try to open it fails to open in the file manager. I have tried this with mp4 file and apk files

Here is the sender's code

        @Override
        public void run() {
            //File file = new File(src);
            File file = new File(Environment.getExternalStorageDirectory() + "/c.mp4");

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


                //os.write(bytes, 0, bytes.length);  //commented
                //os.flush();   //commented
                socket.close();

                final String sentMsg = "File sent to: " + socket.getInetAddress();
                MainActivity.this.runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        Toast.makeText(MainActivity.this, sentMsg, Toast.LENGTH_LONG).show();
                    }});

            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                try {
                    socket.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        

        
    

And here is the receiver



        @Override
        public void run() {
            Socket socket = null;
            int bytesRead;
            InputStream in; //changed
            int bufferSize=0;

            try {
                socket = new Socket(dstAddress, dstPort);

                bufferSize = socket.getReceiveBufferSize();
                in = socket.getInputStream();
                DataInputStream clientData = new DataInputStream(in);
                File file = new File(Environment.getExternalStorageDirectory(), "c.mp4");
                OutputStream output = new FileOutputStream(file);
                byte[] buffer = new byte[bufferSize];
                int read;
                while ((read = clientData.read(buffer)) != -1){
                    output.write(buffer, 0 , read);
                }


                //bos.close();  //commented
                socket.close();

                MainActivity2.this.runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        Toast.makeText(MainActivity2.this, "Finished", Toast.LENGTH_LONG).show();
                    }});

            } catch (IOException e) {

                e.printStackTrace();

                final String eMsg = "Something wrong: " + e.getMessage();
                MainActivity2.this.runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        Toast.makeText(MainActivity2.this,
                                eMsg,
                                Toast.LENGTH_LONG).show();
                    }});

            } finally {
                if(socket != null){
                    try {
                        socket.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        
    




Solution

  • You need to read the clientData (DataInputStream) in the same way you write it to the output stream of the socket.

    uft-8 string, long value, seq. of bytes

    Your mp4 file content will prefixed with "anything" and length. so it will be considered as corrupted file.

    save the file without ("anything" and length)