站内搜索: 请输入搜索关键词

当前页面: 开发资料首页JSP 专题javabean怎么用相对路径读取web-inf下的abc.txt文件?

javabean怎么用相对路径读取web-inf下的abc.txt文件?

摘要: javabean怎么用相对路径读取web-inf下的abc.txt文件?


rt


用ServletContext.getRealPath(String path)


public java.lang.String getRealPath(java.lang.String path)Returns a String containing the real path for a given virtual path. For example, the path "/index.html" returns the absolute file path on the server's filesystem would be served by a request for "http://host/contextPath/index.html", where contextPath is the context path of this ServletContext..
The real path returned will be in a form appropriate to the computer and operating system on which the servlet container is running, including the proper path separators. This method returns null if the servlet container cannot translate the virtual path to a real path for any reason (such as when the content is being made available from a .war archive).

Parameters:
path - a String specifying a virtual path
Returns:
a String specifying the real path, or null if the translation cannot be performed



UP


学习


import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

HttpServletRequest request;
/*
这里request=传进来的HttpServletRequest对象
*/
path = request.getSession().getServletContext().getRealPath("")+"WEBAPP/web-inf/abc.txt"; //假设web服务目录是EBAPP

File file = new File(path);


↑返回目录
前一篇: 用jsp+servlet+javabean这种框架时,用servlet做流程控制,有没有更好的方案?
后一篇: mypath=getClass().getResource("").getPath()