package com.tax.controller;


import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.UnknownHostException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.ModelAttribute;

import com.tax.dao.ErrorReportDao;
import com.tax.service.TaxService;
import com.tax.util.Config;
import com.tax.util.JsonUtil;



/**
 * Action的基类(封装Action的一些方法,其他的Action继承)
 * @ClassName: BaseAction
 * @Description: Base Action for all the request.
*
 */
@Scope("prototype")
public class BaseAction {

	@Resource
	public ErrorReportDao errorReportDao;
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	protected transient final Log logger = LogFactory.getLog(getClass());
	
	protected HttpServletRequest request;
	protected HttpServletResponse response;
	protected HttpSession session;

	protected ThreadLocal<HttpServletRequest> requestLocal = new ThreadLocal<HttpServletRequest>();
	protected ThreadLocal<HttpServletResponse> responseLocal = new ThreadLocal<HttpServletResponse>();
	protected ThreadLocal<HttpSession> sessionLocal = new ThreadLocal<HttpSession>();

	public static final String JSON_SUCCESS = "{success:true}";

	@ModelAttribute
	public void setReqAndRes(HttpServletRequest request,
			HttpServletResponse response) {
		this.request = request;
		this.response = response;
		this.session = request.getSession();
		this.requestLocal.set(request);
		this.responseLocal.set(response);
		this.sessionLocal.set(request.getSession());
	}

	/**
	 * Convenience method to get the request
	 * 
	 * @return current request
	 */
	protected HttpServletRequest getRequest() {
		return requestLocal.get();
	}

	/**
	 * Convenience method to get the response
	 * 
	 * @return current response
	 */
	protected HttpServletResponse getResponse() {
		return responseLocal.get();
	}

	/**
	 * Convenience method to get the session. This will create a session if one
	 * doesn't exist.
	 * 
	 * @return the session from the request (request.getSession()).
	 */
	protected HttpSession getSession() {
		return getRequest().getSession();
	}


	/**
	 * 
	 * @Title: getIpAddr
	 * @Description: 获取客户端最真实有效的IP地址
	 * @param @return 设定文件
	 * @return String 返回类型
	 * @throws
	 */
	public static String getIpAddr(HttpServletRequest request) {
		String ipAddress = request.getHeader("X-Real-IP");
		if (ipAddress == null || ipAddress.length() == 0
				|| "unknown".equalsIgnoreCase(ipAddress)) {
			ipAddress = request.getHeader("X-Forwarded-For");
		}
		if (ipAddress == null || ipAddress.length() == 0
				|| "unknown".equalsIgnoreCase(ipAddress)) {
			ipAddress = request.getHeader("Proxy-Client-IP");
		}
		if (ipAddress == null || ipAddress.length() == 0
				|| "unknown".equalsIgnoreCase(ipAddress)) {
			ipAddress = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ipAddress == null || ipAddress.length() == 0
				|| "unknown".equalsIgnoreCase(ipAddress)) {
			ipAddress = request.getRemoteAddr();
			if (ipAddress.equals("127.0.0.1")
					|| ipAddress.equals("0:0:0:0:0:0:0:1")) {
				// 根据网卡取本机配置的IP
				InetAddress inet = null;
				try {
					inet = InetAddress.getLocalHost();
				} catch (UnknownHostException e) {
					e.printStackTrace();
				}
				ipAddress = inet.getHostAddress();
			}
		}
		// 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
		if (ipAddress != null && ipAddress.length() > 15) { // "***.***.***.***".length()
															// = 15
			if (ipAddress.indexOf(",") > 0) {
				ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
			}
		}
		return ipAddress;
	}
	
	/**
	 * 对象转JSON流输出
	 * @param obj
	 */
	protected void  printJSON(Object obj) {
        response.setContentType("text/html");  
        PrintWriter out=null;  
        try {
			out = response.getWriter();
			String JSON = JsonUtil.parseObj2Json(obj);
			out.print(JSON);
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
	        out.close();  
		}
	}
}
最近下载更多
最代码-宋家辉  LV61 2023年8月5日
binbin123123  LV1 2020年6月2日
8战魂5无双8  LV43 2019年12月7日
xzl8023xwq  LV13 2019年10月22日
zhxtatata  LV4 2019年6月27日
qianbaobao  LV1 2019年5月27日
安宇12345  LV15 2019年4月18日
15673237576  LV1 2019年3月16日
18392130476  LV6 2019年1月28日
fsj0122  LV1 2019年1月16日
最近浏览更多
暂无贡献等级
吞吞吐吐她  LV6 2024年3月27日
暂无贡献等级
最代码-宋家辉  LV61 2023年8月5日
uni-code_0123  LV1 2023年8月4日
qazws123  LV1 2023年6月7日
sweetbox  LV10 2023年4月16日
泡面不会打野  LV1 2022年12月30日
tonghao  LV18 2022年12月9日
111114758  LV2 2022年11月27日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友