寫這篇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);
- 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的方式,http://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:http://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";
- };
http://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>
- <mvc:resources mapping="/resources/**" location="/public-resources/"/>
- <mvc:default-servlet-handler/>
參考:
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