使用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;
    	}
	}

}






目录
相关文章
|
28天前
|
Java Maven 微服务
springboot项目开启远程调试-jar包
springboot项目开启远程调试-jar包
20 0
|
1月前
|
SpringCloudAlibaba Java 持续交付
【构建一套Spring Cloud项目的大概步骤】&【Springcloud Alibaba微服务分布式架构学习资料】
【构建一套Spring Cloud项目的大概步骤】&【Springcloud Alibaba微服务分布式架构学习资料】
155 0
|
14天前
|
JSON 前端开发 Java
统一异常处理:让Spring Boot项目异常更优雅
统一异常处理:让Spring Boot项目异常更优雅
24 1
|
28天前
|
Java Maven
maven项目导出可执行jar
maven项目导出可执行jar
28 0
|
1月前
|
Java Serverless 测试技术
Serverless 应用引擎常见问题之上传自定义jar包自动vpc启动报错如何解决
Serverless 应用引擎(Serverless Application Engine, SAE)是一种完全托管的应用平台,它允许开发者无需管理服务器即可构建和部署应用。以下是Serverless 应用引擎使用过程中的一些常见问题及其答案的汇总:
32 4
|
1月前
|
NoSQL Java 应用服务中间件
使用innoSetup将mysql+nginx+redis+jar包打包成windows安装包
使用innoSetup将mysql+nginx+redis+jar包打包成windows安装包
使用innoSetup将mysql+nginx+redis+jar包打包成windows安装包
|
1月前
|
缓存 NoSQL Java
spring cache整合redis实现springboot项目中的缓存功能
spring cache整合redis实现springboot项目中的缓存功能
46 1
|
1月前
|
Java Shell API
通用Shell脚本执行Spring Boot项目Jar包
通用Shell脚本执行Spring Boot项目Jar包
|
29天前
|
Java 应用服务中间件 Maven
SpringBoot 项目瘦身指南
SpringBoot 项目瘦身指南
43 0
|
1月前
|
缓存 安全 Java
Spring Boot 面试题及答案整理,最新面试题
Spring Boot 面试题及答案整理,最新面试题
111 0