使用spring获取项目的绝对路径,就算打JAR包一样获得当前项目的绝对路径

搞死了,哈哈,不过弄出来了



核心代码

				ClassPathResource cpr = new ClassPathResource(Constant.CONFIG_PATH_KEY);
				System.out.println("------********----"+cpr.getFile().getPath());
				configPath = cpr.getFile().getPath();



下面开始贴工具包和我自己配置的全局拦截器的JAR


package com.hansci.client.util;

/**
 * <project>hansci_search</project>
 * <package>com.hansci.search.util</package>
 * <class>PropertiesLoader.java</class>
 * @time:2013-2-26 涓嬪崍12:26:59
 *
 */

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

import org.apache.struts2.ServletActionContext;
import org.springframework.core.io.ClassPathResource;



public final class PropertiesLoader {

	private static final long serialVersionUID = 3594717377354174859L;

	private PropertiesLoader() {
	}

	public static Properties getProperties(String propertyFileName)
			throws IOException {
		InputStream is = null;
		String configPath = "";
		try {
			if (propertyFileName == null || "".equals(propertyFileName)) {
//				 configPath =new Thread().getContextClassLoader().getClass().getResource("/").getPath()+Constant.CONFIG_PATH_KEY;
//				 System.out.println("--------configPath="+configPath);
				ClassPathResource cpr = new ClassPathResource(Constant.CONFIG_PATH_KEY);
				System.out.println("------********----"+cpr.getFile().getPath());
				configPath = cpr.getFile().getPath();
//				configPath = ServletActionContext.getServletContext()
//						.getRealPath("/") + Constant.CONFIG_PATH_KEY;
			} else {
				//configPath = ServletActionContext.getServletContext().getRealPath("/") + Constant.CONFIG_PATH_KEY;
				configPath="";
			}
			File file = null;
			if (configPath != null && configPath.trim().length() != 0) {
				file = new File(configPath);
				is = new FileInputStream(file);
			} else {
				is = PropertiesLoader.class.getResourceAsStream(propertyFileName);
			}
			//
			if (is == null) {
				throw new FileNotFoundException(configPath + File.separator
						+ propertyFileName + " not found");
			}
			// load properties
			Properties props = new Properties();
			props.load(is);
			return props;
		} finally {
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public static int getIntegerProperty(Properties p, String name,
			int defaultValue) {
		String l = p.getProperty(name);
		return l == null ? defaultValue : Integer.valueOf(l).intValue();
	}

	public static String getStringProperty(Properties p, String name,
			String defaultValue) {
		String propertyValue = p.getProperty(name);
		return propertyValue == null ? defaultValue : propertyValue;
	}

	public static boolean getBooleanProperty(Properties p, String name,
			boolean defaultValue) {
		String propertyValue = p.getProperty(name);
		return propertyValue == null ? defaultValue : Boolean
				.valueOf(propertyValue);
	}
	
/*	public static void main(String[] args){
	PropertiesLoader propertiesLoader = new PropertiesLoader();
	Properties p;
	try {	
		
		p = propertiesLoader.getProperties("");//鍙傛暟涓虹┖鏃? 璁块棶resource.properties鏂囦欢
		String strFlowName=propertiesLoader.getStringProperty(p, "URL","null") ;
		System.out.println(strFlowName);
		strFlowName=propertiesLoader.getStringProperty(p, "AUTHORITY_LOGIN_PATH","null") ;
		System.out.println(strFlowName);
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	
	
	
}*/
}



package com.hansci.client.action.authority;



import java.io.IOException;
import java.util.Properties;

import com.hansci.client.util.Constant;
import com.hansci.client.util.PropertiesLoader;
import com.hansci.client.webservice.LoginAuthService;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class UserAuthorityInterceptor extends AbstractInterceptor {


	private static final long serialVersionUID = -3929030161999051802L;

	
	public String intercept(ActionInvocation invocation) throws Exception {
        //创建ActionContext实例
		@SuppressWarnings("unused")
		ActionContext ctx = ActionContext.getContext();
		//获取user-client中得到的用户登录的sessionID的属性
    	LoginAuthService service = new LoginAuthService();
    	
    	Properties p;
    	
    	String loginPath="";
    	String operatePath="";
    	try {	
    		
    		p = PropertiesLoader.getProperties("");//鍙傛暟涓虹┖鏃? 璁块棶resource.properties鏂囦欢
    		loginPath=PropertiesLoader.getStringProperty(p, Constant.AUTHORITY_LOGIN_PATH,"null") ;
    		System.out.println(loginPath);
    		operatePath=PropertiesLoader.getStringProperty(p, Constant.AUTHORITY_OPERATE_PATH,"null") ;
    		System.out.println(operatePath);
    	} catch (IOException e) {
    		// TODO Auto-generated catch block
    		e.printStackTrace();
    	}
    	
    	
        System.out.println(service.login(loginPath));
    	if(null!=service.login(loginPath)&&!service.login(loginPath).equalsIgnoreCase("null")) {//如果没有登录,则需要返回重新登录
    		return invocation.invoke();
    	}else {
    		return Action.LOGIN;
    	}
	}

}






你可能感兴趣的:(使用spring获取项目的绝对路径,就算打JAR包一样获得当前项目的绝对路径)