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

mus*_*123 28 java xml jsp java-ee

getRealPath()返回本地系统中的实际路径,但在与.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>
Run Code Online (Sandbox Code Playgroud)

任何人都可以为我提供替代方案吗?如果您也展示了一些示例代码,那将非常有用.

Dav*_*ant 36

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

ServletContext context = session.getServletContext();
String realContextPath = context.getRealPath(request.getContextPath());
Run Code Online (Sandbox Code Playgroud)

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

" null 如果servlet容器因任何原因(例如,当从.war存档中提供内容时)无法将虚拟路径转换为实际路径,则此方法返回."

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

ServletContext context = session.getServletContext();
InputStream is = context.getResourceAsStream("generate.xml");
Run Code Online (Sandbox Code Playgroud)

  • 如果我确实需要路径怎么办?例如在这个问题中:http://stackoverflow.com/questions/6889728/referencing-a-file-within-servlet函数setOutputProperty需要获取一个路径的String,那我该怎么办? (3认同)
  • 如果你想*写*文件怎么办?getResourceAsStream返回一个InputStream,对吗? (2认同)
  • .this.getClass()的getResource( "/")的getPath(); (2认同)

小智 17

有点晚了,但是当我在WebLogic中遇到这个问题时,我遇到了这个问题.我的解决方案是将此添加到我的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>
Run Code Online (Sandbox Code Playgroud)

当您不希望(或不能)编辑WebLogic服务器上的配置时,我发现此解决方案更好.


小智 6

你使用Weblogic吗?

如果是 - 那么这是一个Weblogic问题,您可以在Weblogic管理控制台中修复 - >域 - > Web应用程序 - 单击复选框"已存档的实际路径".

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


归档时间:

查看次数:

55903 次

最近记录:

8 年 前