java - Spring 应用程序中的正确文件路径

标签 java json spring filepath

这听起来很奇怪,但最近几个小时我试图从我的 java 类的 Spring 应用程序的 web 应用程序文件夹中编写一个 json 文件。我附上了快照,可以帮助您清楚地理解它。 click to Refer the file system snap here..."jsonfiles" is the folder i am writing files marked with red circle.请建议 java 类中 json 文件的正确文件路径。提前致谢。并且还建议这样做是一个好的做法吗?我对这种情况很陌生。

正在写入文件的 Java 代码:

FileWriter file = new FileWriter("../src/main/webaccess/jsonfiles/jsonfile.json")){
file.write(json.toString());
}

enter image description here

最佳答案

不,这不是将输出(在您的情况下为 JSON)文件放置在 webapp 文件夹中的正确路径。

原因:在部署过程中,您将失去此更改,因为 war 文件将被提取,并且您的 webapp 文件夹将被覆盖。

正确方法:写入 tomcat 目录之外的文件系统。一般做法是定义一个环境变量,指向应用程序具有写入权限的外部目录,并将所有内容写入其中。这与写入日志文件类似。

关于java - Spring 应用程序中的正确文件路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43110360/

相关文章:

java - 从二进制字符串转换时出现 NumberFormatException

java - instanceof 与重定向方法

c# - 使用 JSON 序列化作为持久化机制而不是 RDB

java - Spring JPA/Hibernate 存储库 findOne 方法返回 null

java - 在检查 Spring 中的错误之前,我可以在 BindingResult 中添加值吗?

java - Eclipse 崩溃 : "An error has occurred. See the log file"

java - 值为 null 的字符串和内存分配

javascript - 遍历 Handlebars 中的 json 对象重复项

java - GeoTools解析GeoJson时没有这样的属性错误

java - 你会使用 AOP 来跟踪积分吗?