File Directory Navigation with Android Nanohttpd lightweight server

前端 未结 2 1817
[愿得一人]
[愿得一人] 2020-12-20 09:25

With the code below, i was able to create a mobile server on android phone with the Nanohttpd lightweight server. The code basically loop through the root directory of the h

2条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-20 10:24

    i finally figure out how to do this after enough time of studying the NanoHTTPD Framework.The code below helps me to navigate within the directories in the host android device:

    @Override
            public Response serve(String uri, Method method,
                    Map header, Map parameters,
                    Map files) {
                File rootDir = Environment.getExternalStorageDirectory();
                File[] filesList = null;
                String filepath = "";
                if (uri.trim().isEmpty()) {
                    filesList = rootDir.listFiles();
                } else {
                    filepath = uri.trim();
                }
                filesList = new File(filepath).listFiles();
                String answer = "sdcard0 - TECNO P5 - WiFi File Transfer Pro";
                if (new File(filepath).isDirectory()) {
                    for (File detailsOfFiles : filesList) {
                        answer += ""
                                + detailsOfFiles.getAbsolutePath() + "
    "; } } else { } answer += "" + "uri: " + uri + " \nfiles " + files + " \nparameters " + parameters + " \nheader "; return new NanoHTTPD.Response(answer); }

    the uri parameter in the Response Method contains browser url at that point in time: Example if url displaying on the address bar is: /192.168.43.1:8080/storage/sdcard1/Smadav_2012_Rev._9.0, then uri contains /storage/sdcard1/Smadav_2012_Rev._9.0. What i did is to just pass the uri as a filepath and of course, this is not the case for first connection when the uri is empty.

提交回复
热议问题