gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 07:06:05 32 4
gpt4 key购买 nike

通过下面的代码,我能够使用 Nanohttpd 轻量级服务器在 Android 手机上创建移动服务器。该代码基本上循环遍历主机 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

最佳答案

经过足够的时间研究 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:例如,如果地址栏上显示的 url 为:/192.168.43.1:8080/storage/sdcard1/Smadav_2012_Rev._9.0,则 uri 包含 /storage/sdcard1/Smadav_2012_Rev._9.0 . 我所做的只是将 uri 作为文件路径传递,当然,当 uri 为空时,第一次连接的情况并非如此。

关于java - 使用 Android Nanohttpd 轻量级服务器进行文件目录导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23603072/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com