gpt4 book ai didi

java - 如何在spark-java中返回静态文件作为响应? (不重定向)

转载 作者:行者123 更新时间:2023-12-02 11:30:17 26 4
gpt4 key购买 nike

我需要返回静态html文件的内容作为响应,例如,如果用户尝试访问localhost:4567/hello.html,我想显示另一个html文件而不是 hello.html 表示他无权查看此页面。

现在我重定向到页面:

private void securityFilter(Request request, Response response) {
if (notAuthorized) {
response.redirect("/Error/AccessDenied.html");
}
}

但我不想重定向用户,我想返回状态代码 401 的响应,并以静态文件作为响应内容。

谢谢。

最佳答案

But I do not want to redirect the user, I want to return a response with status code 401 and with a static file as a response content.

然后您需要以字符串形式读取 HTML 文件并将其设置为响应正文。

private void securityFilter(Request request, Response response) {
if (notAuthorized) {
response.body(/* read HTML file as string */);
}
}

关于java - 如何在spark-java中返回静态文件作为响应? (不重定向),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49347943/

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