文件目录使用Android Nanohttpd轻量级服务器进行导航

时间:2014-05-12 07:13:15

标签: java android nanohttpd

通过下面的代码,我能够在Android手机上使用Nanohttpd轻量级服务器创建移动服务器。代码基本上遍历主机android设备的根目录,并将文件和文件夹列为链接。我想要实现的是当用户点击任何链接(文件夹链接)时,浏览器应显示所单击文件夹链接中包含的文件和文件夹。我怎么做这个,因为我找不到任何适合初学者的Nanohttpd文档。

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



    }

}

Here is how the output looks like in my browser

2 个答案:

答案 0 :(得分:4)

我终于弄清楚如何在经过足够的时间研究NanoHTTPD框架后这样做。下面的代码帮助我在主机android设备的目录中导航:

@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为空时,第一次连接不是这种情况。

答案 1 :(得分:3)

您在第一个参数中获得URI。所以将其附加到具有打开指定目录的路径。 如果您请求192.168.1.6:8080/ABC程序将在外部目录中查找ABC文件夹。

然后检查拍摄的项目是文件还是目录&amp;据此,我们改变了我们的产出。使用

.isFile()

下面是应该有效的代码:

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

抱歉说不好。