Java tutorial
/** * Program : AbstractBaseService.java * Author : long * Create : 201577 ?10:38:53 * * Copyright 2015 by Embedded Internet Solutions Inc., * All rights reserved. * * This software is the confidential and proprietary information * of Embedded Internet Solutions Inc.("Confidential Information"). * You shall not disclose such Confidential Information and shall * use it only in accordance with the terms of the license agreement * you entered into with Embedded Internet Solutions Inc. * */ package com.iwancool.dsm.service.impl; import java.util.Random; import javax.servlet.http.HttpServletRequest; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.util.StringUtils; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import com.iwancool.dsm.service.IGenericService; /** * service * @Description TODO * @ClassName AbstractBaseService * @Date 2016820 ?8:32:01 * @Author huchanghuan */ public class AbstractBaseService implements IGenericService { protected final Log log = LogFactory.getLog(getClass()); /** * ? * * @author long * @return * @throws Exception * @return String * @exception * @author long * @since 1.0.0 * @date 2015715 ?6:57:18 */ public String getProjectRealPath() throws Exception { return getProjectRealPath("/"); } /** * ? * * @author long * @param path * @return * @throws Exception * @return String * @exception * @author long * @since 1.0.0 * @date 2015715 ?6:57:37 */ public String getProjectRealPath(String path) throws Exception { HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()) .getRequest(); return request.getSession().getServletContext().getRealPath(path); } /** * ???? * * @author long * @param type * @return * @throws Exception * @return String * @exception * @author long * @since 1.0.0 * @date 2015715 ?7:03:40 */ public String getRandomFileName(String type) throws Exception { Random random = new Random(1000); int randomNum = random.nextInt(9999); return System.currentTimeMillis() + "_" + randomNum + "." + StringUtils.getFilenameExtension(type); } /** * ?IP? * * @author long * @create 2014-4-16 ?6:05:46 * @since * @param request * @return */ protected String getRequestIPAddress(HttpServletRequest request) { String remoteIp = request.getHeader("x-forwarded-for"); if (remoteIp == null || remoteIp.isEmpty() || "unknown".equalsIgnoreCase(remoteIp)) { remoteIp = request.getHeader("X-Real-IP"); } if (remoteIp == null || remoteIp.isEmpty() || "unknown".equalsIgnoreCase(remoteIp)) { remoteIp = request.getHeader("Proxy-Client-IP"); } if (remoteIp == null || remoteIp.isEmpty() || "unknown".equalsIgnoreCase(remoteIp)) { remoteIp = request.getHeader("WL-Proxy-Client-IP"); } if (remoteIp == null || remoteIp.isEmpty() || "unknown".equalsIgnoreCase(remoteIp)) { remoteIp = request.getHeader("HTTP_CLIENT_IP"); } if (remoteIp == null || remoteIp.isEmpty() || "unknown".equalsIgnoreCase(remoteIp)) { remoteIp = request.getHeader("HTTP_X_FORWARDED_FOR"); } if (remoteIp == null || remoteIp.isEmpty() || "unknown".equalsIgnoreCase(remoteIp)) { remoteIp = request.getRemoteAddr(); } if (remoteIp == null || remoteIp.isEmpty() || "unknown".equalsIgnoreCase(remoteIp)) { remoteIp = request.getRemoteHost(); } return remoteIp; } }