如何从WEB-INF / classes目录中的类写入WebContent目录中的文件 [英] How to write to a file in WebContent directory from a class in WEB-INF/classes directory
问题描述
我在动态Web应用程序的WEB-INF / Classes目录中有一个Java类UpdateStats。这个类有一个函数writeLog(),它将一些日志写入文本文件。我希望这个文本文件位于webcontent目录中。因此,每次调用该函数时,都会在该文本文件中写入更新统计信息。
问题是如何从该函数中的webcontent目录中提供该文本文件的路径,该函数位于WEB-INF / Classes目录中。
String path = getServletContext()。 getRealPath( WEB-INF /../);
文件文件=新文件(路径);
String fullPathToYourWebappRoot = file.getCanonicalPath();
希望这有帮助。
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.
You can get your webapp root directory from ServletContext:
String path = getServletContext().getRealPath("WEB-INF/../");
File file = new File(path);
String fullPathToYourWebappRoot = file.getCanonicalPath();
Hope this helps.
这篇关于如何从WEB-INF / classes目录中的类写入WebContent目录中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!