使用 .war 文件部署时,为什么 getRealPath() 返回 null? [英] Why does getRealPath() return null when deployed with a .war file?

查看:17
本文介绍了使用 .war 文件部署时,为什么 getRealPath() 返回 null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

getRealPath() is returning the actual path in the local system, but returns null when deployed with a .war file.

<%@ 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>

谁能为我提供替代方案?如果您也展示一些示例代码,那将非常有帮助.

Can anyone provide me with an alternative for this? It would be very helpful if you showed some sample code too.

推荐答案

首先,ServletRequest.getRealPath(String path) 已弃用.适当的替换是:

For a start, ServletRequest.getRealPath(String path) is deprecated. The appropriate replacement is:

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

但是,ServletContext.getRealPath(String path) 状态:

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

"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)."

API 正在履行其合同!但是,一切都没有丢失,因为您可以使用以下方法从 WAR 加载资源,如 ServletContext:

So the API is fulfilling its contract! However, all is not lost, as you can load a resource from the WAR using the following method, as defined in ServletContext:

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

这篇关于使用 .war 文件部署时,为什么 getRealPath() 返回 null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆