You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
1.1 KiB
42 lines
1.1 KiB
5 years ago
|
package ${basePackage}.frame.utils;
|
||
|
|
||
|
import org.springframework.stereotype.Component;
|
||
|
import org.springframework.web.context.request.RequestContextHolder;
|
||
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
||
|
|
||
|
import javax.servlet.http.HttpServletRequest;
|
||
|
import java.util.Locale;
|
||
|
|
||
|
/**
|
||
|
* Url工具类
|
||
|
*
|
||
|
* @author wangbing
|
||
|
* @version 0.0.1
|
||
|
* @since 2017-01-01
|
||
|
*/
|
||
|
@Component
|
||
|
public class UrlUtil {
|
||
|
|
||
|
public String getUrl(String url) {
|
||
|
if (url == null) {
|
||
|
return "";
|
||
|
}
|
||
|
|
||
|
if (!url.startsWith("/")) {
|
||
|
return "/" + url;
|
||
|
}
|
||
|
|
||
|
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
|
||
|
// 协议
|
||
|
String scheme = request.getScheme();
|
||
|
// 域名
|
||
|
String serverName = request.getServerName();
|
||
|
// 端口
|
||
|
int serverPort = request.getServerPort();
|
||
|
// 上下文路径
|
||
|
String context = request.getContextPath();
|
||
|
|
||
|
return String.format(Locale.CHINA, "%s://%s:%d%s%s", scheme, serverName, serverPort, context, url);
|
||
|
}
|
||
|
}
|