我尝试使用以下代码查找 url 是否存在。要求是在url中查找文件是否存在。
try{
HttpURLConnection.setFollowRedirects(false);
HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection();
con.setRequestMethod("HEAD");
responseCode = con.getResponseCode();
return Response.ok(Integer.toString(responseCode)).build();
}
catch(Exception e){
return Response.ok(e.getMessage()).build();
}
如果 url 不包含任何空格/特殊字符,这将非常有效。但如果有,它总是返回代码 404。我能知道如何解决这个问题吗?提前致谢。