我试图在android上直接使用wifi发送文件,但仅发送和接收文件名。
这是我的代码
发件人

Socket socket = new Socket();
        int port = intent.getExtras().getInt(EXTRAS_GROUP_OWNER_PORT);
        FileInputStream fis = null;
        BufferedInputStream bis = null;
        DataInputStream dis = null;
        OutputStream os = null;
        DataOutputStream dos = null;
        try {
            //socket.bind(null);
            socket.connect((new InetSocketAddress("192.168.49.1", port)), SOCKET_TIMEOUT);
            ContentResolver cr = context.getContentResolver();
            Uri uri = null;
            uri = uri.parse(FileUri);
            File myFile = new File(uri.getPath());
            byte[] mybytesarray = new byte[(int) myFile.length()];
            fis = new FileInputStream(myFile);
            bis = new BufferedInputStream(fis);
            dis = new DataInputStream(bis);
            dis.readFully(mybytesarray, 0, mybytesarray.length);
            os = socket.getOutputStream();
            dos = new DataOutputStream(os);
            dos.writeUTF(myFile.getName()); //write fileName
            dos.writeLong(mybytesarray.length); //write file length/size
            int read;
            while ((read = dis.read(mybytesarray)) > 0) {
                dos.write(mybytesarray, 0, read);
            }
        } catch (IOException e) {
                Log.e(WiFiDirectActivity.TAG, e.getMessage());
            } finally {
            if (socket != null) {
                if (socket.isConnected()) {
                    try {
                        dos.close();
                        os.close();
                        dos.close();
                        socket.close();
                        Log.d(WiFiDirectActivity.TAG, "socket close");
                    } catch (IOException e) {
                        // Give up
                        e.printStackTrace();
                    }

接收者
        int current = 0;
        ServerSocket serverSocket = null;
        InputStream in = null;
        OutputStream output = null;
        DataInputStream ClientData;
        try{
        serverSocket = new ServerSocket(8988);
            Socket ClientSocket = null;
            ClientSocket = serverSocket.accept();
            in = ClientSocket.getInputStream();
            ClientData = new DataInputStream(in);
            String FileName = ClientData.readUTF();
            output = new FileOutputStream(Environment.getExternalStorageDirectory()+"/" + FileName);
            long size = ClientData.readLong();
            byte[] buffer = new byte[8192];
            int bytesRead;
            while((bytesRead = ClientData.read(buffer)) > 0){
                output.write(buffer, 0, bytesRead);
            }
            output.flush();
            in.close();
            output.close();
            ClientSocket.close();
            serverSocket.close();
            return FileName;
        } catch (IOException e) {
            Log.e(WiFiDirectActivity.TAG, e.getMessage());
            return null;
        }

最佳答案

            while ((read = dis.read(mybytearray)) > 0) {
                dos.write(mybytearray, 0, mybytearray.length);
                Log.d(WiFiDirectActivity.TAG, "sending");
                dos.flush();
            }


        long size = ClientData.readLong();
        byte[] buffer = new byte[1024];
        while((size = ClientData.read(buffer)) > 0){
            output.write(buffer, 0, bytesRead);
            size = bytesRead;
        }

您已经弄乱了这段代码,并且在链接的问题中使用了错误的答案:请参阅我的大量评论。它应该是:
            while ((read = dis.read(mybytearray)) > 0) {
                dos.write(mybytearray, 0, read);
                Log.d(WiFiDirectActivity.TAG, "sending");
            }

(切勿在循环内冲洗),以及
        long size = ClientData.readLong();
        byte[] buffer = new byte[8192]; // at least
        int bytesRead;
        while((bytesRead = ClientData.read(buffer)) > 0){
            output.write(buffer, 0, bytesRead);
        }

关于java - 如何通过Socket Java传输数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30808260/

10-12 06:03