使用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;
}
}
}
最後更新:2017-04-03 22:15:45