问题描述
问题:我使用的是 NanoHTTPD 。它是伟大的工作,但它不提供的.js文件,图像等。
详解:我有一个页
资产文件夹内的文件夹。此文件夹包含 index.html的
,CSS文件,图像等。我使用NanoHTTPD这样的,但是当我浏览我的浏览器,没有任何样式或图片。服务器无法找到的图片和其他文件。只有index.html文件的内容。活动:
MyHTTPD服务器= NULL;
尝试 {
服务器=新MyHTTPD(getApplicationContext());
尝试
{
server.start();
}
赶上(IOException异常IOE)
{
通信System.err.println(无法启动服务器:\ N+ IOE);
System.exit(-1);
}
的System.out.println(监听端口8080上按下回车键,停止\ñ。);
尝试{System.in.read(); }赶上(的Throwable T){
的System.out.println(读取错误);
};
}赶上(IOException异常E1){
// TODO自动生成的catch块
e1.printStackTrace();
}
MyHTTPD类
公共语境CTX = NULL;
/ **
*根据给定的端口上的HTTP服务器。
* /
公共MyHTTPD(上下文CTX)抛出IOException异常{
超(8080);
this.ctx = CTX;
}
@覆盖
公众的反应发球(字符串URI,方法的方法,
地图<字符串,字符串>头,地图<字符串,字符串> PARMS,
地图<字符串,字符串>文件)
{
字符串的HTML = NULL;
InputStream的是= NULL;
尝试 {
是= ctx.getAssets()打开(页/ index.html的)。
}赶上(IOException异常E1){
// TODO自动生成的catch块
e1.printStackTrace();
}
字节[] B:
尝试 {
B =新的字节[is.available()];
is.read(B);
HTML =新的String(B);
}赶上(IOException异常E){// TODO自动生成的catch块
e.printStackTrace();
}
返回新NanoHTTPD.Response(HTML);
}
注意:我读过这个问题(和答案): Android中使用NanoHTTPD文件上传错误nanohttpd 如何在Android中创建nanohttpd服务器?
在我的服务()
方法,它看起来是这样的:
@覆盖
公共服务的响应(URI字符串,字符串的方法,属性标题,属性PARMS,属性文件){
Log.d(TAG,服务:: URI+ URI);
最后StringBuilder的BUF =新的StringBuilder();
对于(进入<对象,对象> KV:header.entrySet())
buf.append(kv.getKey()+:+ kv.getValue()+\ N);
InputStream的mbuffer = NULL;
尝试 {
如果(URI!= NULL){
如果(uri.contains(JS)){
mbuffer = mContext.getAssets()开(uri.substring(1))。
返回新NanoHTTPD.Response(HTTP_OK,MIME_JS,mbuffer);
}否则,如果(uri.contains(CSS)){
mbuffer = mContext.getAssets()开(uri.substring(1))。
返回新NanoHTTPD.Response(HTTP_OK,MIME_CSS,mbuffer);
}否则,如果(uri.contains(PNG)){
mbuffer = mContext.getAssets()开(uri.substring(1))。
// HTTP_OK =200 OK或HTTP_OK = Status.OK;(查看评论)
返回新NanoHTTPD.Response(HTTP_OK,MIME_PNG,mbuffer);
}否则,如果(uri.contains(到/ mnt / SD卡)){
Log.d(TAG,要求对SD卡媒体+ URI);
文件要求=新的文件(URI);
mbuffer =新的FileInputStream(要求);
FileNameMap fileNameMap = URLConnection.getFileNameMap();
字符串MIMETYPE = fileNameMap.getContentTypeFor(URI);
响应streamResponse =新的响应(HTTP_OK,MIMETYPE,mbuffer);
随机RND =新的随机();
串的etag = Integer.toHexString(rnd.nextInt());
streamResponse.addHeader(ETag的,ETAG);
streamResponse.addHeader(连接,保活);
返回streamResponse;
}其他{
mbuffer = mContext.getAssets()开(的index.html)。
返回新NanoHTTPD.Response(HTTP_OK,MIME_HTML,mbuffer);
}
}
}赶上(IOException异常E){
Log.d(TAG,打开文件时出错+ uri.substring(1));
e.printStackTrace();
}
返回null;
}
有一些不那么干净的解决方案,MIME类型。验证应该像这样获取文件的MIME类型的Java 一做>,我在我简单的项目,我只是检查几种默。
参考MIME类型是NanoHTTPD类的静态字段:
/ **
*动态内容常见MIME类型
* /
公共静态最后弦乐
MIME_PLAINTEXT =text / plain的,
MIME_HTML =text / html的,
MIME_JS =应用程序/ JavaScript的,
MIME_CSS =文本/ CSS,
MIME_PNG =图像/ PNG,
MIME_DEFAULT_BINARY =应用程序/八位字节流,
MIME_XML =为text / xml;
通过此实现,我能够从资产以及从外部存储器中读取文件。
PROBLEM : I'm using NanoHTTPD . It's working great but it's not serving .js files, images and others.
DETAILED EXPLANATION : i have a pages
folder inside of assets folder. This folder containing index.html
, css files, images and others. I'm using NanoHTTPD like this, but when i browse with my browser, there aren't any styles or images. Server can't found images and other files. There is only index.html file's content. Activity :
MyHTTPD server = null;
try {
server = new MyHTTPD(getApplicationContext());
try
{
server.start();
}
catch( IOException ioe )
{
System.err.println( "Couldn't start server:\n" + ioe );
System.exit( -1 );
}
System.out.println( "Listening on port 8080. Hit Enter to stop.\n" );
try { System.in.read(); } catch( Throwable t ) {
System.out.println("read error");
};
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
MyHTTPD class
public Context ctx = null;
/**
* Constructs an HTTP server on given port.
*/
public MyHTTPD(Context ctx) throws IOException {
super(8080);
this.ctx = ctx;
}
@Override
public Response serve( String uri, Method method,
Map<String, String> header, Map<String, String> parms,
Map<String, String> files )
{
String html = null;
InputStream is = null;
try {
is = ctx.getAssets().open("pages/index.html");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
byte[] b;
try {
b = new byte[is.available()];
is.read(b);
html = new String(b);
} catch (IOException e) { // TODO Auto-generated catch block
e.printStackTrace();
}
return new NanoHTTPD.Response(html);
}
NOTE : I've read this questions (and answers) :Using NanoHTTPD in Androidfile uploading error nanohttpdHow to create nanohttpd server in android?
in my serve()
method it looks like this:
@Override
public Response serve(String uri, String method, Properties header, Properties parms, Properties files) {
Log.d(TAG,"SERVE :: URI "+uri);
final StringBuilder buf = new StringBuilder();
for (Entry<Object, Object> kv : header.entrySet())
buf.append(kv.getKey() + " : " + kv.getValue() + "\n");
InputStream mbuffer = null;
try {
if(uri!=null){
if(uri.contains(".js")){
mbuffer = mContext.getAssets().open(uri.substring(1));
return new NanoHTTPD.Response(HTTP_OK, MIME_JS, mbuffer);
}else if(uri.contains(".css")){
mbuffer = mContext.getAssets().open(uri.substring(1));
return new NanoHTTPD.Response(HTTP_OK, MIME_CSS, mbuffer);
}else if(uri.contains(".png")){
mbuffer = mContext.getAssets().open(uri.substring(1));
// HTTP_OK = "200 OK" or HTTP_OK = Status.OK;(check comments)
return new NanoHTTPD.Response(HTTP_OK, MIME_PNG, mbuffer);
}else if (uri.contains("/mnt/sdcard")){
Log.d(TAG,"request for media on sdCard "+uri);
File request = new File(uri);
mbuffer = new FileInputStream(request);
FileNameMap fileNameMap = URLConnection.getFileNameMap();
String mimeType = fileNameMap.getContentTypeFor(uri);
Response streamResponse = new Response(HTTP_OK, mimeType, mbuffer);
Random rnd = new Random();
String etag = Integer.toHexString( rnd.nextInt() );
streamResponse.addHeader( "ETag", etag);
streamResponse.addHeader( "Connection", "Keep-alive");
return streamResponse;
}else{
mbuffer = mContext.getAssets().open("index.html");
return new NanoHTTPD.Response(HTTP_OK, MIME_HTML, mbuffer);
}
}
} catch (IOException e) {
Log.d(TAG,"Error opening file"+uri.substring(1));
e.printStackTrace();
}
return null;
}
There is some not so clean solution with mime types. Validation should be done with something like this Getting A File's Mime Type In Java, I my simple project I am just checking few kinds of mime.
Reference mime types are static fields in NanoHTTPD class:
/**
* Common mime types for dynamic content
*/
public static final String
MIME_PLAINTEXT = "text/plain",
MIME_HTML = "text/html",
MIME_JS = "application/javascript",
MIME_CSS = "text/css",
MIME_PNG = "image/png",
MIME_DEFAULT_BINARY = "application/octet-stream",
MIME_XML = "text/xml";
With this implementation I was able to read files from assets as well as from external memory.
这篇关于为什么图像和样式文件上NanoHTTPD无法找到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!