Apply absolute and relative paths

Source: Internet
Author: User
This problem depends on where your profile is placed, and if you put it in the classes directory (or subdirectory) of the project, you can use * *. Class.getResource (' relative path ') to get the profile path. If it is a different directory, you can only get the directory of the project root + configuration file by ServletContext when the project starts to determine the path. and put the path to where the class file can be referenced.
The following is a class that I wrote when I was working on a project to get the path, which may not be very good. But I still hope to be of some help to you:
Package com.example.web;

Import Java.io.File;
Import Java.net.URL;

Import Javax.servlet.ServletContext;
Import Javax.servlet.http.HttpServletRequest;

/**
* Path Fetch class
* */
public class Webpath {
/**
* Get the absolute path of the project root directory
*
* @return such as: F:\TongJianpeng\J2EEUtil
* */
public static String Getabsolutepathwithproject () {
Return System.getproperty ("User.dir");
}

/**
* Get the drive letter of the project
* */
public static String Getdriverpathwithproject () {
return new File ("/"). GetAbsolutePath ();
}

/**
* Get the absolute path of the project root directory
*
* @return Project root. For example <br/> F:\tomcat\webapps\J2EEUtil\
* */
public static String Getabsolutepathwithwebproject (
HttpServletRequest request) {
Return Request.getsession (). Getservletcontext (). Getrealpath ("/");
}

/**
* Gets the absolute path of the specified directory under the project root directory
*
* @param the specified directory at the root of the project
*. For example:/login/
* @return The specified directory at the root of the project. For example:<br/> F:\tomcat\webapps\J2EEUtil\login\
* */
public static String Getabsolutepathwithwebproject (
HttpServletRequest request, String path) {
Return Request.getsession (). Getservletcontext (). Getrealpath (path);
}

/**
* Get the absolute path of the project root directory
*
* @return Project root. For example <br/> F:\tomcat\webapps\J2EEUtil\
* */
public static String Getabsolutepathwithwebproject (ServletContext context) {
Return Context.getrealpath ("/");
}

/**
* Gets the absolute path of the specified directory under the project root directory
*
* @param the specified directory at the root of the project
*. For example:/login/
* @return The specified directory at the root of the project. For example:<br/> F:\tomcat\webapps\J2EEUtil\login\
* */
public static String Getabsolutepathwithwebproject (ServletContext context,
String path) {
return Context.getrealpath (path);
}

/**
* Get the absolute path to the project Classpath directory
*
* @return Absolute path to the classes directory <br/>
* file:/f:/tomcat/webapps/j2eeutil/web-inf/classes/
* */
public static URL Getabsolutepathwithclass () {
Return WebPath.class.getResource ("/");
}

/**
* Gets the absolute path of the specified directory under the project Classpath directory
*
* @param path
* The specified directory under the classes directory. For example:/com/
* @return file:/f:/tomcat/webapps/j2eeutil/web-inf/classes/com/
* */
public static URL Getabsolutepathwithclass (String path) {
return WebPath.class.getResource (path);
}

/**
* Gets the absolute path of the directory where the specified class file is located
*
* @param clazz
* Class
* @return The absolute path of the class file. For example,:<br/> package com. Main.java class .<br/> under the Aries.Util.Web
* Path: file:/
* f:/tomcat/webapps/j2eeutil/web-inf/classes/com/aries/util/web/
* */
public static URL Getabsolutepathwithclass (Class clazz) {
Return Clazz.getresource ("");
}
}
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.