[英]Java HTTP Server and Resources
I'm using NanoHTTPd to serve files from my android app. 我正在使用NanoHTTPd从我的Android应用程序提供文件。 I can open the .html files fine but it's trying to view images that's the problem.
我可以很好地打开.html文件,但它正在尝试查看图像,这就是问题所在。 Anything like a webpages background image doesn't display.
网页背景图片等任何内容均不会显示。
Does anybody have any example code for this. 有人对此有任何示例代码吗? I know nanoHTTPd can do this.
我知道nanoHTTPd可以做到这一点。 I've lots of experience with Android and Java but this is the first time I've use a server.
我在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);
}
}
EDIT: 编辑:
opening an image in the browser just returns lots of text symbols(...��k�OOO�...). 在浏览器中打开图像只会返回很多文本符号(...��k�OOO�...)。 Am I parsing the image the wrong way?
我以错误的方式解析图像吗?
Fix: 固定:
Like Gustav said I wasn't using the corrent meme types but also I wasn't returning serveFile(.....) eg.... 就像古斯塔夫(Gustav)所说的那样,我没有使用现成的meme类型,但也没有返回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) {}
}
You're passing MIME_HTML
to the Response
constructor regardless of what you're going to send as payload. 无论要将什么作为有效载荷发送,都将
MIME_HTML
传递给Response
构造函数。 The parameter is a String
, so try 该参数是
String
,因此请尝试
return new NanoHTTPD.Response(HTTP_OK, "image/jpeg", html);
when serving (JPEG) images. 提供(JPEG)图像时。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.