【问题标题】:file created using getRealPath gets created on .metada/.plugins folder not in WebContent project folder使用 getRealPath 创建的文件在 .metada/.plugins 文件夹上创建,而不是在 WebContent 项目文件夹中
【发布时间】:2014-07-01 04:58:38
【问题描述】:

我正在尝试使用Servlet 中的以下代码创建一个文件:

File outfile= new File(servletContext.getRealPath("/Output/output.xml"));
OutputStream os = new FileOutputStream(outfile);

来自 J2EE 应用程序 (CallMain),文件在部署的临时路径中创建

.metadata.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\CallMain\Output

实际上我希望在当前项目文件夹中创建文件:WebContent.

【问题讨论】:

  • 试试这个File file = new File(getServletContext().getRealPath("/Output/output.xml"));
  • 谢谢,但结果相同!
  • 你有webapp下创建的输出文件夹吗?
  • 是的网页内容/输出。
  • 嘿试试这样 getServletContext().getRealPath("/")+"Output/output.xml";

标签: eclipse jakarta-ee


【解决方案1】:

实际上,当您使用嵌入式 tomcat 服务器在 eclipse 中运行 servlet 时

getServletContext().getRealPath("WEB-INF")

给你这个

D:\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\DynaServletProject\WEB-INF

但是当您从独立的 Apache Tomcat 运行 servlet 时:

getServletContext().getRealPath("WEB-INF")

这给了你

D:\apache-tomcat-7.0.33\webapps\DynaServletProject\WEB-INF

虽然在 Eclipse 中你可以给出绝对路径,但我认为这不是最好的方法

【讨论】:

    【解决方案2】:

    默认情况下,您的应用程序从工作区的 .metadata 目录中隐藏的目录暂存和运行。那 是 在运行时的“真实路径”。检查您的服务器配置以获取有关从工作区文件夹部署应用程序目录的选项。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-09
      • 1970-01-01
      • 1970-01-01
      • 2013-09-22
      • 2014-08-02
      • 2018-12-02
      相关资源
      最近更新 更多