gpt4 book ai didi

Java HTTP 服务器和资源

转载 作者:可可西里 更新时间:2023-11-01 17:10:59 30 4
gpt4 key购买 nike

我正在使用 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) {}
}

最佳答案

您将 MIME_HTML 传递给 Response 构造函数,而不管您要作为有效负载发送什么。参数是一个String,所以试试

return new NanoHTTPD.Response(HTTP_OK, "image/jpeg", html);

提供 (JPEG) 图片时。

关于Java HTTP 服务器和资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15336420/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com