gpt4 book ai didi

Java SimpleFileServer disable directory listing(Java SimpleFileServer禁用目录列表)

转载 作者:bug小助手 更新时间:2023-10-25 15:29:46 27 4
gpt4 key购买 nike



For a minimalistic SimpleFileServer pseudocode below, how to disable directory listing?

对于下面的简约SimpleFileServer伪代码,如何禁用目录列表?


var server = HttpsServer.create(new InetSocketAddress(port), 2)
server.createContext("/file", SimpleFileServer.createFileHandler(Path.of("d:\file")));
server.start();

更多回答

Maybe it's me, but I would think that this question would benefit from more detail, more real code, and removal of the GitHub link. All pertinent information should be in the question itself, such as a minimal reproducible example code post, and not in a link.

也许是我的问题,但我认为这个问题将受益于更多的细节,更真实的代码,以及删除GitHub链接。所有相关信息都应该在问题本身中,例如一个最小的可重现的示例代码帖子,而不是在链接中。

Oh, I thought I only use jdk classes. Did u see any legal issues?

哦,我还以为我只用JDK类呢。你看到什么法律问题了吗?

What do you mean by "legal issues"? This site is a software Q&A site and does not delve into legal issues, other than that intellectual property rights must be respected. That's it.

你所说的“法律问题”是什么意思?本网站是一个软件问答网站,除了知识产权必须得到尊重外,不会深入探讨法律问题。就这样。

To be safe I am removing github redirection.

为了安全起见,我删除了github重定向。

优秀答案推荐

If you put an index file (index.html or index.htm) in the directory, then that will be served, rather than the directory contents.

如果您将索引文件(index.html或index.htm)放在目录中,则将提供该索引文件,而不是目录内容。


One more thing: you probably want a / at the end of the context path (so "/file/") as per the note in the API:

还有一件事:根据API中的说明,您可能需要在上下文路径的末尾添加一个/(即“/file/”):



The path should generally, but is not required to, end with '/'. If the path does not end with '/', eg such as with "/foo" then this would match requests with a path of "/foobar" or "/foo/bar".



Alternatively:

或者:


var server = HttpServer.create(new InetSocketAddress(8080), 2);
var fileHandler = SimpleFileServer.createFileHandler(Path.of("d:\file"));
server.createContext("/file/", (exchange) -> {
if (exchange.getHttpContext().getPath().equals(exchange.getRequestURI().getPath())) {
try (exchange) {
exchange.setAttribute("request-path", "could not resolve request URI path");
exchange.sendResponseHeaders(404, 0);
}
} else {
fileHandler.handle(exchange);
}
});
server.start();

更多回答

Thank you. Before "fileHandler.handle(exchange);", adding "if (file not exists) return;" helped a lot.

谢谢。在“fileHandler.Handle(Exchange);”之前,加上“if(FILE NOT EXISTS)RETURN;”很有帮助。

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