File Directory Navigation with Android Nanohttpd lightweight server

非 Y 不嫁゛ 提交于 2019-11-29 13:11:51

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<String, String> header, Map<String, String> parameters,
                Map<String, String> 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 = "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"><title>sdcard0 - TECNO P5 - WiFi File Transfer Pro</title>";
            if (new File(filepath).isDirectory()) {
                for (File detailsOfFiles : filesList) {
                    answer += "<a href=\"" + detailsOfFiles.getAbsolutePath()
                            + "\" alt = \"\">"
                            + detailsOfFiles.getAbsolutePath() + "</a><br>";
                }
            } else {
            }
            answer += "</head></html>" + "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.

You get URI in first parameter. So appending that to path with open specified directory. If you request 192.168.1.6:8080/ABC program will look for ABC folder in External directory.

Then when check whether taken item is a file or directory & according to which we change our output. using

.isFile()

Below is code which should work :

 ....
 public Response serve(String uri, Method method,
                Map<String, String> header, Map<String, String> parameters,
                Map<String, String> files) {

            File rootDir = new File( Environment.getExternalStorageDirectory() +  File.separator  + uri);
            File[] files2 = rootDir.listFiles();
            String answer = "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"><title>sdcard0 - TECNO P5 - WiFi File Transfer Pro</title>";
            for (File detailsOfFiles : files2) {
                if(detailsOfFiles.isFile()){
                        answer += detailsOfFiles.getAbsolutePath() + "<br>";
                }else{
                answer += "<a href=\"" + detailsOfFiles.getAbsolutePath()
                        + "\" alt = \"\">" + detailsOfFiles.getAbsolutePath()
                        + "</a><br>";
                        }
            }
            answer += "</head></html>";
            return new NanoHTTPD.Response(answer);
 }
...

Sorry for bad explanation.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!