View Javadoc
1   package gboat2.attachment.util;
2   
3   import java.io.File;
4   import java.io.FileInputStream;
5   import java.io.InputStream;
6   import java.text.SimpleDateFormat;
7   import java.util.Date;
8   import java.util.Properties;
9   
10  import org.apache.commons.lang3.RandomStringUtils;
11  import org.apache.commons.lang3.StringUtils;
12  import org.dom4j.Document;
13  import org.dom4j.DocumentException;
14  import org.dom4j.DocumentFactory;
15  import org.dom4j.Node;
16  import org.dom4j.io.SAXReader;
17  import org.slf4j.Logger;
18  import org.slf4j.LoggerFactory;
19  
20  /**
21   *  读取系统上传文件的路径的配置文件
22   * @author tanxw
23   * @since jdk1.6
24   * @date 2012-4-13
25   *  
26   */
27  public class ConfigUtil {
28  	
29  	private static final Logger logger = LoggerFactory.getLogger(ConfigUtil.class);
30  	
31  	private static final String SAVE_PATH_ROOT = "save-path-root";
32  	
33  	private static final String CONTEXT_URI = "context-uri";
34  	
35  	private static final String DOWNLOAD_STRATEGY = "download-strategy";
36  	
37  	public static final String NGINX_STRATEGY = "nginx";
38  	
39  	public static final String STREAM_STRATEGY = "stream";
40  	
41  	public static final String APACHE_STRATEGY = "apache";
42  	
43  	public static final String LIGHTTPD_STRATEGY = "lighttpd";
44  	
45  	/**
46  	 * cache config
47  	 */
48  	protected static Properties props = new Properties();
49  	
50  	protected ConfigUtil() {
51  	}
52  	
53  	static {
54  		DocumentFactory df = DocumentFactory.getInstance();
55  		SAXReader reader = new SAXReader(df);
56  		Document document = null;
57  		InputStream resourceAsStream = null;
58  		// 取得XML路
59  		try {
60  			//优先读取web root下面的配置文件,如果没有再读取默认配置文件
61  			String webroot = System.getProperty("webapp.gboat2.root");
62  			if (StringUtils.isNotEmpty(webroot)) {
63  				String filepath = ensurePath(webroot) + "WEB-INF/config/upload.xml";
64  				File file = new File(filepath);
65  				if (file.exists()) {
66  					resourceAsStream = new FileInputStream(file);
67  				}
68  			}
69  			
70  			//读取默认配置文件
71  			if (null == resourceAsStream) {
72  				resourceAsStream = ConfigUtil.class.getClassLoader().getResourceAsStream("upload.xml");
73  			}
74  			
75  			document = reader.read(resourceAsStream);
76  			//String separator = System.getProperty("file.separator");
77  			
78  			//取得下载上下文路径
79  			String contextPath = document.selectSingleNode("/upload/context-uri").getText().trim();
80  			props.put(CONTEXT_URI, ensurePath(contextPath));
81  			Node downStrategyNode = document.selectSingleNode("/upload/download-strategy");
82  			if (null != downStrategyNode && null != downStrategyNode.getText()) {
83  				props.put(DOWNLOAD_STRATEGY, downStrategyNode.getText().trim());
84  			}
85  			//取得保存文件的根路径
86  			String savepathRoot = "";
87  			if (isWindows()) {
88  				savepathRoot = document.selectSingleNode("/upload/save-path/windows").getText().trim();
89  			} else {
90  				savepathRoot = document.selectSingleNode("/upload/save-path/linux").getText().trim();
91  			}
92  			props.put(SAVE_PATH_ROOT, ensurePath(savepathRoot));
93  			
94  		} catch (Exception e) {
95  			logger.error("读取文件上传的配置文件出错!", e);
96  		}
97  	}
98  	
99  	public static String getDownloadStrategy() {
100 		return props.getProperty(DOWNLOAD_STRATEGY, NGINX_STRATEGY);
101 	}
102 	
103 	/**
104 	 * 得到由当前年月日组成的相对路径
105 	 * @return String end with "/", but not start with "/".
106 	 */
107 	public static String generateRelativeSaveDir() {
108 		Date date = new Date();
109 		SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
110 		String dateString = sf.format(date);
111 		return dateString + "/";
112 	}
113 	
114 	/**
115 	 *根据原始文件名生成新的文件名 :即当前时间戳+4位随机数+原始扩展名 
116 	 * @param fileName
117 	 * @return 生成的新文件名
118 	 */
119 	public static String genearteNewFilename(String originalName) {
120 		int pos = -1;
121 		String extName = "";
122 		if (StringUtils.isNotEmpty(originalName)) {
123 			pos = originalName.lastIndexOf(".");
124 		}
125 		if (pos != -1) {
126 			extName = originalName.substring(pos);
127 		}
128 		String filenamePrefix = String.valueOf(new Date().getTime());
129 		String filename = filenamePrefix + RandomStringUtils.randomNumeric(4) + extName;
130 		return filename;
131 	}
132 	
133 	/**
134 	 * @return context down path, it always end with "/", eg: "files/".
135 	 */
136 	public static String getDownloadContextPath() {
137 		String contextPath = props.getProperty(CONTEXT_URI);
138 		return ensurePath(contextPath);
139 	}
140 	
141 	/**
142 	 * make sure the path end with "/"
143 	 * @param path
144 	 * @return
145 	 */
146 	public static String ensurePath(String path) {
147 		path = path.replace("\\", "/");
148 		
149 		if (!path.endsWith("/")) {
150 			path += "/";
151 		}
152 		return path;
153 	}
154 	
155 	/**
156 	 * @return fiel save path root, it always end with "/", eg: "c:/files/".
157 	 */
158 	public static String getSavepathRoot() {
159 		String root = props.getProperty(SAVE_PATH_ROOT);
160 		return ensurePath(root);
161 	}
162 	
163 	public static boolean isWindows() {
164 		String os = System.getProperty("os.name");
165 		return null != os && os.indexOf("Windows") != -1;
166 	}
167 	
168 	public static void main(String[] args) throws DocumentException {
169 		
170 		System.out.print(ensurePath("a\\a\\a"));
171 	}
172 }