通过Socket Android / Java接收文件时出错

时间:2014-08-29 11:57:40

标签: java android file sockets send

我正在尝试将文件从Android发送到带有java服务器的计算机终端。 我留下你写下面的代码用来发送和接收文件以及收到的代价错误。

Android(发件人 - 客户端):

                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(接收服务器):

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

我收到此错误

enter image description here

1 个答案:

答案 0 :(得分:1)

接收器:

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

发信人:

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