写这篇Blog,主要是因为看到太多的凌乱的,不安全的处理文件的代码了。甚至可以说每个项目都会有人喜欢写自己的一FileUitl。
下面介绍一些利用JDK7标准库来灵活处理文件的方法。
实用的工具类,Path,Paths,Files,FileSystem
有一些很灵活的处理方法:
1 2 3 4 5 6 7 8 | //得到一个Path对象Path path = Paths.get("/test/a.txt");//Path转换FileFile file = path.toFile();Files.readAllBytes(path);Files.deleteIfExists(path);Files.size(path); |
正确拼接路径不要手动拼接路径
不好的代码:
1 2 | String game = "foo"; File file = new File("~/test/" + game + ".txt"); |
即使是要手动拼接路径,请使用下面两个平台无关的变量:
1 2 | System.out.println(File.pathSeparator); System.out.println(File.separator); |
正确简洁的方法是使用Paths类:
1 2 3 | Path path = Paths.get("~/test/", "foo", "bar", "a.txt"); System.out.println(path); // ~/test/foo/bar/a.txt |
读取文件的所有内容,文件的所有行
读取文件所有内容前,先判断文件大小,防止OOM。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | public static byte[] readAllBytes(String fileName, long maxSize) throws IOException { Path path = Paths.get(fileName); long size = Files.size(path); if (size > maxSize) { throw new IOException("file: " + path + ", size:" + size + "> " + maxSize); } return Files.readAllBytes(path); } public static List<String> readAlllines(String fileName, Charset charset, long maxSize) throws IOException { Path path = Paths.get(fileName); long size = Files.size(path); if (size > maxSize) { throw new IOException("file: " + path + ", size:" + size + "> " + maxSize); } return Files.readAllLines(path, charset); } |
利用JDK7的特性,auto close,远离一堆的catch, close
1 2 3 4 5 | Path path = Paths.get("~/test/", "foo", "bar", "a.txt"); try (InputStream in = Files.newInputStream(path)) { // process //in.read(); } |
历遍目录
DK7新特性,FileVisitor
1 2 3 4 5 6 7 8 9 10 11 12 | public class MyFileVisitor extends SimpleFileVisitor<Path>{ @Override
System.out.println(file); return FileVisitResult.CONTINUE; } public static void main(String[] args) throws IOException { Path path = Paths.get("/home/user/test"); Files.walkFileTree(path, new MyFileVisitor()); }} |
判断文件是否在父路径下
网上流传一种递归判断parent的方式,http://stackoverflow.com/questions/18227634/check-if-file-is-in-subdirectory
但是查阅jdk代码后,发现getParent()函数是通过处理文件名得到的。所以直接比较文件前缀即可。
请务必注意,file.getCanonicalPath()函数 。
1 2 3 4 5 6 7 | public static boolean isSubFile(File parent, File child) throws IOException { return child.getCanonicalPath().startsWith(parent.getCanonicalPath()); } public static boolean isSubFile(String parent, String child) throws IOException { return isSubFile(new File(parent), new File(child)); } |
监视文件改变
JDK7新特性,但是API比较难用。TODO
淘宝有个diamond的配置管理项目,是利用定时器不断去读取来文件是否改变的。
JDK7则是利用了linux的inotify机制。
Web服务器防止非法的文件路径访问
字符截断攻击和文件历遍漏洞原理:在文件名中插入%00的URL编码,web服务器会把%00后面的内容抛弃。
例如这样的URL:http://www.test.com/../../../../etc/passwd%00.gif
防范方法
写入文件前,判断文件是否在父路径下,参考上面的函数。
利用Java的安全机制
1 2 3 4 | // All files in /img/java can be readgrant codeBase "file:/home/programpath/" { permission java.io.FilePermission "/img/java", "read";}; |
Tomcat的设置
http://tomcat.apache.org/tomcat-7.0-doc/security-manager-howto.html
静态资源不要自己手写代码去读取,尽量使用Web服务器或者Web框架的本身的静态资源映射功能。
比如Tomcat的默认自带的DefaultServlet:
1 2 3 4 | <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>/static/*</url-pattern></servlet-mapping> |
Spring mvc可以配置
1 | <mvc:resources mapping="/resources/**" location="/public-resources/"/> |
或者使用spring mvc里的DefaultServletHttpRequestHandler。这个默认优先级是最低的,也就是最后没人处理的URL会交给WebServer本身的default servlet去处理。比如Tomcat的就是上面所说的。
1 | <mvc:default-servlet-handler/> |
个人推荐使用DefaultServletHttpRequestHandler,因为Web容器的文件访问功能要比Spring mvc自身的要强大。比如Tomcat的DefaultServlet支持Etag,断点续传,缓存等。





