为什么使用.war文件进行部署时,getRealPath()返回null?

getRealPath()返回本地系统中的实际path,但在使用.war文件进行部署时返回null。

 <%@ page import="java.io.*" %> <%@ page contentType="text/html;charset=ISO-8859-1" %> <% int iLf = 10; char cLf = (char)iLf; String a= application.getResource("/"); //String myfile = application.getRealPath("/")+ "generate.xml"; //String myfile = request.getContextPath()+"generate.xml"; //String myfile = request.getRealPath("/")+"generate.xml"; out.println(myfile); File outputFile = new File(myfile); outputFile.createNewFile(); FileWriter outfile = new FileWriter(outputFile); outfile.write(" <?xml version='1.0' encoding='UTF-8'?> "+cLf); outfile.write(" <playlist version='1' xmlns = 'http://xspf.org/ns/0/' > " +cLf); outfile.write(" <title>My Band Rocks Your Socks</title> "+cLf); outfile.write("<trackList>"+cLf); %> <%! String[] sports; %> <% sports = request.getParameterValues("sports"); out.println("<html><body><h1>hello</h1></body></html>"); if (sports != null) { for (int i = 0; i < sports.length; i++) { // outfile.writeln (sports[i]); String total=sports[i]; String[] sa=total.split("[,]"); // String[] sub=new String(); outfile.write("<track>"+cLf); for (int j=0;j<sa.length;j++) { // outfile.writeln(sa[j]); // outfile.writeln("sa["+j+"]="+sa[j]); if( j == 0) { outfile.write("<location>" + sa[0] +"</location>"+cLf); } else if (j == 1) { outfile.write("<image>" + sa[1] +"</image>"+cLf); } else if( j==2) { outfile.write("<title>" + sa[2] +"</title>"+cLf); } }// end of inner for loop() outfile.write("</track>"+cLf); //outfile.writeln(); }// end of outer for() } //else outfile.writeln ("<b>none<b>"); outfile.write(" </trackList> "+cLf); outfile.write(" </playlist> "+cLf); outfile.close(); %> <object type="application/x-shockwave-flash" width="400" height="170" data="xspf_player.swf?playlist_url=generate.xml"> <param name="movie" value="xspf_player.swf?playlist_url=generate.xml" /> </object> 

任何人都可以为我提供一个替代scheme吗? 如果你也展示了一些示例代码,这将是非常有帮助的。

首先,不推荐使用ServletRequest.getRealPath(String path) 。 适当的replace是:

 ServletContext context = session.getServletContext(); String realContextPath = context.getRealPath(request.getContextPath()); 

但是, ServletContext.getRealPath(String path)状态的API文档:

“如果servlet容器由于某种原因无法将虚拟path转换为真实path(例如从.war归档中获得内容时),则此方法返回null

所以API正在履行合同! 然而,所有的都不会丢失,因为您可以使用以下方法从WAR中加载资源,如ServletContext中所定义的:

 ServletContext context = session.getServletContext(); InputStream is = context.getResourceAsStream("generate.xml"); 

有点晚了,但是当我在WebLogic中遇到这个问题时,我遇到了这个问题。 我的解决scheme是添加到我的weblogic.xml

 <?xml version='1.0' encoding='UTF-8'?> <weblogic-web-app> <container-descriptor> <show-archived-real-path-enabled>true</show-archived-real-path-enabled> </container-descriptor> </weblogic-web-app> 

当您不想(或不能)编辑WebLogic服务器上的configuration时,我发现此解决scheme更好。

你使用Weblogic吗?

如果是 – 那么这是Weblogic问题,您可以在Weblogicpipe理控制台 – >域 – > Web应用程序中修复 – 单击checkbox“已存档的实际path已启用”。

请参阅: http : //ananthkannan.blogspot.com/2009/12/servletcontextgetrealpath-returns-null.html

如果你想写入

使用

 this.getClass().getResource("/").getPath(); 

得到path

我不相信有可能做你想做的事情。

你应该使用getResource从war文件中读取xml文件(这也可以在没有战争的情况下运行)

 servletContext.getResourceAsStream("/generate.xml") 

前导斜杠取决于generate.xml的存储位置。

我也有同样的问题。 调用getRealPath()在部署到独立服务器时返回null。 search了一段时间后,我find了解决scheme,这不是在代码中。 这是在你的networking服务器的configuration。

对于我来说,这是Weblogic 10.3,你去首页 – configuration – Web应用程序,设置存档实path启用为true。 重新启动服务器,一切工作正常。

希望这个帮助,关心。

这也解决了这个问题:

weblogic.xml中

 <?xml version = '1.0' encoding = 'windows-1252'?> <weblogic-web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.bea.com/ns/weblogic/weblogic-web-app http://www.bea.com/ns/weblogic/weblogic-web-app/1.0/weblogic-web-app.xsd" xmlns="http://www.bea.com/ns/weblogic/weblogic-web-app"> <container-descriptor> <index-directory-enabled>true</index-directory-enabled> <show-archived-real-path-enabled>true</show-archived-real-path-enabled> </container-descriptor> <virtual-directory-mapping> <local-path>bla.war</local-path> <url-pattern>*</url-pattern> </virtual-directory-mapping> <context-root>bla</context-root> 

注意context.getRealPath()在存在用户权限问题时可以返回null,检查在哪个用户下运行的Web服务器。

以下修补程序适合我。

 // I am using Struts2 ServletContext sc = (ServletContext) ac.get(StrutsStatics.SERVLET_CONTEXT); fileInputStream = sc.getResourceAsStream("test.xls"); 

部署战争文件后,我能够从上下文path获取文件。