package com.gaowj.util;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.servlet.http.HttpServletRequest;

import oracle.sql.BLOB;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.core.io.ClassPathResource;

public class ServiceUtil {
	
    /**
     * 从struts.properties中找键值
     * @param name
     * @return
     */
	public static String getProValue(String name){
	    String value = "";
		try {
			InputStream in = new ClassPathResource("struts.properties").getInputStream();
			Properties config = new Properties();
			config.load(in);
			if (config.getProperty(name) != null) {
				value = config.getProperty(name);
			}
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		return  value;
	}
   
   
   public static String getClientIp(HttpServletRequest request){
		String ip = request.getHeader("X-Forwarded-For");
		if(StringUtils.isNotEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)){
		    //多次反向代理后会有多个ip值，第一个ip才是真实ip
		    int index = ip.indexOf(",");
		  if(index != -1){ 
			  System.out.println("a=X-Forwarded-For捕获IP:"+ ip);
		      return ip.substring(0,index);
		  }else{
			  System.out.println("b=X-Forwarded-For捕获IP:"+ ip);
		      return ip;
		  }
		}
		ip = request.getHeader("X-Real-IP");
		  if(StringUtils.isNotEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)){
			  System.out.println("c=X-Real-IP捕获IP:"+ ip);
		      return ip;
		  }
		  System.out.println("d=getRemoteAddr捕获IP:"+ request.getRemoteAddr());
		  return request.getRemoteAddr();
		}
   
	
	public static void main(String args[]){   
		//System.out.println(ServiceUtil.getProValue("serviceFilePath"));  
		System.out.println("c:\\crt.tr".lastIndexOf("\\")); 
	}

}
