如何从 servlet 写入具有 url 的文件
How to write on file having its url from a servlet
如何从 Servlet 写入具有 URL(例如 /WebContent/Database/myFile.json
)的文件?
约束:我不能使用绝对路径写入此文件。
附加信息:servlet 接收一个字符串作为 POST 参数,然后她应该将此字符串写入位于同一 servlet 上下文中的文件中。
更新
将文件写入 servlet 上下文 see this answer from @BalusC 是一个非常糟糕的主意。
不推荐但是这里是你如何获得这个上下文相对路径的绝对路径
ServletContext context = request.getSession().getServletContext();
String relPath = "/WebContent/Database/myFile.json";
String absPath = context.getRealPath(relPath);
如何从 Servlet 写入具有 URL(例如 /WebContent/Database/myFile.json
)的文件?
约束:我不能使用绝对路径写入此文件。
附加信息:servlet 接收一个字符串作为 POST 参数,然后她应该将此字符串写入位于同一 servlet 上下文中的文件中。
更新 将文件写入 servlet 上下文 see this answer from @BalusC 是一个非常糟糕的主意。
不推荐但是这里是你如何获得这个上下文相对路径的绝对路径
ServletContext context = request.getSession().getServletContext();
String relPath = "/WebContent/Database/myFile.json";
String absPath = context.getRealPath(relPath);