java中的绝对URL
我试图在 java 类中获取绝对 URL,但即使在将其类型转换为 Object 后也会出现错误。
任何建议
String file = ((Object) request).getRequestURI();
if (((Object) request).getQueryString() != null) {
file += '?' + ((Object) request).getQueryString();
}
URL reconstructedURL = new URL(request.getScheme(),
request.getServerName(),
request.getServerPort(),
file);
System.out.println(URL.toString());
I am trying to get absolute URL in java class, but getting an error even after type casting it to Object.
Any suggestions
String file = ((Object) request).getRequestURI();
if (((Object) request).getQueryString() != null) {
file += '?' + ((Object) request).getQueryString();
}
URL reconstructedURL = new URL(request.getScheme(),
request.getServerName(),
request.getServerPort(),
file);
System.out.println(URL.toString());
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
除了 @Daniel 所说的 URL类没有像
.toString()
这样的静态方法。您应该使用reconstructedURL
而不是我不测试它,但请尝试这个
Addition to what @Daniel said URL class has not static method like
.toString()
. You should usereconstructedURL
insteadI don't test it but please try this
永远不需要强制转换为 Object,因为一切都是对象。我建议发布您的错误的堆栈跟踪。
((Object) request).getRequestURI()
不起作用,因为 Object 没有getRequestURI()
函数。把演员们留下吧。A cast to Object is never nessecary, because everything is an object. I suggest posting the stacktrace of your error.
((Object) request).getRequestURI()
doesn't work, because Object does not have agetRequestURI()
function. Just leave the cast away.