Jakarta ee 如何在j2ee项目中获取资源的相对路径
我有一个动态Web项目,它有一个平面文件(或者说文本文件)。我已经创建了一个servlet,我需要在其中使用这个文件 我的代码如下:Jakarta ee 如何在j2ee项目中获取资源的相对路径,jakarta-ee,jboss,relative-path,war,Jakarta Ee,Jboss,Relative Path,War,我有一个动态Web项目,它有一个平面文件(或者说文本文件)。我已经创建了一个servlet,我需要在其中使用这个文件 我的代码如下: protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // String resource = request.getParameter ("json") ;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// String resource = request.getParameter ("json") ;
if ( resource != null && !resource.equals ( "" ) ) {
//use getResourceAsStream ( ) to properly get the file.
InputStream is = getServletContext ().getResourceAsStream ("rateJSON") ;
if ( is != null ) { // the resource exists
response.setContentType("application/json");
response.setHeader("Pragma", "No-cache");
response.setDateHeader("Expires", 0);
response.setHeader("Cache-Control", "no-cache");
StringWriter sw = new StringWriter ( ) ;
for ( int c = is.read ( ) ; c != -1; c = is.read ( ) ) {
sw.write ( c ) ;
}
PrintWriter out = response.getWriter();
out.print (sw.toString ()) ;
out.flush();
}
}
}
问题是,输入流是具有空值
我不知道如何获得正确的相对路径。
我使用JBOSS作为应用服务器
我已经在动态Web项目的WebContent目录中添加了资源文件。
作为另一种方法,我尝试了以下方法:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
ServletConfig config = getServletConfig();
String contextName = config.getInitParameter("ApplicationName");
System.out.println("context name"+ contextName);
String contextPath = config.getServletContext().getRealPath(contextName);
System.out.println("context Path"+contextPath);
//contextPath = contextPath.substring(0, contextPath.indexOf(contextName));
contextPath += "\\rateJSON.txt";
System.out.println(contextPath);
String resource = request.getParameter ("json") ;
System.out.println("Hi there1"+resource);
if ( resource != null && !resource.equals ( "" ) ) {
System.out.println("Hi there");
//use getResourceAsStream ( ) to properly get the file.
//InputStream is = getServletContext ().getResourceAsStream (resource) ;
InputStream is = getServletConfig().getServletContext().getResourceAsStream(contextPath);
if ( is != null ) { // the resource exists
System.out.println("Hi there2");
response.setContentType("application/json");
response.setHeader("Pragma", "No-cache");
response.setDateHeader("Expires", 0);
response.setHeader("Cache-Control", "no-cache");
StringWriter sw = new StringWriter ( );
for ( int c = is.read ( ) ; c != -1; c = is.read ( ) ) {
sw.write ( c ) ;
System.out.println(c);
}
PrintWriter out = response.getWriter();
out.print (sw.toString ()) ;
System.out.println(sw.toString());
out.flush();
}
}
}
contextPath的值现在是:C:\JBOSS\JBOSS-5.0.1.GA\server\default\tmp\4p72206b-uo5r7k-g0vn9pof-1-g0vsh0o9-b7\Nationwide.war\WEB-INF\rateJSON
但是在这个位置没有rateJSON文件?JBOSS似乎没有把这个文件放到App.war中,或者没有部署它???
有人能帮我吗?首先,检查Nationwide.war以确保包含rateJSON.txt。如果是,请尝试:
String rootPath = getServletConfig().getServletContext().getRealPath("/");
File file = new File(realPath + "WEB-INF/rateJSON.txt");
InputStream is = new FileInputStream(file);
首先,检查Nationwide.war以确保包含rateJSON.txt。如果是,请尝试:
String rootPath = getServletConfig().getServletContext().getRealPath("/");
File file = new File(realPath + "WEB-INF/rateJSON.txt");
InputStream is = new FileInputStream(file);
我认为这个(getServletContext().getRealPath())在
如果servlet容器由于任何原因(例如当内容从.war存档中可用时)无法将虚拟路径转换为真实路径,则此方法返回null
我认为这个(getServletContext().getRealPath())在
如果servlet容器由于任何原因(例如当内容从.war存档中可用时)无法将虚拟路径转换为真实路径,则此方法返回null
在Nationwide.war文件中没有“rateJSON”文件。为什么?出于某种原因,打包战争的任何东西都不包括该文件。您是在使用Ant脚本构建WAR,还是在IDE中自动部署到服务器?在Nationwide.WAR中自动部署到服务器“rateJSON”文件不存在。为什么?出于某种原因,打包战争的任何东西都不包括该文件。您是使用Ant脚本构建WAR,还是自动部署到IDE中的服务器?自动部署到服务器