文件目录导航与Android Nanohttpd轻量级​​服务器 [英] File Directory Navigation with Android Nanohttpd lightweight server

查看:1016
本文介绍了文件目录导航与Android Nanohttpd轻量级​​服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过下面的code,我能够创造Android手机在移动服务器与Nanohttpd轻量级​​服务器。在code通过主机Android设备的根目录基本回路,并列出文件和文件夹的链接。我想实现是当用户点击任何链接(文件夹链接),浏览器应该显示包含在点击文件夹链接的文件和文件夹。我如何做到这一点,因为我找不到任何初学者文档Nanohttpd

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 host android device and list both files and folders as links. What i want to implement is when a user clicks on any of the link (the folder links), the browser should display the files and folder contained in the clicked folder link. How do i do this as i can not find any Nanohttpd documentation for beginners.

import java.io.File;
import java.util.Map;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.widget.TextView;

public class MainActivity extends Activity {
    private static final int PORT = 8080;
    private TextView hello;
    private WebServer server;
    private Handler handler = new Handler();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        hello = (TextView) findViewById(R.id.hello);
    }

    /*
     * There are some earlier versions of android that can not implement this
     * method of getting IP address and isEmpty() method. The
     * 
     * @SupreesLint("NewAPI") helps to suppress the error that will arise in
     * such devices when implementing these methods . For the application
     * however, a minimum version of API that can be able to execute the
     * application flawlessly is set. The enables error checking as lower
     * version that can not implement this methods wouldn't be able to install
     * the application.
     */
    @SuppressLint("NewApi")
    @Override
    protected void onResume() {
        super.onResume();

        TextView textIpaddr = (TextView) findViewById(R.id.ipaddr);
        if (Utils.getIPAddress(true).trim().isEmpty()) {
            textIpaddr.setText(Utils.getIPAddress(false) + ":" + PORT);
        } else {
            textIpaddr.setText(Utils.getIPAddress(true) + ":" + PORT);
        }

        try {
            server = new WebServer();
            server.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String intToIp(int i) {

        return ((i >> 24) & 0xFF) + "." + ((i >> 16) & 0xFF) + "."
                + ((i >> 8) & 0xFF) + "." + (i & 0xFF);
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (server != null)
            server.stop();
    }

    private class WebServer extends NanoHTTPD {

        public WebServer() {
            super(8080);
        }

        @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[] 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) {
                answer += "<a href=\"" + detailsOfFiles.getAbsolutePath()
                        + "\" alt = \"\">" + detailsOfFiles.getAbsolutePath()
                        + "</a><br>";
            }
            answer += "</head></html>";
            return new NanoHTTPD.Response(answer);
        }



    }

}


:

推荐答案

我终于弄清楚如何学习NanoHTTPD Framework.The code足够的时间后,做下面帮我在主目录内导航Android设备:

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

在响应法的URI参数包含在那个时间点浏览器的网址:
例如,如果显示的URL地址栏上是: /192.168.43.1:8080/storage/sdcard1/Smadav_2012_Rev._9.0 ,然后URI包含 /storage/sdcard1/Smadav_2012_Rev._9.0 我所做的是刚刚通过URI作为文件路径,当然,这并不是第一次连接的情况下,当URI是空的。

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.

这篇关于文件目录导航与Android Nanohttpd轻量级​​服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆