package cn.com.qmth.am.utils; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.net.URL; import org.apache.commons.io.IOUtils; public class ResouceUtil { public static URL getUrl(String path) { try { ClassLoader classLoader = ResouceUtil.class.getClassLoader(); URL url = classLoader.getResource(path); return url; } catch (Exception e) { throw new RuntimeException(e); } } public static InputStream getStream(String path) { try { ClassLoader classLoader = ResouceUtil.class.getClassLoader(); URL url = classLoader.getResource(path); return url.openStream(); } catch (Exception e) { throw new RuntimeException(e); } } public static String getContent(String path) { try { ClassLoader classLoader = ResouceUtil.class.getClassLoader(); URL url = classLoader.getResource(path); return readFileContent(url.openStream()); } catch (Exception e) { throw new RuntimeException(e); } } @SuppressWarnings("deprecation") private static String readFileContent(InputStream in) { StringBuilder content = new StringBuilder(); InputStreamReader streamReader = null; BufferedReader bufferedReader = null; try { String encoding = "UTF-8"; streamReader = new InputStreamReader(in, encoding); bufferedReader = new BufferedReader(streamReader); String line; while ((line = bufferedReader.readLine()) != null) { content.append(line); } } catch (Exception e) { throw new RuntimeException(e); } finally { IOUtils.closeQuietly(in); IOUtils.closeQuietly(streamReader); IOUtils.closeQuietly(bufferedReader); } return content.toString(); } }