Lorenzo Sogliani
Lorenzo Sogliani

Reputation: 227

Error Receiving File over Socket Android/Java

I'm trying to send a file from an Android to a computer terminal with java server. I leave you wrote the code below to use to send and receive files and the consideration received error.

Android (Sender-Client):

                byte[] mybytearray = new byte[(int) selectedFile.length()];
                BufferedInputStream bis = new BufferedInputStream(new FileInputStream(selectedFile));
                bis.read(mybytearray, 0, mybytearray.length);
                OutputStream os = socket2.getOutputStream();
                os.write(mybytearray, 0, mybytearray.length);
                os.flush();

Java(Receiver-Server):

                    76)int lenghtf = Integer.parseInt(lenght);
                    77)byte[] mybytearray = new byte[lenghtf];
                    78)InputStream is = socket.getInputStream();
                    79) FileOutputStream fos = new FileOutputStream(namef);
                    80) BufferedOutputStream bos = new BufferedOutputStream(fos);
                    81) int bytesRead = is.read(mybytearray, 0, mybytearray.length);
                    82) bos.write(mybytearray, 0, bytesRead);
                    83)bos.close();

I get this error

enter image description here

Upvotes: 1

Views: 286

Answers (1)

Lorenzo Sogliani
Lorenzo Sogliani

Reputation: 84

Receiver:

FileOutputStream out1 = new FileOutputStream("File Ricevuti\\"+namef);
                    byte[] buf = new byte[socket.getReceiveBufferSize()];
                    int len = 0;
                    while ((len = inp.read(buf)) != -1) {
                        out1.write(buf, 0, len);
                        out1.flush();
                        //out1.flush();
                    }

                    out1.close();

Sender:

 InputStream in =  new FileInputStream( f );

                byte[] buf = new byte[socket2.getSendBufferSize()];
                int len = 0;
                while ((len = in.read(buf)) != -1) {
                    out.write(buf, 0, len);

                }
                in . close ();
                out.close();

Upvotes: 1

Related Questions