zoukankan      html  css  js  c++  java
  • struts2获取文件真实路径

    CreateTime--2017年8月25日15:59:33

    Author:Marydon

    struts2获取文件真实路径

    需要导入:

    import java.io.FileNotFoundException;
    import org.apache.struts2.ServletActionContext; 

    方法封装

    /**
     * 获取指定路径的实际路径(文件所在磁盘路径)
     * 
     * @param servletContext
     * @param path 相对于目录发布所在路径的路径
     * @return 文件真实路径
     * @throws FileNotFoundException
     */
    public static String getRealPath(String path) throws FileNotFoundException {
    
        // Interpret location as relative to the web application root directory.
        if (!path.startsWith("/")) {
            path = "/" + path;
        }
        String realPath = ServletActionContext.getServletContext().getRealPath(path);
        if (realPath == null) {
            throw new FileNotFoundException("未找到文件:" + path);
        }
        return realPath;
    }

    举例:

      该文件所在的发布根目录

      该文件所在的发布根路径

      该文件的真实路径

     

  • 相关阅读:
    mysql对表操作的各种语句
    Map遍历两种方式
    hibernate3
    Spring、mybaits整合
    mybaits注解
    mybaits 框架运用
    mybatis入门
    限制文本框字符数
    Unity3D Mathf函数
    Unity3d 粒子工具注释
  • 原文地址:https://www.cnblogs.com/Marydon20170307/p/7428461.html
Copyright © 2011-2022 走看看