8

嗨,我的最终目标是在两个设备之间使用 android 中的 wifi direct api 传输文件。一旦设备充当客户端,另一个设备充当服务器,就像 wifi direct sdk 演示中一样。为此,使用服务器端口和主机地址从客户端创建套接字。我想传输多个文件。在接收方接受客户端套接字连接时,我必须使用从客户端发送的文件的文件名创建文件。但我不知道服务器端的文件名。

那么如何使用套接字连接发送文件名,用于这种 wifi 直接传输模式以进行多个文件传输。

使用服务器端口和主机地址从客户端创建套接字:

fileUris = intent.getExtras().getParcelableArrayList(EXTRA_STREAM);
        String host = intent.getExtras().getString(
                EXTRAS_GROUP_OWNER_ADDRESS);
        Socket socket = new Socket();
        int port = intent.getExtras().getInt(EXTRAS_GROUP_OWNER_PORT);

        try {
            Log.d(WifiDirectActivity.TAG, "Opening client socket - ");
            socket.bind(null);
            socket.connect((new InetSocketAddress(host, port)),
                    SOCKET_TIMEOUT);

            Log.d(WifiDirectActivity.TAG,
                    "Client socket - " + socket.isConnected());
            OutputStream stream = socket.getOutputStream();
            ContentResolver cr = context.getContentResolver();
            InputStream is = null;
            for (int i = 0; i < fileUris.size(); i++) {
                Uri uri = fileUris.get(0);

                try {
                    is = cr.openInputStream(Uri.parse(uri.toString()));
                } catch (FileNotFoundException e) {
                    Log.d(WifiDirectActivity.TAG, e.toString());
                }
                DeviceDetailFragment.copyFile(is, stream);
                Log.d(WifiDirectActivity.TAG, "Client: Data written");
            }

从服务器端接受客户端套接字连接:

    ServerSocket serverSocket = new ServerSocket(8988);
            Log.d(WifiDirectActivity.TAG, "Server: Socket opened");
            Socket client = serverSocket.accept();

            Log.d(WifiDirectActivity.TAG,
                    "Server: connection done with client");

            final File f = new File(
                    Environment.getExternalStorageDirectory() + "/"
                            + context.getPackageName() + "/wifip2pshared-"
                            + "sample");
            File dirs = new File(f.getParent());
            if (!dirs.exists())
                dirs.mkdirs();
            f.createNewFile();

            Log.d(WifiDirectActivity.TAG,
                    "server: copying files " + f.toString());
            InputStream inputstream = client.getInputStream();                          

            copyFile(inputstream, new FileOutputStream(f));
            serverSocket.close();

在文件创建时在接收方提供文件名真的很受打击。有什么方法可以发送文件名。请帮助我。提前致谢。

4

1 回答 1

2

您可以创建一个嵌入文件名和实际数据的捆绑对象。像这样的东西:

    public class WiFiDirectBundle extends Serializable {
        private String fileName;
        private String mimeType;
        private Long fileSize;
        private byte[] fileContent;

        public WiFiDirectBundle() {}

        // adds a file to the bundle, given its URI
        public void setFile(Uri uri) {
            File f = new File(Uri.parse(uri.toString()));

            fileName = f.getName();
            mimeType = MimeTypeMap.getFileExtensionFromUrl(f.getAbsolutePath());
            fileSize = f.length();

            FileInputStream fin = new FileInputStream(f);        
            fileContent = new byte[(int) f.length()];
            fin.read(fileContent);
        }

        // restores the file of the bundle, given its directory (change to whatever
        // fits you better)
        public String restoreFile(String baseDir) {
            File f = new File(baseDir + "/" + fileName);
            try {
                FileOutputStream fos = new FileOutputStream(f);
                if (fileContent != null) {
                    fos.write(fileContent);
                }

                fos.close();
                return f.getAbsolutePath();

            } catch (IOException e) {
                    e.printStackTrace();
            }

            return null;
        }

        public String getFileName() {
            return fileName;
        }

        public String getMimeType() {
            return mimeType;
        }

        public Long getFileSize() {
            return fileSize;
        }
    }

WiFiDirectBundle然后,您可以通过简单地使用输入和输出流来来回传递一个实例。当您收到一个对象时,您必须将它显式地强制转换为WiFiDirectBundle该类。

我知道这并不优雅,但它确实有效。

于 2013-04-17T08:58:48.090 回答