JAVA 获取完整URL

//JAVA 获取完整URL 方法
public String getRequestURL(HttpServletRequest request) {
if (request == null) {
return "";
}
String url = "";
url = request.getContextPath();
url = url + request.getServletPath();

java.util.Enumeration names = request.getParameterNames();
int i = 0;
if (!"".equals(request.getQueryString()) || request.getQueryString() != null) {
url = url + "?" + request.getQueryString();
}

if (names != null) {
while (names.hasMoreElements()) {
String name = (String) names.nextElement();
if (i == 0) {
url = url + "?";
} else {
url = url + "&";
}
i++;

String value = request.getParameter(name);
if (value == null) {
value = "";
}

url = url + name + "=" + value;
try {
// java.net.URLEncoder.encode(url, "ISO-8859");
} catch (Exception e) {
e.printStackTrace();
}
}
}
try {
// String enUrl = java.net.URLEncoder.encode(url, "utf-8");
} catch (Exception ex) {
ex.printStackTrace();
}

return url;

你可能感兴趣的:(java,request,url)