我正在使用 NanoHTTPd 从我的 android 应用程序中提供文件。我可以很好地打开 .html 文件,但它正在尝试查看问题所在的图像。不会显示网页背景图像之类的任何内容。
有没有人有任何示例代码。我知道 nanoHTTPd 可以做到这一点。我在 Android 和 Java 方面有很多经验,但这是我第一次使用服务器。
private class MyHTTPD extends NanoHTTPD {
public MyHTTPD() throws IOException {
super(PORT, null);
}
@Override
public Response serve(String uri, String method, Properties header, Properties parms, Properties files) {
Log.d("response", "URI:" + uri + " method: " + method + " header: " + header + " parms: " + parms + " files: " + files);
final StringBuilder buf = new StringBuilder();
for (Entry<Object, Object> kv : header.entrySet())
buf.append(kv.getKey() + " : " + kv.getValue() + "\n");
handler.post(new Runnable() {
@Override
public void run() {
hello.setText(buf);
}
});
String html = null;
InputStream is = null;
if (uri.length() > 3) {
// respond with resource or sub page
// serve image?
if (uri.substring(uri.lastIndexOf("."), uri.length()).equals(".jpg")) {
try {
is = new FileInputStream(Environment.getExternalStorageDirectory().getPath() + "/WiFile" + uri);
Log.d("Serve", "image: " + Environment.getExternalStorageDirectory().getPath() + "/WiFile" + uri);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
//serve page
try {
is = new FileInputStream(Environment.getExternalStorageDirectory().getPath() + "/WiFile" + uri);
Log.d("response subpage", Environment.getExternalStorageDirectory().getPath() + "/WiFile" + uri);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} else {
// respond with index
try {
is = new FileInputStream(Environment.getExternalStorageDirectory().getPath() + "/WiFile/" + "index.html");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.d("response index", Environment.getExternalStorageDirectory().getPath() + "/WiFile/" + "index.html");
}
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(HTTP_OK, MIME_HTML, html);
}
}
编辑:
在浏览器中打开图像只会返回大量文本符号(...��k�OOO�...)。我是否以错误的方式解析图像?
使固定:
就像古斯塔夫说的那样,我没有使用正确的模因类型,但我也没有返回 serveFile(.....) 例如....
// serve image?
if (uri.substring(uri.lastIndexOf("."), uri.length()).equals(".jpeg")) {
try {
is = new FileInputStream(Environment.getExternalStorageDirectory().getPath() + "/WiFile" + uri);
mimeType = "image/jpeg";
Log.d("Serve", "image: " + Environment.getExternalStorageDirectory().getPath() + "/WiFile" + uri);
return serveFile(uri, header, new File(Environment.getExternalStorageDirectory().getPath() + "/WiFile"), true);
} catch (FileNotFoundException e) {}
} else {
//serve page
try {
is = new FileInputStream(Environment.getExternalStorageDirectory().getPath() + "/WiFile" + uri);
mimeType = MIME_HTML;
Log.d("response subpage", Environment.getExternalStorageDirectory().getPath() + "/WiFile" + uri);
return serveFile(uri, header, new File(Environment.getExternalStorageDirectory().getPath() + "/WiFile"), true);
} catch (FileNotFoundException e) {}
}
} else {
// respond with index
try {
is = new FileInputStream(Environment.getExternalStorageDirectory().getPath() + "/WiFile/" + "index.html");
mimeType = MIME_HTML;
Log.d("response index", Environment.getExternalStorageDirectory().getPath() + "/WiFile/" + "index.html");
return serveFile(uri, header, new File(Environment.getExternalStorageDirectory().getPath() + "/WiFile"), true);
} catch (FileNotFoundException e) {}
}