Java Web资源加载类

package com.yss.framework.api.util;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.Properties;

import com.yss.framework.api.logger.LogManager;

public class WebResourceLoader {
	private WebResourceLoader() {
	}

	public static URL getResource(String relativePath) {
		URL resourceAbsoluteURL = null;
		try {
			if (relativePath.indexOf("../") < 0) {
				return WebResourceLoader.class.getResource(relativePath);
			}
			String classPath = WebResourceLoader.class.getProtectionDomain().getCodeSource().getLocation().toString();
			classPath = classPath.substring(0,classPath.lastIndexOf("/"));
			if (relativePath.substring(0, 1).equals("/")) {
				relativePath = relativePath.substring(1);
			}
			String wildcardString = relativePath.substring(0, relativePath
					.lastIndexOf("../") + 3);
			relativePath = relativePath.substring(relativePath
					.lastIndexOf("../") + 3);
			int containSum = containSum(wildcardString, "../");
			classPath = cutLastString(classPath, "/", containSum);
			String resourceAbsolutePath = classPath + relativePath;
			resourceAbsoluteURL = new URL(resourceAbsolutePath);
		} catch (Exception e) {
			LogManager.getLogger(WebResourceLoader.class).log("获取资源路径出错!", e);
		}
		return resourceAbsoluteURL;
	}

	public static String getResourcePath(String relativePath) {
		try {
			return URLDecoder.decode(getResource(relativePath).getPath(),
					"UTF-8");
		} catch (UnsupportedEncodingException e) {
			LogManager.getLogger(WebResourceLoader.class).log("获取资源路径出错!", e);
		}
		return null;
	}

	public File getResourceFile(String relativePath) {
		try {
			return new File(getResource(relativePath).getFile());
		} catch (Exception e) {
			return null;
		}
	}

	public static InputStream getStream(String relativePath) {
		try {
			return getStream(getResource(relativePath));
		} catch (Exception e) {
			LogManager.getLogger(WebResourceLoader.class).log(e.getMessage(), e);
			return null;
		}
	}

	public static Properties getProperties(String resource) {
		Properties properties = new Properties();
		InputStream in = null;
		try {
			in = getStream(resource);
			properties.load(in);
			return properties;
		} catch (Exception e) {
			LogManager.getLogger(WebResourceLoader.class).log("加载属性文件出错!", e);
			return null;
		} finally {
			try {
				in.close();
			} catch (Exception e) {
				LogManager.getLogger(WebResourceLoader.class).log("加载属性文件出错!", e);
			}
		}
	}

	private static int containSum(String source, String dest) {
		int containSum = 0;
		int destLength = dest.length();
		while (source.indexOf(dest) >= 0) {
			containSum = containSum + 1;
			source = source.substring(destLength);
		}
		return containSum;
	}

	private static InputStream getStream(URL url) {
		try {
			if (url != null)
				return url.openStream();
		} catch (IOException e) {
			LogManager.getLogger(WebResourceLoader.class).log("获取文件流出错!", e);
		}
		return null;
	}

	private static String cutLastString(String source, String dest, int num) {
		for (int i = 0; i < num; i++)
			source = source.substring(0, source.lastIndexOf(dest, source
					.length() - 2) + 1);
		return source;
	}

	@SuppressWarnings("unused")
	private static String getStringForNum(int num, String str) {
		String ret = "";
		for (; num > 0; num--)
			ret += str;
		return ret;
	}
}

 

转载于:https://my.oschina.net/u/3237893/blog/829693

你可能感兴趣的:(Java Web资源加载类)