package framework.startup;
|
|
import java.io.File;
|
|
import javax.servlet.ServletContext;
|
import javax.servlet.http.HttpServletRequest;
|
|
import org.apache.log4j.Logger;
|
import org.springframework.web.context.WebApplicationContext;
|
|
/**
|
* 框架需提供的工具类
|
*
|
* @author liuyajun, 8384503@qq.com
|
* @date 2016年1月11日
|
* @time 下午8:37:38
|
*/
|
public final class FrameUtil {
|
private static String webRoot = null;
|
private static String webInf = null;
|
private static String uploadFilePath = null;
|
private static String downloadFilePath = null;
|
|
private static ServletContext application;
|
private static WebApplicationContext webApplicationContext;
|
|
/**
|
* 上传文件的路径,末尾带/
|
* @return
|
*/
|
public final static String getUploadFilePath(){
|
return uploadFilePath;
|
}
|
|
/**
|
* 下载文件目录,末尾带 /
|
* @return
|
*/
|
public final static String getDownloadFilePath(){
|
return downloadFilePath;
|
}
|
/**
|
* 得到 webRoot 的绝对目录,末尾带/
|
* @return
|
*/
|
public final static String getWebRoot(){
|
return webRoot;
|
}
|
|
/**
|
* 得到 WEB-INF 的绝对目录,末尾带/
|
* @return
|
*/
|
public final static String getWebInf(){
|
return webInf;
|
}
|
|
/**
|
* 得到系统 application 对象
|
* @return
|
*/
|
public final static ServletContext getApplication(){
|
return application;
|
}
|
|
/**
|
* 得到完整的带参数的当前地址
|
* @param req
|
* @return
|
*/
|
public final static String getFullUrl(HttpServletRequest req){
|
String url=req.getRequestURL().toString();
|
if(req.getQueryString()!=null && req.getQueryString().trim().length()>0){
|
url += "?"+req.getQueryString();
|
}
|
return url;
|
}
|
/**
|
* 得到URL的根, 末尾带/
|
*
|
* @param req
|
* @return
|
*/
|
public final static String getURLRoot(HttpServletRequest req){
|
String key = "FRAME_REQUEST_URL_ROOT";
|
if(req.getAttribute(key) !=null) {
|
return (String)req.getAttribute(key);
|
}
|
|
String url=req.getRequestURL().toString();
|
String uri = req.getRequestURI();
|
String root = req.getContextPath();
|
|
String head = "";
|
int pos = 0;
|
if(uri.equals("/")){
|
pos = url.indexOf(uri,url.indexOf("//")+2);
|
}else{
|
pos = url.indexOf(uri);
|
}
|
head = url.substring(0,pos);
|
|
if(!head.endsWith("/")){
|
head += "/";
|
}
|
if(!root.equals("")){
|
if(root.startsWith("/")){
|
root = root.substring(1);
|
}
|
head += root;
|
}
|
if(! head.endsWith("/")){
|
head = head + "/";
|
}
|
|
req.setAttribute(key, head);
|
return head;
|
}
|
|
static Logger log = Logger.getLogger(FrameUtil.class);
|
|
protected static void setDownloadFilePath(String path){
|
|
if(path ==null || path.trim().length()==0){
|
path = FrameUtil.getWebRoot()+"WEB-INF/upload";
|
new File(path).mkdirs();
|
downloadFilePath = path + "/";
|
return;
|
}
|
|
path = path.trim();
|
if(path.endsWith("\\") || path.endsWith("/")){
|
path = path.substring(0, path.length()-1);
|
}
|
|
File f = new File(path);
|
if(f.exists() && f.isDirectory()){
|
downloadFilePath = path + "/";
|
return;
|
}
|
|
if(path.startsWith("/") || path.startsWith("\\")){
|
path = path.substring(1);
|
}
|
|
f = new File(FrameUtil.getWebRoot() + path);
|
if(f.exists() && f.isDirectory()){
|
downloadFilePath = FrameUtil.getWebRoot() + path + "/";
|
return;
|
}
|
|
f = new File(FrameUtil.getWebInf() + path);
|
if(f.exists() && f.isDirectory()){
|
downloadFilePath = FrameUtil.getWebInf() + path + "/";
|
return;
|
}
|
// fuzhen
|
|
downloadFilePath = "D:\\";
|
|
return ;
|
//throw new RuntimeException(
|
// "必须在 framework.startup.FrameStartup 中指定文件上传路径");
|
}
|
protected static void setUploadFilePath(String path){
|
|
if(path ==null || path.trim().length()==0){
|
path = FrameUtil.getWebRoot()+"WEB-INF/upload";
|
new File(path).mkdirs();
|
uploadFilePath = path + "/";
|
return;
|
}
|
|
path = path.trim();
|
if(path.endsWith("\\") || path.endsWith("/")){
|
path = path.substring(0, path.length()-1);
|
}
|
|
File f = new File(path);
|
if(f.exists() && f.isDirectory()){
|
uploadFilePath = path + "/";
|
return;
|
}
|
|
if(path.startsWith("/") || path.startsWith("\\")){
|
path = path.substring(1);
|
}
|
|
f = new File(FrameUtil.getWebRoot() + path);
|
if(f.exists() && f.isDirectory()){
|
uploadFilePath = FrameUtil.getWebRoot() + path + "/";
|
return;
|
}
|
|
f = new File(FrameUtil.getWebInf() + path);
|
if(f.exists() && f.isDirectory()){
|
uploadFilePath = FrameUtil.getWebInf() + path + "/";
|
return;
|
}
|
|
// fuzhen
|
|
uploadFilePath = "D:\\";
|
|
return ;
|
|
//throw new RuntimeException(
|
//"必须在 framework.startup.FrameStartup 中指定文件上传路径");
|
}
|
|
protected static void setServletContext(ServletContext app) {
|
if(app ==null){
|
throw new RuntimeException("NULL ServletContext");
|
}
|
|
application = app;
|
|
webRoot = application.getRealPath("/").trim();
|
if(! (webRoot.endsWith("/") || webRoot.endsWith("\\"))){
|
webRoot = webRoot + "/";
|
}
|
|
log.info("webRoot: "+webRoot);
|
|
webInf = webRoot + "WEB-INF/";
|
log.info("WEB-INF: "+webInf);
|
|
}
|
|
protected static void setWebApplicationContext(WebApplicationContext context){
|
webApplicationContext = context;
|
}
|
|
/**
|
* 得到spring bean
|
* @param beanId
|
* @return
|
*/
|
public static Object getBean(String beanId){
|
return webApplicationContext.getBean(beanId);
|
}
|
|
// DataSourceTransactionManager txManager = (DataSourceTransactionManager)wac.getBean("txManager");
|
//
|
// DefaultTransactionDefinition def = new DefaultTransactionDefinition();
|
// def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
|
// TransactionStatus status = txManager.getTransaction(def);
|
// DefaultSqlSessionFactory sf = (DefaultSqlSessionFactory)wac.getBean("sqlSessionFactory");
|
// SqlSession sess = sf.openSession(false);
|
//
|
// UserMapper mapper = sess.getMapper(UserMapper.class);
|
//
|
// TSysUser user = new TSysUser();
|
// user.setUserId("40733b24-c5c2-11e5-b84d-6a9eda6d908a");
|
// user.setRealName("aaaaaaa");
|
//
|
// mapper.updateUser(user);
|
//
|
// //txManager.rollback(status);
|
// txManager.commit(status);
|
// sess.close();
|
|
}
|