Exemplo n.º 1
0
  /**
   * 提供相对于classpath的资源路径,返回文件的输入流
   *
   * @paramrelativePath必须传递资源的相对路径。是相对于classpath的路径。如果需要查找classpath外部的资源,需要使用../来查找
   * @return 文件输入流
   * @throwsIOException
   * @throwsMalformedURLException
   */
  public static InputStream getStream(String relativePath)
      throws MalformedURLException, IOException {
    if (!relativePath.contains("../")) {
      return getClassLoader().getResourceAsStream(relativePath);

    } else {
      return ClassLoaderUtil.getStreamByExtendResource(relativePath);
    }
  }
Exemplo n.º 2
0
  /**
   * @paramrelativePath 必须传递资源的相对路径。是相对于classpath的路径。如果需要查找classpath外部的资源,需要使用. ./来查找
   * @return资源的绝对URL
   * @throwsMalformedURLException
   */
  public static URL getExtendResource(String relativePath) throws MalformedURLException {

    // ClassLoaderUtil.log.info(Integer.valueOf(relativePath.indexOf("../")))
    // ;
    if (!relativePath.contains("../")) {
      return ClassLoaderUtil.getResource(relativePath);
    }
    String classPathAbsolutePath = ClassLoaderUtil.getAbsolutePathOfClassLoaderClassPath();
    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 = ClassLoaderUtil.containSum(wildcardString, "../");
    classPathAbsolutePath = ClassLoaderUtil.cutLastString(classPathAbsolutePath, "/", containSum);
    String resourceAbsolutePath = classPathAbsolutePath + relativePath;
    URL resourceAbsoluteURL = new URL(resourceAbsolutePath);
    return resourceAbsoluteURL;
  }
Exemplo n.º 3
0
 /**
  * @paramrelativePath必须传递资源的相对路径。是相对于classpath的路径。如果需要查找classpath外部的资源,需要使用../来查找
  * @return
  * @throwsMalformedURLException
  * @throwsIOException
  */
 public static InputStream getStreamByExtendResource(String relativePath)
     throws MalformedURLException, IOException {
   return ClassLoaderUtil.getStream(ClassLoaderUtil.getExtendResource(relativePath));
 }
Exemplo n.º 4
0
 /**
  * @paramresource
  * @return
  */
 public static URL getResource(String resource) {
   return ClassLoaderUtil.getClassLoader().getResource(resource);
 }
Exemplo n.º 5
0
 /**
  * 得到本Class所在的ClassLoader的Classpat的绝对路径。 URL形式的
  *
  * @return
  */
 public static String getAbsolutePathOfClassLoaderClassPath() {
   return ClassLoaderUtil.getClassLoader().getResource("").toString();
 }