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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值