简体   繁体   中英

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

在此处输入图片说明

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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