English 中文(简体)
如何获得文件路径?
原标题:How to get filepath?

我正在研究一个Eclipse Web Dynamic Project, 并试图访问我本地机器/服务器上的文件。

我要找的就是在代码Igniter中的“ base_url () ” 之类的东西, 它自动指向服务器所在的目录 。

我在用麦克

try{
          model.read(new FileInputStream(url),"");
      }catch(IOException e)
      {
          System.out.println("Exception caught"+e.getMessage());
      }

这是我正在研究的代码的一部分, 我试图输入正确的 URL 路径以读取 。

在搜索StackOverflow和其他地方后, 我发现了这个代码:

String url = request.getRequestURL().toString().replace(request.getRequestURI().substring(1), request.getContextPath())
            +"/WebContent/WEB-INF/test.xml";

这似乎不起作用。

然后我试图硬化编码路径目录, 只是为了意识到我不知道麦克文件系统是如何工作的: /

有人能分享这个光吗?

提前感谢您

问题回答

如果您使用 servlet 或其他东西(如WS),我并不太明白,然而, request 对象暴露了一种叫做 getRealPath () 的方法,该方法为您的文件系统中服务器的上下文提供了真正的路径。

所以,你需要修改你的代码 用这个:

String url = this.getServletContext().getRealPath("")+"/test.xml";

这与OS无关, 你肯定知道java是便携式的。

我觉得你想要的是

String path = getServletContext().getRealPath("/");

指向服务器中您应用程序的 bas 目录

e.g. /opt/tomcat/webapps/MyApp

我总是用"资源AsStream"来把它吸进课道。在网络应用程序中,平坦的文件名有问题。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签