我想提供本地文件夹的文件(例如.html和.css文件),以重新创建“真实”本地网站的常规行为。
以下是两个应用程序示例,它们与我的工作方式相关:Tiny Web Server Free和kWS - Android Web Server。
我经常搜索Google,但我找不到任何东西......
我尝试使用NanoHttpd,但似乎我们无法设置根目录或主目录,我们只能通过返回Response
serve()
对象来返回一些http代码方法。那不是我想要的。
我希望能够设置一个根目录,例如sdcard/www/
,其中index.html
包含来自sdcard/www/img/
子文件夹的图片...
另外,我找到this answer,但这不是我想要的。它包括使用Response
方法返回serve()
对象中.html文件的内容。
我怎么能做我想做的事?
答案 0 :(得分:1)
我已成功通过以下代码提供文件夹... 这是我在Android Web Server类中的Response方法 - >
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
的内部存储空间内
如果你有任何问题请跟我说。
答案 1 :(得分:1)
答案 2 :(得分:1)
我正在使用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);
}