前端获取运行时路径

依赖;

    <dependency>
        <groupId>com.jcraft</groupId>
        <artifactId>jsch</artifactId>
        <version>0.1.55</version>
    </dependency>

后端:
import com.cosmosource.model.socketio.ResultModel;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
public class IPandPortPath {
/**
* 功能描述:获取IP和port根路径
*
* @param httpServletRequest
* @return
*/
private static Logger logger = LoggerFactory.getLogger(WebSocketTest.class);
@RequestMapping(value = “/getIPandPortPath”, produces = “application/json;charset=UTF-8”)
@ResponseBody
public ResultModel getIPandPortPath(HttpServletRequest httpServletRequest) {
Map<String, Object> resultMap = new HashMap<String, Object>();
String scheme = httpServletRequest.getScheme();
String serverName = httpServletRequest.getServerName();
String localPort = String.valueOf(httpServletRequest.getLocalPort());
String direPath = serverName + “:” + localPort;
logger.info("[getIPandPortPath] parms:direPath=[{}]", direPath);
if (StringUtils.isBlank(direPath)) {
logger.error("[getIPandPortPath] 获取IP和port根路径失败,direPath=[{}]", direPath);
return new ResultModel(ResultModel.PARAM_ERROR, “参数错误”, new HashMap<>());
}
resultMap.put(“IPandPortPath”, direPath);
logger.info("[getIPandPortPath] 获取IP和port根路径成功,direPath=[{}]", direPath);
return new ResultModel(ResultModel.RESULT_OK, “获取IP和port根路径成功”, resultMap);
}
}

前端:

var IPandPortPath = null;
Ext.Ajax.request({
async: false,
method: “GET”,
url: “getIPandPortPath”,
defaultPostHeader: “application/json;charset=utf-8”,
params: {}
,
success: function (response, opts) {
var data = Ext.decode(response.responseText);
var resultList = data.data;
IPandPortPath = resultList.IPandPortPath;
//getSessionStateWebsocket();
},
failure: function (response, opts) {
Ext.Msg.alert(‘提示’, ‘获取classPath根路径失败!’, Ext.emptyFn);
}
});

上一篇:Spring简介


下一篇:Spring AOP日志实现(二)--获取访问者IP及访问路径