在Java裏處理文件的技巧
寫這篇Blog,主要是因為看到太多的淩亂的,不安全的處理文件的代碼了。甚至可以說每個項目都會有人喜歡寫自己的一些FileUitl。。
下麵介紹一些利用JDK7標準庫來靈活處理文件的方法。
實用的工具類,Path,Paths,Files,FileSystem
有一些很靈活的處理方法:
//得到一個Path對象 Path path = Paths.get("/test/a.txt"); //Path轉換File File file = path.toFile(); Files.readAllBytes(path); Files.deleteIfExists(path); Files.size(path);
正確拚接路徑不要手動拚接路徑
不好的代碼:
String game = "foo"; File file = new File("~/test/" + game + ".txt");即使是要手動拚接路徑,請使用下麵兩個平台無關的變量:
System.out.println(File.pathSeparator); System.out.println(File.separator);正確簡潔的方法是使用Paths類:
Path path = Paths.get("~/test/", "foo", "bar", "a.txt"); System.out.println(path); // ~/test/foo/bar/a.txt
讀取文件的所有內容,文件的所有行
讀取文件所有內容前,先判斷文件大小,防止OOM。
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
Path path = Paths.get("~/test/", "foo", "bar", "a.txt"); try (InputStream in = Files.newInputStream(path)) { // process //in.read(); }
曆遍目錄
DK7新特性,FileVisitor
public class MyFileVisitor extends SimpleFileVisitor<Path>{ @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { 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的方式,https://stackoverflow.com/questions/18227634/check-if-file-is-in-subdirectory
但是查閱jdk代碼後,發現getParent()函數是通過處理文件名得到的。所以直接比較文件前綴即可。
請務必注意,file.getCanonicalPath()函數 。
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:https://www.test.com/../../../../etc/passwd%00.gif
防範方法
- 寫入文件前,判斷文件是否在父路徑下,參考上麵的函數。
- 利用Java的安全機製
// All files in /img/java can be read grant codeBase "file:/home/programpath/" { permission java.io.FilePermission "/img/java", "read"; };Tomcat的設置
https://tomcat.apache.org/tomcat-7.0-doc/security-manager-howto.html
- 靜態資源不要自己手寫代碼去讀取,盡量使用Web服務器或者Web框架的本身的靜態資源映射功能。
比如Tomcat的默認自帶的DefaultServlet:
<servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>/static/*</url-pattern> </servlet-mapping>Spring mvc可以配置
<mvc:resources mapping="/resources/**" location="/public-resources/"/>或者使用spring mvc裏的DefaultServletHttpRequestHandler。這個默認優先級是最低的,也就是最後沒人處理的URL會交給WebServer本身的default servlet去處理。比如Tomcat的就是上麵所說的。
<mvc:default-servlet-handler/>個人推薦使用DefaultServletHttpRequestHandler,因為Web容器的文件訪問功能要比Spring mvc自身的要強大。比如Tomcat的DefaultServlet支持Etag,斷點續傳,緩存等。
參考:
https://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html
https://svn.apache.org/repos/asf/tomcat/trunk/java/org/apache/catalina/servlets/DefaultServlet.java
最後更新:2017-04-03 12:56:18