使用 Android Web 服务器在文件夹中提供文件
Serve files in folder with an Android Web Server
我想提供本地文件夹的文件(例如,.html 和 .css 文件)以重新创建 "real" 本地网站的通常行为.
这里有两个应用程序示例,它们可以按照我希望的方式运行:Tiny Web Server Free and kWS - Android Web Server。
我用 Google 搜索了很多,但我找不到任何东西...
我尝试使用 NanoHttpd,但我们似乎无法设置根目录或主目录,我们只能通过 returning a [=] 来 return 一些 http 代码11=] 对象与 serve()
方法。那不是我想要的。
我希望能够设置一个根目录,例如 sdcard/www/
,其中包含 sdcard/www/img/
子文件夹中的图像的 index.html
...
另外,我找到了this answer,但这不是我想要的。它包括 return 使用 serve()
方法将 .html 文件的内容写入 Response
对象。
我怎样才能做我想做的事?
我已经通过以下代码成功地提供了一个文件夹...
这是我 Android Web 服务器 class 中的响应方法 -->
public Response serve(IHTTPSession session) {
String uri=session.getUri();
String msg = "<html><body><h1>Hello server</h1>\n";
File [] arrayfile;
int i=0;
try{
session.parseBody(new HashMap<String, String>());
}catch (ResponseException | IOException r){
r.printStackTrace();
}
Map<String, String> parms = session.getParms();
if (parms.get("username") == null) {
msg += "<form action='?' method='get'>\n <p>Your name: <input type='text' name='username'></p>\n" + "</form>\n";
} else {
msg += "<p>Hello, " + parms.get("username") + "!</p>";
}
msg += "<br><br><a href='/Open_rap'>Open Image of Lionel Messi</a><br><br>";
msg += "<br><br><a href='/files'>Browse Files</a><br><br>";
msg += "<br><br><a href='/getmethod'>GET METHOD OPERATION</a><br><br>";
msg += "<br><br><a href='/postmethod'>POST METHOD OPERATION</a><br><br>";
msg += "<br><br><a href='/jquery'>JQUERY OPERATION</a><br><br>";
if(uri.equals("/hello")){
String response="Hello World";
return newFixedLengthResponse(response);
}
else if(uri.equals("/getmethod")){
String html="<html><head><h1>Welcome to the Form</h1><head/><body>";
if(parms.get("name")==null){
html +="<form action='' method='get'> \n " +
"<p>Enter Your Name:</p> <input type='text' name='name'>" +
"</form>" +
"</body>";
}
else{
html +="<p>Hello Mr. "+ parms.get("name") +"</p>"+
"</body> ";
}
html +="</html>";
return newFixedLengthResponse(html);
}
else if(uri.equals("/postmethod")){
String html="<html><head><h1>Welcome to the Form</h1><head/><body>";
Map<String, String> files = new HashMap<String, String>();
Method method = session.getMethod();
String postParameter="";
html +="<form action='' method='post'> \n " +
"<p>Enter Your Name:</p> <input type='text' name='name'>" +
"</form>";
if (Method.POST.equals(method) || Method.PUT.equals(method)) {
try {
session.parseBody(files);
} catch (IOException ioe) {
try {
// return newFixedLengthResponse(Response.Status.INTERNAL_ERROR, MIME_PLAINTEXT, "SERVER INTERNAL ERROR: IOException: " + ioe.getMessage());
} catch (Exception e) {
e.printStackTrace();
Log.d("Exception", e.getMessage());
}
} catch (ResponseException re) {
try {
// return newFixedLengthResponse(re.getStatus(), MIME_PLAINTEXT, re.getMessage());
} catch (Exception e) {
e.printStackTrace();
Log.d("Exception", re.getMessage());
}
}
}
html +="</body></html>";
String postBody = session.getQueryParameterString();
postParameter = session.getParms().get("name");
Log.d("Postbody",postBody+"\n"+postParameter);
if(postParameter!=null){
String html1="<html><head><h1>"+ postParameter +"</h1><head></html>";
return newFixedLengthResponse(html1);
}
return newFixedLengthResponse(Response.Status.OK,"text/html",html);
}
else if(uri.equals("/Open_rap")){
File root= Environment.getExternalStorageDirectory();
FileInputStream fis = null;
File file = new File(root.getAbsolutePath() + "/www/messi.jpg");
try{
if(file.exists())
{
fis = new FileInputStream(file);
}
else
Log.d("FOF :", "File Not exists:");
}catch (FileNotFoundException e)
{
e.printStackTrace();
}
return newFixedLengthResponse(Response.Status.OK,"image/jpeg",fis, file.length() );
}
else if(uri.equals("/files")) {
File root = Environment.getExternalStorageDirectory();
FileInputStream fis = null;
File file = new File(root.getAbsolutePath() + "/www/files/");
arrayfile = file.listFiles();
String html = "<html><body><h1>List Of All Files</h1>";
for (i = 0; i < arrayfile.length; i++) {
Log.d("Files", "FileName:" + arrayfile[i].getName());
html += "<a href='/www/files/" + arrayfile[i].getName() + "' >" + arrayfile[i].getName() + "</a><br><br>";
}
html += "</body></html>";
return newFixedLengthResponse(html);
}
else if(uri.equals("/jquery")){
String address="http://"+MainActivity.ipaddress+":8080/jquery-3.3.1.min.js";
Log.d("IP",address);
String s="<html>\n" +
"<head>\n" +
// "<script src=\"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js\"></script>\n" +
// "<script src='file:///android_asset/js/jquery-3.3.1.min.js' type='text/javascript'></script>\n" +
"<script src='"+address+"'></script>\n" +
"<script>\n" +
"$(document).ready(function(){\n" +
" $(\"button\").click(function(){\n" +
" $(\"p\").toggle();\n" +
" });\n" +
"});\n" +
"</script>\n" +
"</head>\n" +
"<body>\n" +
"\n" +
"<h2>This is a heading</h2>\n" +
"\n" +
"<button>Click me to hide paragraphs</button>\n" +
"<p>This is a paragraph.</p>\n" +
"<p>This is another paragraph.</p>\n" +
"\n" +
"\n" +
"</body>\n" +
"</html>\n";
return newFixedLengthResponse(s);
}
else if(uri.equals("/jquery-3.3.1.min.js")){
File root= Environment.getExternalStorageDirectory();
FileInputStream fis = null;
File file = new File(root.getAbsolutePath() + "/www/resources/jquery-3.3.1.min.js");
Log.d("Jquery","hello");
try{
if(file.exists())
{
fis = new FileInputStream(file);
}
else
Log.d("FOF :", "File Not exists:");
}catch (FileNotFoundException e)
{
e.printStackTrace();
}
return newFixedLengthResponse(Response.Status.OK,"application/javascript",fis, file.length() );
}
else if(uri.contains(".")){
String[] split=uri.split("/");
String s="";
for (i=0;i<split.length;i++){
Log.d("String",""+split[i]+""+i);
s=s+"/"+split[i];
}
String x=s.substring(1,s.length());
Log.d("String2",s);
Log.d("String2",x+" "+x.length());
String y = NanoHTTPD.getMimeTypeForFile(x);
Log.d("MIME-TYPE",y);
File root= Environment.getExternalStorageDirectory();
FileInputStream fis = null;
//File file = new File(root.getAbsolutePath() + "/"+split[1]+"/"+split[2]+"/"+split[3]);
File file=new File(root.getAbsolutePath()+x);
try{
if(file.exists())
{
fis = new FileInputStream(file);
}
else
Log.d("FOF :", "File Not exists:");
}catch (FileNotFoundException e)
{
e.printStackTrace();
}
return newFixedLengthResponse(Response.Status.OK,y,fis, file.length() );
}
else {
return newFixedLengthResponse(msg + "</body></html>\n");
}
}
如你所见,
uri.contains(.)
这部分代码是我使用 root
文件对象实现文件夹的地方,
file input stream
我的文件保存在我的 android 设备 /www/files
的内部存储器中
如果您有任何问题,请联系我。
我正在使用 nanohttpd 来提供文件,这是我使用的工作代码
@Override
public Response serve(IHTTPSession session) {
String msg = "<html><body><h1>Hello server</h1></body></html>\n";
String msgUnauthorized = "<html><body><h1>Unauthorized</h1></body></html>\n";
Method method = session.getMethod();
String uri = session.getUri();
Map<String, String> files = new HashMap<>();
String Endpoint = session.getUri();
if (Endpoint.equals("/")) {
String answer = "";
try {
// Open file from SD Card
File root = Environment.getExternalStorageDirectory().getAbsoluteFile();
FileReader index = new FileReader(root +
"/www/openrap/index.html");
BufferedReader reader = new BufferedReader(index);
String line = "";
while ((line = reader.readLine()) != null) {
answer += line;
}
reader.close();
} catch (IOException ioe) {
Log.w("Httpd", ioe.toString());
}
return newFixedLengthResponse(answer);
}
我想提供本地文件夹的文件(例如,.html 和 .css 文件)以重新创建 "real" 本地网站的通常行为.
这里有两个应用程序示例,它们可以按照我希望的方式运行:Tiny Web Server Free and kWS - Android Web Server。
我用 Google 搜索了很多,但我找不到任何东西...
我尝试使用 NanoHttpd,但我们似乎无法设置根目录或主目录,我们只能通过 returning a [=] 来 return 一些 http 代码11=] 对象与 serve()
方法。那不是我想要的。
我希望能够设置一个根目录,例如 sdcard/www/
,其中包含 sdcard/www/img/
子文件夹中的图像的 index.html
...
另外,我找到了this answer,但这不是我想要的。它包括 return 使用 serve()
方法将 .html 文件的内容写入 Response
对象。
我怎样才能做我想做的事?
我已经通过以下代码成功地提供了一个文件夹... 这是我 Android Web 服务器 class 中的响应方法 -->
public Response serve(IHTTPSession session) {
String uri=session.getUri();
String msg = "<html><body><h1>Hello server</h1>\n";
File [] arrayfile;
int i=0;
try{
session.parseBody(new HashMap<String, String>());
}catch (ResponseException | IOException r){
r.printStackTrace();
}
Map<String, String> parms = session.getParms();
if (parms.get("username") == null) {
msg += "<form action='?' method='get'>\n <p>Your name: <input type='text' name='username'></p>\n" + "</form>\n";
} else {
msg += "<p>Hello, " + parms.get("username") + "!</p>";
}
msg += "<br><br><a href='/Open_rap'>Open Image of Lionel Messi</a><br><br>";
msg += "<br><br><a href='/files'>Browse Files</a><br><br>";
msg += "<br><br><a href='/getmethod'>GET METHOD OPERATION</a><br><br>";
msg += "<br><br><a href='/postmethod'>POST METHOD OPERATION</a><br><br>";
msg += "<br><br><a href='/jquery'>JQUERY OPERATION</a><br><br>";
if(uri.equals("/hello")){
String response="Hello World";
return newFixedLengthResponse(response);
}
else if(uri.equals("/getmethod")){
String html="<html><head><h1>Welcome to the Form</h1><head/><body>";
if(parms.get("name")==null){
html +="<form action='' method='get'> \n " +
"<p>Enter Your Name:</p> <input type='text' name='name'>" +
"</form>" +
"</body>";
}
else{
html +="<p>Hello Mr. "+ parms.get("name") +"</p>"+
"</body> ";
}
html +="</html>";
return newFixedLengthResponse(html);
}
else if(uri.equals("/postmethod")){
String html="<html><head><h1>Welcome to the Form</h1><head/><body>";
Map<String, String> files = new HashMap<String, String>();
Method method = session.getMethod();
String postParameter="";
html +="<form action='' method='post'> \n " +
"<p>Enter Your Name:</p> <input type='text' name='name'>" +
"</form>";
if (Method.POST.equals(method) || Method.PUT.equals(method)) {
try {
session.parseBody(files);
} catch (IOException ioe) {
try {
// return newFixedLengthResponse(Response.Status.INTERNAL_ERROR, MIME_PLAINTEXT, "SERVER INTERNAL ERROR: IOException: " + ioe.getMessage());
} catch (Exception e) {
e.printStackTrace();
Log.d("Exception", e.getMessage());
}
} catch (ResponseException re) {
try {
// return newFixedLengthResponse(re.getStatus(), MIME_PLAINTEXT, re.getMessage());
} catch (Exception e) {
e.printStackTrace();
Log.d("Exception", re.getMessage());
}
}
}
html +="</body></html>";
String postBody = session.getQueryParameterString();
postParameter = session.getParms().get("name");
Log.d("Postbody",postBody+"\n"+postParameter);
if(postParameter!=null){
String html1="<html><head><h1>"+ postParameter +"</h1><head></html>";
return newFixedLengthResponse(html1);
}
return newFixedLengthResponse(Response.Status.OK,"text/html",html);
}
else if(uri.equals("/Open_rap")){
File root= Environment.getExternalStorageDirectory();
FileInputStream fis = null;
File file = new File(root.getAbsolutePath() + "/www/messi.jpg");
try{
if(file.exists())
{
fis = new FileInputStream(file);
}
else
Log.d("FOF :", "File Not exists:");
}catch (FileNotFoundException e)
{
e.printStackTrace();
}
return newFixedLengthResponse(Response.Status.OK,"image/jpeg",fis, file.length() );
}
else if(uri.equals("/files")) {
File root = Environment.getExternalStorageDirectory();
FileInputStream fis = null;
File file = new File(root.getAbsolutePath() + "/www/files/");
arrayfile = file.listFiles();
String html = "<html><body><h1>List Of All Files</h1>";
for (i = 0; i < arrayfile.length; i++) {
Log.d("Files", "FileName:" + arrayfile[i].getName());
html += "<a href='/www/files/" + arrayfile[i].getName() + "' >" + arrayfile[i].getName() + "</a><br><br>";
}
html += "</body></html>";
return newFixedLengthResponse(html);
}
else if(uri.equals("/jquery")){
String address="http://"+MainActivity.ipaddress+":8080/jquery-3.3.1.min.js";
Log.d("IP",address);
String s="<html>\n" +
"<head>\n" +
// "<script src=\"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js\"></script>\n" +
// "<script src='file:///android_asset/js/jquery-3.3.1.min.js' type='text/javascript'></script>\n" +
"<script src='"+address+"'></script>\n" +
"<script>\n" +
"$(document).ready(function(){\n" +
" $(\"button\").click(function(){\n" +
" $(\"p\").toggle();\n" +
" });\n" +
"});\n" +
"</script>\n" +
"</head>\n" +
"<body>\n" +
"\n" +
"<h2>This is a heading</h2>\n" +
"\n" +
"<button>Click me to hide paragraphs</button>\n" +
"<p>This is a paragraph.</p>\n" +
"<p>This is another paragraph.</p>\n" +
"\n" +
"\n" +
"</body>\n" +
"</html>\n";
return newFixedLengthResponse(s);
}
else if(uri.equals("/jquery-3.3.1.min.js")){
File root= Environment.getExternalStorageDirectory();
FileInputStream fis = null;
File file = new File(root.getAbsolutePath() + "/www/resources/jquery-3.3.1.min.js");
Log.d("Jquery","hello");
try{
if(file.exists())
{
fis = new FileInputStream(file);
}
else
Log.d("FOF :", "File Not exists:");
}catch (FileNotFoundException e)
{
e.printStackTrace();
}
return newFixedLengthResponse(Response.Status.OK,"application/javascript",fis, file.length() );
}
else if(uri.contains(".")){
String[] split=uri.split("/");
String s="";
for (i=0;i<split.length;i++){
Log.d("String",""+split[i]+""+i);
s=s+"/"+split[i];
}
String x=s.substring(1,s.length());
Log.d("String2",s);
Log.d("String2",x+" "+x.length());
String y = NanoHTTPD.getMimeTypeForFile(x);
Log.d("MIME-TYPE",y);
File root= Environment.getExternalStorageDirectory();
FileInputStream fis = null;
//File file = new File(root.getAbsolutePath() + "/"+split[1]+"/"+split[2]+"/"+split[3]);
File file=new File(root.getAbsolutePath()+x);
try{
if(file.exists())
{
fis = new FileInputStream(file);
}
else
Log.d("FOF :", "File Not exists:");
}catch (FileNotFoundException e)
{
e.printStackTrace();
}
return newFixedLengthResponse(Response.Status.OK,y,fis, file.length() );
}
else {
return newFixedLengthResponse(msg + "</body></html>\n");
}
}
如你所见,
uri.contains(.)
这部分代码是我使用 root
文件对象实现文件夹的地方,
file input stream
我的文件保存在我的 android 设备 /www/files
的内部存储器中
如果您有任何问题,请联系我。
我正在使用 nanohttpd 来提供文件,这是我使用的工作代码
@Override
public Response serve(IHTTPSession session) {
String msg = "<html><body><h1>Hello server</h1></body></html>\n";
String msgUnauthorized = "<html><body><h1>Unauthorized</h1></body></html>\n";
Method method = session.getMethod();
String uri = session.getUri();
Map<String, String> files = new HashMap<>();
String Endpoint = session.getUri();
if (Endpoint.equals("/")) {
String answer = "";
try {
// Open file from SD Card
File root = Environment.getExternalStorageDirectory().getAbsoluteFile();
FileReader index = new FileReader(root +
"/www/openrap/index.html");
BufferedReader reader = new BufferedReader(index);
String line = "";
while ((line = reader.readLine()) != null) {
answer += line;
}
reader.close();
} catch (IOException ioe) {
Log.w("Httpd", ioe.toString());
}
return newFixedLengthResponse(answer);
}