在Java里处理文件的技巧

写这篇Blog,主要是因为看到太多的凌乱的,不安全的处理文件的代码了。甚至可以说每个项目都会有人喜欢写自己的一些FileUitl。。

下面介绍一些利用JDK7标准库来灵活处理文件的方法。

实用的工具类,Path,Paths,Files,FileSystem 

有一些很灵活的处理方法: 

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. //得到一个Path对象  
  2. Path path = Paths.get("/test/a.txt");  
  3. //Path转换File  
  4. File file = path.toFile();  
  5.   
  6. Files.readAllBytes(path);  
  7. Files.deleteIfExists(path);  
  8. Files.size(path);  

正确拼接路径不要手动拼接路径

不好的代码: 

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. String game = "foo";  
  2. File file = new File("~/test/" + game + ".txt");  
即使是要手动拼接路径,请使用下面两个平台无关的变量: 
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. System.out.println(File.pathSeparator);  
  2. System.out.println(File.separator);  
正确简洁的方法是使用Paths类: 
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. Path path = Paths.get("~/test/""foo""bar""a.txt");  
  2. System.out.println(path);  
  3. //  ~/test/foo/bar/a.txt  

读取文件的所有内容,文件的所有行

读取文件所有内容前,先判断文件大小,防止OOM。 

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public static byte[] readAllBytes(String fileName, long maxSize) throws IOException {  
  2.     Path path = Paths.get(fileName);  
  3.     long size = Files.size(path);  
  4.     if (size > maxSize) {  
  5.         throw new IOException("file: " + path + ", size:" + size + "> " + maxSize);  
  6.     }  
  7.     return Files.readAllBytes(path);  
  8. }  
  9.   
  10. public static List<String> readAlllines(String fileName, Charset charset, long maxSize) throws IOException {  
  11.     Path path = Paths.get(fileName);  
  12.     long size = Files.size(path);  
  13.     if (size > maxSize) {  
  14.         throw new IOException("file: " + path + ", size:" + size + "> " + maxSize);  
  15.     }  
  16.     return Files.readAllLines(path, charset);  
  17. }  

利用JDK7的特性,auto close,远离一堆的catch, close

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. Path path = Paths.get("~/test/""foo""bar""a.txt");  
  2. try (InputStream in = Files.newInputStream(path)) {  
  3.     // process  
  4.     //in.read();  
  5. }  

历遍目录

DK7新特性,FileVisitor 

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public class MyFileVisitor extends SimpleFileVisitor<Path>{  
  2.     @Override  
  3.     public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {  
  4.         System.out.println(file);  
  5.         return FileVisitResult.CONTINUE;  
  6.     }  
  7.       
  8.     public static void main(String[] args) throws IOException {  
  9.         Path path = Paths.get("/home/user/test");  
  10.         Files.walkFileTree(path, new MyFileVisitor());  
  11.     }  
  12. }  

判断文件是否在父路径下

网上流传一种递归判断parent的方式,http://stackoverflow.com/questions/18227634/check-if-file-is-in-subdirectory

但是查阅jdk代码后,发现getParent()函数是通过处理文件名得到的。所以直接比较文件前缀即可。 

请务必注意,file.getCanonicalPath()函数 

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public static boolean isSubFile(File parent, File child) throws IOException {  
  2.     return child.getCanonicalPath().startsWith(parent.getCanonicalPath());  
  3. }  
  4.   
  5.   
  6. public static boolean isSubFile(String parent, String child) throws IOException {  
  7.     return isSubFile(new File(parent), new File(child));  
  8. }  

监视文件改变

JDK7新特性,但是API比较难用。TODO 

淘宝有个diamond的配置管理项目,是利用定时器不断去读取来文件是否改变的。

JDK7则是利用了linux的inotify机制。

Web服务器防止非法的文件路径访问

字符截断攻击和文件历遍漏洞原理:在文件名中插入%00的URL编码,web服务器会把%00后面的内容抛弃。 

例如这样的URL:http://www.test.com/../../../../etc/passwd%00.gif

防范方法

  • 写入文件前,判断文件是否在父路径下,参考上面的函数。 
  • 利用Java的安全机制 
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // All files in /img/java can be read  
  2. grant codeBase "file:/home/programpath/" {  
  3.   permission java.io.FilePermission "/img/java""read";  
  4. };  
Tomcat的设置 
http://tomcat.apache.org/tomcat-7.0-doc/security-manager-howto.html

  • 静态资源不要自己手写代码去读取,尽量使用Web服务器或者Web框架的本身的静态资源映射功能。

比如Tomcat的默认自带的DefaultServlet:

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <servlet-mapping>  
  2.     <servlet-name>default</servlet-name>  
  3.     <url-pattern>/static/*</url-pattern>  
  4. </servlet-mapping>  
Spring mvc可以配置

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <mvc:resources mapping="/resources/**" location="/public-resources/"/>  
或者使用spring mvc里的DefaultServletHttpRequestHandler。这个默认优先级是最低的,也就是最后没人处理的URL会交给WebServer本身的default servlet去处理。比如Tomcat的就是上面所说的。

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <mvc:default-servlet-handler/>  
个人推荐使用DefaultServletHttpRequestHandler,因为Web容器的文件访问功能要比Spring mvc自身的要强大。比如Tomcat的DefaultServlet支持Etag,断点续传,缓存等。

参考:

http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html

http://svn.apache.org/repos/asf/tomcat/trunk/java/org/apache/catalina/servlets/DefaultServlet.java

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值