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; }
舉例:
該文件所在的發布根目錄
該文件所在的發布根路徑
該文件的真實路徑