zoukankan      html  css  js  c++  java
  • servlet中获取各种相对地址(服务器、服务器所在本地磁盘、src等)。

    【本文简介】

    本文将提供javaWeb中经常使用到的相对路径的获取方法,分别有:

    1. url基本地址
    2. 带目录的url地址
    3. 服务器的根路径
    4. 服务器所在的 本地磁盘路径
    5. 服务器所在的本地磁盘路径,带文件夹
    6. SRC目录下的文件的路径,带文件夹

    并封装成一个工具类,以便复用。

    【java代码】

     1 package com.zjm.www.util;
     2 
     3 import javax.servlet.http.HttpServletRequest;
     4 
     5 /**
     6  * @描述 : 获取各种相对路径的工具类
     7  * @作者 :小M
     8  * @博客 : http://www.cnblogs.com/xiaoMzjm/
     9  * @时间 : 2014/07/30
    10  */
    11 public class PathUtil{
    12 
    13      /**
    14      * 获取服务的url基本地址
    15      * @param request    请求
    16      * @return           例如:http://localhost:8080/test/ , 其中test为项目名
    17      */
    18     public static String getServerPath(HttpServletRequest request){
    19         String path = request.getContextPath();
    20         String basePath = request.getScheme() + "://"+ request.getServerName() + ":" + request.getServerPort()+ path + "/";
    21           
    22         return basePath;
    23     }
    24     /**
    25      * 获取带目录的url地址
    26      * @param request    请求
    27      * @param folderName 文件夹名 ,例如:DownLoadFile
    28      * @return           例如:http://localhost:8080/test/DownLoadFile
    29      */
    30     public static String getServerPath(HttpServletRequest request,String folderName){
    31         String path = request.getContextPath();
    32         String basePath = request.getScheme() + "://"+ request.getServerName() + ":" + request.getServerPort()+ path + "/";
    33         return basePath+folderName;
    34     }
    35     /**
    36      * 获取服务器的根路径
    37      * @param request    请求
    38      * @return           例如:/test , 其中test为项目名
    39      */
    40     public static String getServerContextPath(HttpServletRequest request){
    41         String path = request.getContextPath();
    42         return path;
    43     }
    44       
    45     /**
    46      * 获取服务器所在的 本地磁盘路径
    47      * @param request    请求
    48      * @return            例如:D:Dsofeapache-tomcat-8.0.5webapps	est , 其中test为项目名
    49      */
    50     public static String getDiskPath(HttpServletRequest request){
    51         String path = request.getServletContext().getRealPath("/")+"\";
    52         return path;
    53     }
    54     /**
    55      * 获取服务器所在的本地磁盘路径,带文件夹
    56      * @param request        请求
    57      * @param folderName     文件夹名 ,例如:DownLoadFile
    58      * @return               例如:D:Dsofeapache-tomcat-8.0.5webapps	estDownLoadFile
    59      */
    60     public static String getDiskPath(HttpServletRequest request,String folderName){
    61         String path = request.getServletContext().getRealPath("/")+"\";
    62         return path+folderName;
    63     }
    64     
    65     /**
    66      * 获取SRC目录下的文件的路径,带文件夹
    67      * @param folderName    
    68      * @return                例如:/F:/myEclipse2013WokeSpace/TestByServlet/WebRoot/WEB-INF/classes/test.txt
    69      */
    70     public String getSRCPath(String folderName){
    71         String path = this.getClass().getClassLoader().getResource(folderName).getPath();
    72         return path;
    73     }
    74 }
    View Code

    【该工具类附件】

    复制在浏览器打开既可下载。

     http://files.cnblogs.com/xiaoMzjm/PathUtil.rar

  • 相关阅读:
    sqlite3中给表添加列
    webpack打包后服务端__dirname失效问题
    nodejs查看本机hosts文件域名对应ip
    vscode远程调试node服务端
    git 删除错误commit
    npm安装模块没有权限解决办法
    node gyp编译所需要的环境
    npm和yarn的淘宝镜像添加
    笨办法学Python 笔记
    梯度下降法优化线性回归算法参数
  • 原文地址:https://www.cnblogs.com/xiaoMzjm/p/3878758.html
Copyright © 2011-2022 走看看