尝试更新src/web/prod文件夹(jsp)中的文件 [英] Trying to update the file in src/web/prod folder (jsp)

查看:122
本文介绍了尝试更新src/web/prod文件夹(jsp)中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在同时使用jboss,eclipse和svn.我必须将文件保存在测试文件夹中: test/create.jsp test/data.txt .我想做的是,当我调用 create.jsp 时,它将更新我的 data.txt .显然,我希望我的data.txt保持原样,因为其他脚本正试图从中读取.

I am using jboss, eclipse and svn together. I have to files in my test folder: test/create.jsp and test/data.txt . What I want to do is when I call my create.jsp it will update my data.txt . Obviously I want my data.txt to stay where it is as other scripts are tryong to read from it.

我尝试了数十种新方法将路径放置到File对象,但是由于某种原因,它在jboss war文件夹下创建了文件.

I have tried dozens of new ways to put the path to my File object but for some reason it creates the file under jboss war folders.

尝试:

ServletContext app = getServletContext();
String path1 = app.getRealPath("/");
File f = new File(path1);
// AND
File f = new File("../../data.txt");

推荐答案

假定/test文件夹位于Web内容中,则需要以下内容:

Assuming that /test folder is located in the webcontent, then you need the following:

String absolutePath = getServletContext().getRealPath("/test/data.txt");
File file = new File(absolutePath);

String webcontentRoot = getServletContext().getRealPath("/");
File file = new File(webcontentRoot, "test/data.txt");

看到了吗? Java IO仅了解本地磁盘文件系统路径,而不能理解URL或上下文外部的路径. ServletContext#getRealPath()用于将相对Web路径转换为本地磁盘文件系统的绝对路径,而该路径又可以在通常的Java IO中使用.您应该从不在Java IO内容中使用相对路径.您将取决于当前的工作目录,该目录可能因环境/情况而异.

Do you see it? The Java IO only understands local disk file system paths, not URL's or paths outside the context. The ServletContext#getRealPath() is to be used to convert a relative web path to an absolute local disk file system path which in turn can be used further in the usual Java IO stuff. You should never use relative paths in Java IO stuff. You will be dependent on the current working directory which may differ per environment/situation.

也就是说,您通常不想将文件写入Web内容.每当您重新部署WAR时,它们都会迷路.而是在webapp之外的其他地方创建固定磁盘文件系统路径并加以利用.甚至更好的是,使用独立的SQL数据库:)

That said, you normally don't want to write files to the webcontent. They will get lost whenever you redeploy the WAR. Rather create a fixed disk file system path somewhere else outside the webapp and make use of it. Or even better, make use of an independent SQL database :)

这篇关于尝试更新src/web/prod文件夹(jsp)中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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