Java7引入了java.nio.file.Path作为java.io.File的possible replacement。
使用File时,当我访问特定下的文件时,我将执行以下操作:
File parent = new File("c:\\tmp");
File child = new File(parent, "child"); // this accesses c:\tmp\child
用Path做这件事的方法是什么?
我想这会起作用的:
Path parent = Paths.get("c:\\tmp");
Path child = Paths.get(parent.toString(), "child");
但调用parent.toString()
似乎很难看。有没有更好的方法?
发布于 2011-11-22 21:37:58
发布于 2022-01-26 17:28:49
对于任何发现这个问题的人,特别是只在指定路径内的文件中查找的,您必须意识到路径遍历攻击。
请参阅:Filtering upwards path traversal in Java (or Scala)
检查路径是否以根目录开头是critical。
Path parent = Paths.get("C:\\tmp");
Path child = parent.resolve("chlid").normalize();
if (!child.startsWith(parent)) {
throw new IllegalArgumentException("Potential Path Traversal Attack");
}
https://stackoverflow.com/questions/8227499
复制相似问题