java 如何从 WEB-INF/classes 目录中的类写入 WebContent 目录中的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16846080/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to write to a file in WebContent directory from a class in WEB-INF/classes directory
提问by kamlesh tajpuri
I have a Java Class UpdateStats in WEB-INF/Classes directory of a dynamic web application.This class has a function writeLog() which writes some logs to a text file.I want this text file to be in webcontent directory.Thus everytime the function is called updates stats are written in that text file. The problem is how to give the path of that text file in webcontent directory from within that function,which resides in WEB-INF/Classes directory.
我在动态 Web 应用程序的 WEB-INF/Classes 目录中有一个 Java 类 UpdateStats。这个类有一个函数 writeLog() 将一些日志写入文本文件。我希望这个文本文件位于 webcontent 目录中。因此每次函数称为更新统计信息写入该文本文件中。问题是如何从该函数中给出 webcontent 目录中该文本文件的路径,该函数位于 WEB-INF/Classes 目录中。
回答by Shinichi Kai
You can get your webapp root directory from ServletContext:
您可以从 ServletContext 获取您的 webapp 根目录:
String path = getServletContext().getRealPath("WEB-INF/../");
File file = new File(path);
String fullPathToYourWebappRoot = file.getCanonicalPath();
Hope this helps.
希望这可以帮助。
回答by Ketan
You can do something like below in your servlet,
您可以在 servlet 中执行以下操作,
When you do getServletContext().getRealPath()
and put some string argument the file will see at your webcontent location.
If you want something into WEB-INF, you can give fileName like "WEB-INF/my_updates.txt".
当您这样做getServletContext().getRealPath()
并放置一些字符串参数时,该文件将在您的 webcontent 位置看到。如果你想在 WEB-INF 中添加一些东西,你可以给 fileName 类似“WEB-INF/my_updates.txt”。
File update_log = null;
final String fileName = "my_updates.txt";
@Override
public void init() throws ServletException {
super.init();
String file_path = getServletContext().getRealPath(fileName);
update_log = new File(file_path);
if (!update_log.exists()) {
try {
update_log.createNewFile();
} catch (IOException e) {
e.printStackTrace();
System.out.println("Error while creating file : " + fileName);
}
}
}
public synchronized void update_to_file(String userName,String query) {
if (update_log != null && update_log.exists()) {
FileOutputStream fos = null;
try {
fos = new FileOutputStream(update_log, true);
fos.write((getCurrentFormattedTime()+" "+userName+" "+query+"\n").getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
回答by RickDavis
To write a file you need to know absolute path of your web content directory on server as file class require absolute path.
要编写文件,您需要知道服务器上 Web 内容目录的绝对路径,因为文件类需要绝对路径。
File f = new File("/usr/local/tomcat/webapps/abc/yourlogfile.txt");
FileOutputStream out = new FileOutputStream(f);
out.writeLog("Data");
Assumption : abc is your project name
假设:abc 是您的项目名称
WebContent is not any directory when you deploy application. All files under web content goes directly under project name.
部署应用程序时,WebContent 不是任何目录。Web 内容下的所有文件都直接位于项目名称下。